标签:native return 电话 import constrain www custom public oct
一对多:(一个客户对应多个联系人)
客户实体:Customer.java
package cn.future.domain; import java.util.HashSet; import java.util.Set; public class Customer { /* * CREATE TABLE `cst_customer` ( `cust_id` BIGINT(32) NOT NULL AUTO_INCREMENT COMMENT ‘客户编号(主键)‘, `cust_name` VARCHAR(32) NOT NULL COMMENT ‘客户名称(公司名称)‘, `cust_source` VARCHAR(32) DEFAULT NULL COMMENT ‘客户信息来源‘, `cust_industry` VARCHAR(32) DEFAULT NULL COMMENT ‘客户所属行业‘, `cust_level` VARCHAR(32) DEFAULT NULL COMMENT ‘客户级别‘, `cust_phone` VARCHAR(64) DEFAULT NULL COMMENT ‘固定电话‘, `cust_mobile` VARCHAR(16) DEFAULT NULL COMMENT ‘移动电话‘, PRIMARY KEY (`cust_id`) ) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; */ private Long cust_id; private String cust_name; private String cust_source; private String cust_industry; private String cust_level; private String cust_phone; private String cust_mobile; //使用set集合表达一对多的关系 private Set<LinkMan> linkMans = new HashSet<LinkMan>(); public Set<LinkMan> getLinkMans() { return linkMans; } public void setLinkMans(Set<LinkMan> linkMans) { this.linkMans = linkMans; } public Long getCust_id() { return cust_id; } public void setCust_id(Long cust_id) { this.cust_id = cust_id; } public String getCust_name() { return cust_name; } public void setCust_name(String cust_name) { this.cust_name = cust_name; } public String getCust_source() { return cust_source; } public void setCust_source(String cust_source) { this.cust_source = cust_source; } public String getCust_industry() { return cust_industry; } public void setCust_industry(String cust_industry) { this.cust_industry = cust_industry; } public String getCust_level() { return cust_level; } public void setCust_level(String cust_level) { this.cust_level = cust_level; } public String getCust_phone() { return cust_phone; } public void setCust_phone(String cust_phone) { this.cust_phone = cust_phone; } public String getCust_mobile() { return cust_mobile; } public void setCust_mobile(String cust_mobile) { this.cust_mobile = cust_mobile; } @Override public String toString() { return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + "]"; } }
客户实体对应的映射文件:Customer.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.future.domain" > <class name="Customer" table="cst_customer" > <id name="cust_id" > <generator class="native"></generator> </id> <property name="cust_name" column="cust_name" ></property> <property name="cust_source" column="cust_source" ></property> <property name="cust_industry" column="cust_industry" ></property> <property name="cust_level" column="cust_level" ></property> <property name="cust_phone" column="cust_phone" ></property> <property name="cust_mobile" column="cust_mobile" ></property> <!-- 一对多的关系在配置文件中配置 --> <!-- name属性:集合属性名 column属性:外键列名 class属性:与Costomer关联的对象完整类名 --> <set name="linkMans"> <key column="lkm_cust_id"></key> <one-to-many class="LinkMan"></one-to-many> </set> </class> </hibernate-mapping>
联系人实体:LinkMan.java
package cn.future.domain; public class LinkMan { /*CREATE TABLE `cst_linkman` ( `lkm_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT ‘联系人编号(主键)‘, `lkm_name` varchar(16) DEFAULT NULL COMMENT ‘联系人姓名‘, `lkm_cust_id` bigint(32) NOT NULL COMMENT ‘客户id‘, `lkm_gender` char(1) DEFAULT NULL COMMENT ‘联系人性别‘, `lkm_phone` varchar(16) DEFAULT NULL COMMENT ‘联系人办公电话‘, `lkm_mobile` varchar(16) DEFAULT NULL COMMENT ‘联系人手机‘, `lkm_email` varchar(64) DEFAULT NULL COMMENT ‘联系人邮箱‘, `lkm_qq` varchar(16) DEFAULT NULL COMMENT ‘联系人qq‘, `lkm_position` varchar(16) DEFAULT NULL COMMENT ‘联系人职位‘, `lkm_memo` varchar(512) DEFAULT NULL COMMENT ‘联系人备注‘, PRIMARY KEY (`lkm_id`), KEY `FK_cst_linkman_lkm_cust_id` (`lkm_cust_id`), CONSTRAINT `FK_cst_linkman_lkm_cust_id` FOREIGN KEY (`lkm_cust_id`) REFERENCES `cst_customer` (`cust_id`) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;*/ private Long lkm_id; private String lkm_name; private Character lkm_gender; private String lkm_phone; private String lkm_mobile; private String lkm_email; private String lkm_qq; private String lkm_position; private String lkm_memo; //表达多对一的关系 private Customer customer; public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } public Long getLkm_id() { return lkm_id; } public void setLkm_id(Long lkm_id) { this.lkm_id = lkm_id; } public String getLkm_name() { return lkm_name; } public void setLkm_name(String lkm_name) { this.lkm_name = lkm_name; } public Character getLkm_gender() { return lkm_gender; } public void setLkm_gender(Character lkm_gender) { this.lkm_gender = lkm_gender; } public String getLkm_phone() { return lkm_phone; } public void setLkm_phone(String lkm_phone) { this.lkm_phone = lkm_phone; } public String getLkm_mobile() { return lkm_mobile; } public void setLkm_mobile(String lkm_mobile) { this.lkm_mobile = lkm_mobile; } public String getLkm_email() { return lkm_email; } public void setLkm_email(String lkm_email) { this.lkm_email = lkm_email; } public String getLkm_qq() { return lkm_qq; } public void setLkm_qq(String lkm_qq) { this.lkm_qq = lkm_qq; } public String getLkm_position() { return lkm_position; } public void setLkm_position(String lkm_position) { this.lkm_position = lkm_position; } public String getLkm_memo() { return lkm_memo; } public void setLkm_memo(String lkm_memo) { this.lkm_memo = lkm_memo; } }
联系人实体对应的映射文件:LinkMan.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.future.domain" > <class name="LinkMan" table="cst_linkman" > <id name="lkm_id" > <generator class="native"></generator> </id> <property name="lkm_name" column="lkm_name" ></property> <property name="lkm_gender" column="lkm_gender" ></property> <property name="lkm_phone" column="lkm_phone" ></property> <property name="lkm_mobile" column="lkm_mobile" ></property> <property name="lkm_email" column="lkm_email" ></property> <property name="lkm_qq" column="lkm_qq" ></property> <property name="lkm_position" column="lkm_position" ></property> <property name="lkm_memo" column="lkm_memo" ></property> <!-- 多对一的关系在配置文件中配置 --> <!-- name属性:引用属性名 column属性:外键列名 class属性:与LinkMan关联的对象完整类名 --> <many-to-one name="customer" column="lkm_cust_id" class="Customer"></many-to-one> </class> </hibernate-mapping>
注意:
客户:因为一个客户对应多个联系人,所以在客户数据库表中没有关于联系人的任何字段信息,但是在客户实体类中却维护了联系人的set集合并且在客户的映射文件中也有维护联系人类,并注明在联系人表中存放的外键是这个客户的主键,也就是说客户的映射文件自动把客户的cust_id字段对应到联系人表中的lkm_cust_id字段
联系人:联系人实体类中存放了对应客户的整个类对象,但在联系人表中却只维护了对应客户的主键(作为外键),联系人的映射文件中也有对这个外键的描述
实体类和映射文件中紫色的文字为hibernate队医一对多|多对一的描述
标签:native return 电话 import constrain www custom public oct
原文地址:http://www.cnblogs.com/ms-grf/p/7236310.html