1. 程式人生 > >Hibernate學習之路(五)

Hibernate學習之路(五)

ring int 表示 gen prop generator 需要 blog hibernate

簡述 多對多關系映射

多對多關系映射需要一張中間表來維護關系

技術分享

  

  一:Role類與Function類

1 public class Function {
2     private int id;
3     private String name;
4     private String code;
5     private String url;
6     private Set<Role> roles = new HashSet<Role>();
7     
8     set/get....
9 }
1 public class Role {
2     private int id;
3     private String name;
4     private Set<Function> functions = new HashSet<Function>();
5     
6         set/get.....
7 }

  二:配置hbm.xml

<hibernate-mapping package="cn.pojo">
    <class name="Role">
        <id name="id">
            <generator class
="native"></generator> </id> <property name="name"></property> <!-- 多對多 --> <set name="functions" table="rol_func"> <!-- 表示當前類映射到關系表中的列 --> <key column="rid"></key> <!-- 表示所對應的另一方在關系表中的列 --> <many-to-many column="fid" class
="Function"></many-to-many> </set> </class> </hibernate-mapping>

雙向多對多映射同理配置hbm.xml

  

Hibernate學習之路(五)