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>

猜你喜欢

转载自blog.csdn.net/bigLiu66/article/details/81348849