1.少的一方Customer.java
public class Customer {
/*CREATE TABLE `cst_customer` (
`cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
`cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)',
`cust_user_id` bigint(32) DEFAULT NULL COMMENT '负责人id',
`cust_create_id` bigint(32) DEFAULT NULL COMMENT '创建人id',
`cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源',
`cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业',
`cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别',
`cust_linkman` varchar(64) DEFAULT NULL COMMENT '联系人',
`cust_phone` varchar(64) DEFAULT NULL COMMENT '固定电话',
`cust_mobile` varchar(16) DEFAULT NULL COMMENT '移动电话',
PRIMARY KEY (`cust_id`)
)*/
private Long cust_id; //客户编号(主键)
private String cust_name; //客户名称(公司名称)
private String cust_user_id; //负责人id
private String cust_create_id; //创建人id
private String cust_source; //客户信息来源
private String cust_industry; //客户所属行业
private String cust_level; //客户级别
private String cust_linkman; //联系人
private String cust_phone; //固定电话
private String cust_mobile; //移动电话
//创建一个set集合用来存储多个联系人信息
private Set<LinkMan> linkMans = new HashSet<>();
}
相应的配置文件Customer.hbm.xml
<hibernate-mapping package="com.wenhao.domain">
<class name="Customer" table="cst_customer">
<id name="cust_id" column="cust_id">
<generator class="native"></generator>
</id>
<property name="cust_name" column="cust_name"></property>
<property name="cust_user_id" column="cust_user_id"></property>
<property name="cust_create_id" column="cust_create_id"></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_linkman" column="cust_linkman"></property>
<property name="cust_phone" column="cust_phone"></property>
<property name="cust_mobile" column="cust_mobile"></property>
<!-- 集合,一对多关系,在配置文件中配置 -->
<!--
name属性: set集合属性名
column: 数据库中外键列名
class属性: 与我关联的对象的完整类名(hibernate-mapping package="com.wenhao.domain"中已经配置了包名,这里只需配置包名即可)
-->
<!--
cascade级联操作:
save-update : 级联保存
delete : 级联删除
all : save-update + delete
-->
<!--
inverse属性 : 配置关系是否维护
true : Customer 不维护关系
false(默认值) : Customer维护关系
inverse属性 : 性能优化,提高关系维护的性能
原则 : 无论怎样放弃,总要有一方维护外键关系
一对多关系中: 一的一方放弃维护外键关系,多的一方不能放弃维护外键关系
-->
<set name="linkMans" cascade="save-update" inverse="true">
<!-- 配置外键:外键在多的一方 -->
<key column="lkm_cust_id"></key>
<one-to-many class="LinkMan"/>
</set>
</class>
</hibernate-mapping>
2 .多的一方: LinkMan.java
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
)
*/
private Long lkm_id ;
private Character lkm_gender ;
private String lkm_name;
private String lkm_phone;
private String lkm_mobile;
private String lkm_email;
private String lkm_qq;
private String lkm_position;
private String lkm_memo;
//该对象属于哪个customer对象管理
private Customer customer ;
}
相应的配置文件LinkMan.hbm.xml
<hibernate-mapping package="com.wenhao.domain">
<class name="LinkMan" table="cst_linkman">
<id name="lkm_id" column="lkm_id">
<generator class="native"></generator>
</id>
<property name="lkm_name"></property>
<property name="lkm_gender" ></property>
<property name="lkm_phone"></property>
<property name="lkm_mobile" ></property>
<property name="lkm_email" ></property>
<property name="lkm_qq" ></property>
<property name="lkm_position" ></property>
<property name="lkm_memo" ></property>
<!-- 多对一 -->
<!--
name属性: Customer对象属性名
column: 数据库中外键列名
class属性: 与我关联的对象的完整类名
-->
<many-to-one name="customer" column="lkm_cust_id" class="Customer"></many-to-one>
</class>
</hibernate-mapping>