标签:column 对象 文件 water _id 配置 set ons ring
<id name="userid" type="java.lang.Integer"> <column name="userid" /> <!-- 主键关联,将主键生成策略改为foreign,表明由关联类来生成主键, 即直接使用另外关联类的主键值,该实体类不能生成主键 --> <generator class="foreign" /> <para name="property">address</para><!-- --> </id> <property name="name" type="java.lang.String"> <column name="name" length="12" /> </property> <property name="password" type="java.lang.String"> <column name="password" length="16" /> </property>
<one-to-one name="address" class="Address" constrained="true"></one-to-one></span>
Addrss.hbm.xml文件配置
<id name="addressid" type="java.lang.Integer"> <column name="addressid" /> <generator class="identity" /> </id> <property name="addressinfo" type="java.lang.String"> <column name="addressinfo" length="12" /> </property>
<!-- class属性:另外个实体类,将其的address属性映射为address表的外键address_id, unique指定一对一映射 --> <many-to-one name="address" class="Address" unique="true"> <!-- 外键 --> <column name="address_id"></column> </many-to-one>
private Set<Address> setAddress = new HashSet<Address>(); public Set<Address> getSetAddress() { return setAddress; } public void setSetAddress(Set<Address> setAddress) { this.setAddress = setAddress; }
第二步:在Address.java表示一
private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; }
<!--set集合名称 --> <!--hibernate机制,双向维护外键 columen 外键名称--> <key column="user_address"></key> <one-to-many class="Address全路径"/> </set>
第五步:在多的配置文件(Address.hbm.xml)配置一的一方
<!--name属性 写address.java表示的user--> <many-to-one name="user" class="User全路径" column="外键名"></many-to-one>
private Set<Role> setRole = new HashSet<Role>(); public Set<Role> getSetRole() { return setRole; } public void setSetRole(Set<Role> setRole) { this.setRole = setRole; }
role.java表示user
private Set<User> setUser = new HashSet<User>(); public Set<User> getSetUser() { return setUser; } public void setSetUser(Set<User> setUser) { this.setUser = setUser; }
<!-- name 角色set集合名称 table 第三张表的名称 --> <set name="setRole" table="t_ub"> <!-- 配置当前映射文件在第三张表外键名称 --> <key column="uid"></key> <many-to-many class="路径" column="rid"></many-to-many> </set>
role配置user
<!-- name 角色set集合名称 table 第三张表的名称 --> <set name="setUser" table="t_ub"> <!-- 配置当前映射文件在第三张表外键名称 --> <key column="bid"></key> <many-to-many class="路径" column="uid"></many-to-many> </set>
Hibernate学习笔记(3)---hibernate关联关系映射
标签:column 对象 文件 water _id 配置 set ons ring
原文地址:http://www.cnblogs.com/durui/p/7582281.html