【JAVAWEB】hibernate配置关系映射

一对多,多对一

public class Customer {
	private Long cust_id;
	private String cust_name;
	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集合表达1对多
	private Set<LinkMan> linkMens = new HashSet<LinkMan>();
}
public class LinkMan {
	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对象表达多对1关系	
	private Customer customer;
}
//Customer.hbm.xml
        <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_linkman" column="cust_linkman" ></property>
		<property name="cust_phone" column="cust_phone" ></property>
		<property name="cust_mobile" column="cust_mobile" ></property>
		<set name="linkMens" cascade="save-update" inverse="true">
		<key column="lkm_cust_id"></key>
		<one-to-many class="LinkMan"/>
		</set>
//LinkMan.hbm.xml
<property name="lkm_gender"></property>
		<property name="lkm_name" ></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>
		<many-to-one name="customer" column="lkm_cust_id" class="Customer" cascade="save-update"></many-to-one>

多对多

public class Role {
	private Long role_id;
	private String role_name;
	private String role_memo;
	Set<User> user = new HashSet<>();
}
public class User {

	private Long user_id;
	private String user_code;
	private String user_name;
	private String user_password;
	private Character user_state;
    Set<Role> role = new HashSet<>();
}
//Role.hbm.xml
<property name="role_name"  ></property>
		<property name="role_memo" ></property>
		   <set name="user" table="sys_user_role" inverse="true">
        <key column="role_id"></key>
        <many-to-many class="User" column="user_id"></many-to-many>
        </set>
//User.hbm.xml
<property name="user_code"  ></property>
		<property name="user_name" ></property>
		<property name="user_password" ></property>
		<property name="user_state"  ></property>
        <set name="role" table="sys_user_role" cascade="save-update">
        <key column="user_id"></key>
        <many-to-many class="Role" column="role_id"></many-to-many>
        </set>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值