码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate逍遥游记-第5章映射一对多-02双向inverse

时间:2016-03-20 00:37:22      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:

1.

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping
 3 PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5 <hibernate-mapping >
 6 
 7    <class name="mypack.Monkey" table="MONKEYS">
 8      
 9       <id name="id" type="long" column="ID">
10         <generator class="increment"/>
11       </id>
12    
13       <property name="name" type="string" column="NAME" />
14         
15       <many-to-one
16         name="team"
17         column="TEAM_ID"
18         class="mypack.Team"
19         cascade="save-update"
20        />
21 
22     </class>
23  
24 </hibernate-mapping>

 

2.

 1 package mypack;
 2 
 3 public class Monkey {
 4 
 5 
 6      private long id;
 7      private String name;
 8      private Team team;
 9 
10     public Monkey() {}
11 
12     public Monkey(String name, Team team) {
13        this.name = name;
14        this.team = team;
15     }
16    
17     public long getId() {
18         return this.id;
19     }
20     
21     public void setId(long id) {
22         this.id = id;
23     }
24     public String getName() {
25         return this.name;
26     }
27     
28     public void setName(String name) {
29         this.name = name;
30     }
31     public Team getTeam() {
32         return this.team;
33     }
34     
35     public void setTeam(Team team) {
36         this.team = team;
37     }
38 }

 

3.

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping
 3 PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5 <hibernate-mapping >
 6 
 7   <class name="mypack.Team" table="TEAMS" >
 8     <id name="id" type="long" column="ID">
 9       <generator class="increment"/>
10     </id>
11 
12     <property name="name" type="string" column="NAME" />
13 <!--
14     <set 
15         name="monkeys"
16         cascade="all-delete-orphan" 
17         inverse="true"
18          >
19         
20         <key column="TEAM_ID" />
21         <one-to-many class="mypack.Monkey" />
22      </set>   
23 -->
24     <set 
25         name="monkeys"
26         inverse="true"
27         cascade="save-update" 
28         >
29         
30         <key column="TEAM_ID" />
31         <one-to-many class="mypack.Monkey" />
32      </set>   
33 
34   </class>
35 </hibernate-mapping>

 

4.

 1 package mypack;
 2 
 3 import java.util.HashSet;
 4 import java.util.Set;
 5 
 6 public class Team{
 7 
 8      private long id;
 9      private String name;
10      private Set monkeys = new HashSet();
11 
12     public Team() {}
13 
14     public Team(String name, Set monkeys) {
15        this.name = name;
16        this.monkeys = monkeys;
17     }
18    
19     public long getId() {
20         return this.id;
21     }
22     
23     public void setId(long id) {
24         this.id = id;
25     }
26     public String getName() {
27         return this.name;
28     }
29     
30     public void setName(String name) {
31         this.name = name;
32     }
33     public Set getMonkeys() {
34         return this.monkeys;
35     }
36     
37     public void setMonkeys(Set monkeys) {
38         this.monkeys = monkeys;
39     }
40 }

 

5.

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <!DOCTYPE hibernate-configuration
 3  PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
 4  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 5 <hibernate-configuration>
 6     <session-factory>
 7         <property name="dialect">
 8             org.hibernate.dialect.MySQLDialect
 9         </property>
10         <property name="connection.driver_class">
11             com.mysql.jdbc.Driver
12         </property>
13         <property name="connection.url">
14             jdbc:mysql://localhost:3306/sampledb
15         </property>
16         <property name="connection.username">
17             root
18         </property>
19         <property name="connection.password">
20             1234
21         </property>
22         <property name="show_sql">true</property>
23         <mapping resource="mypack/Team.hbm.xml" />
24         <mapping resource="mypack/Monkey.hbm.xml" />
25     </session-factory>
26 </hibernate-configuration>

 

6.

Hibernate逍遥游记-第5章映射一对多-02双向inverse

标签:

原文地址:http://www.cnblogs.com/shamgod/p/5296825.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!