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

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.

时间: 2024-10-12 12:57:28

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

Hibernate逍遥游记-第5章映射一对多-01单向cascade、lazy

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 <c

Hibernate逍遥游记-第4章映射对象标识符-increment、identity、hilo、native、assigned、sequence、&lt;meta&gt;

1. 1 package mypack; 2 3 import java.lang.reflect.Constructor; 4 import org.hibernate.*; 5 import org.hibernate.cfg.Configuration; 6 import java.io.*; 7 import java.sql.Time; 8 import java.util.*; 9 10 public class BusinessService{ 11 public static S

Hibernate逍遥游记-第12章 映射值类型集合-002映射Bag(&lt;idbag&gt;&lt;collection-id&gt;)

1. 2. 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 &l

Hibernate逍遥游记-第12章 映射值类型集合-003映射List(&lt;list-index&gt;)

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 <c

Hibernate逍遥游记-第12章 映射值类型集合-001映射set(&lt;element&gt;)

1. 2. 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 &l

Hibernate逍遥游记-第10章 映射继承关系-002继承关系树中的根类对应一个表(discriminator、subclass)

1. 2. 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 &l

Hibernate逍遥游记-第2章-使用hibernate.properties

1. 1 package mypack; 2 3 import org.hibernate.*; 4 import org.hibernate.cfg.Configuration; 5 import java.util.*; 6 7 public class BusinessService{ 8 public static SessionFactory sessionFactory; 9 10 /** 初始化Hibernate,创建SessionFactory实例 */ 11 static{ 1

Hibernate逍遥游记-第15章处理并发问题-003乐观锁

1. 2. 1 drop database if exists SAMPLEDB; 2 create database SAMPLEDB; 3 use SAMPLEDB; 4 5 drop table if exists MONKEYS ; 6 create table MONKEYS( 7 ID bigint not null, 8 NAME varchar(15), 9 COUNT int, 10 VERSION integer, 11 primary key (ID) 12 ) type=

Hibernate逍遥游记-第15章处理并发问题-001事务并发问题及隔离机制介绍

1. 2.第一类丢失更新 3.脏读 4.虚读.幻读 5.不可重复读 6.第二类丢失更新 7.数据库的锁机制 8.数据库事务的隔离机制