数据关联是ORM的一个重要特征,但往往也是导致系统性能低下的诱因。
1.一对一关联
一对一关联有两种:
1>.主键关联:即两张表通过主键形成一对一的关联关系。
eg:用户表和护照表通过一对一主键关联,在hibernate中通过one-to-one节点进行声明;
user实体类
1
public
class
TUser
implements
java.io.Serializable
{
2
3 // Fields
4
5 /** *//**
6 *
7 */
8 private static final long serialVersionUID = -8186441339776871235L;
9 private Integer id;
10 private TPassport TPassport;
11 private String name;
12 private Integer age;
13 private Integer groupid;
14
15 // Constructors
16
17 /** *//** default constructor */
18 public TUser() {
19 }
20
21 /** *//** full constructor */
22 public TUser(TPassport TPassport, String name, Integer age, Integer groupid) {
23 this.TPassport = TPassport;
24 this.name = name;
25 this.age = age;
26 this.groupid = groupid;
27 }
28
29 // Property accessors
30
31 public Integer getId() {
32 return this.id;
33 }
34
35 public void setId(Integer id) {
36 this.id = id;
37 }
38
39 public TPassport getTPassport() {
40 return this.TPassport;
41 }
42
43 public void setTPassport(TPassport TPassport) {
44 this.TPassport = TPassport;
45 }
46
47 public String getName() {
48 return this.name;
49 }
50
51 public void setName(String name) {
52 this.name = name;
53 }
54
55 public Integer getAge() {
56 return this.age;
57 }
58
59 public void setAge(Integer age) {
60 this.age = age;
61 }
62
63 public Integer getGroupid() {
64 return this.groupid;
65 }
66
67 public void setGroupid(Integer groupid) {
68 this.groupid = groupid;
69 }
70
71}
2
3 // Fields
4
5 /** *//**
6 *
7 */
8 private static final long serialVersionUID = -8186441339776871235L;
9 private Integer id;
10 private TPassport TPassport;
11 private String name;
12 private Integer age;
13 private Integer groupid;
14
15 // Constructors
16
17 /** *//** default constructor */
18 public TUser() {
19 }
20
21 /** *//** full constructor */
22 public TUser(TPassport TPassport, String name, Integer age, Integer groupid) {
23 this.TPassport = TPassport;
24 this.name = name;
25 this.age = age;
26 this.groupid = groupid;
27 }
28
29 // Property accessors
30
31 public Integer getId() {
32 return this.id;
33 }
34
35 public void setId(Integer id) {
36 this.id = id;
37 }
38
39 public TPassport getTPassport() {
40 return this.TPassport;
41 }
42
43 public void setTPassport(TPassport TPassport) {
44 this.TPassport = TPassport;
45 }
46
47 public String getName() {
48 return this.name;
49 }
50
51 public void setName(String name) {
52 this.name = name;
53 }
54
55 public Integer getAge() {
56 return this.age;
57 }
58
59 public void setAge(Integer age) {
60 this.age = age;
61 }
62
63 public Integer getGroupid() {
64 return this.groupid;
65 }
66
67 public void setGroupid(Integer groupid) {
68 this.groupid = groupid;
69 }
70
71}
user.hbm.xml
1
<
hibernate-mapping
>
2 < class name ="entitys.TUser" table ="t_user" catalog ="tuser" >
3 < id name ="id" type ="java.lang.Integer" >
4 < column name ="id" />
5 < generator class ="native" />
6 </ id >
7 < one-to-one name ="TPassport" class ="entitys.TPassport" cascade ="all" outer-join ="true" >
8 </ one-to-one >
9 < property name ="name" type ="java.lang.String" >
10 < column name ="name" length ="50" />
11 </ property >
12 < property name ="age" type ="java.lang.Integer" >
13 < column name ="age" />
14 </ property >
15 < property name ="groupid" type ="java.lang.Integer" >
16 < column name ="groupid" />
17 </ property >
18 </ class >
19 </ hibernate-mapping
2 < class name ="entitys.TUser" table ="t_user" catalog ="tuser" >
3 < id name ="id" type ="java.lang.Integer" >
4 < column name ="id" />
5 < generator class ="native" />
6 </ id >
7 < one-to-one name ="TPassport" class ="entitys.TPassport" cascade ="all" outer-join ="true" >
8 </ one-to-one >
9 < property name ="name" type ="java.lang.String" >
10 < column name ="name" length ="50" />
11 </ property >
12 < property name ="age" type ="java.lang.Integer" >
13 < column name ="age" />
14 </ property >
15 < property name ="groupid" type ="java.lang.Integer" >
16 < column name ="groupid" />
17 </ property >
18 </ class >
19 </ hibernate-mapping
passport实体类
1
public
class
TPassport
implements
java.io.Serializable
{
2
3 // Fields
4
5 /** *//**
6 *
7 */
8 private static final long serialVersionUID = 5858122126575586075L;
9 private Integer id;
10 private String serial;
11 private Integer expiry;
12 private TUser TUsers;
13
14 // Constructors
15
16 /** *//** default constructor */
17 public TPassport() {
18 }
19
20 /** *//** minimal constructor */
21 public TPassport(String serial, Integer expiry) {
22 this.serial = serial;
23 this.expiry = expiry;
24 }
25
26 /** *//** full constructor */
27 public TPassport(String serial, Integer expiry, TUser TUsers) {
28 this.serial = serial;
29 this.expiry = expiry;
30 this.TUsers = TUsers;
31 }
32
33 // Property accessors
34
35 public Integer getId() {
36 return this.id;
37 }
38
39 public void setId(Integer id) {
40 this.id = id;
41 }
42
43 public String getSerial() {
44 return this.serial;
45 }
46
47 public void setSerial(String serial) {
48 this.serial = serial;
49 }
50
51 public Integer getExpiry() {
52 return this.expiry;
53 }
54
55 public void setExpiry(Integer expiry) {
56 this.expiry = expiry;
57 }
58
59 public TUser getTUsers() {
60 return TUsers;
61 }
62
63 public void setTUsers(TUser users) {
64 TUsers = users;
65 }
66}
2
3 // Fields
4
5 /** *//**
6 *
7 */
8 private static final long serialVersionUID = 5858122126575586075L;
9 private Integer id;
10 private String serial;
11 private Integer expiry;
12 private TUser TUsers;
13
14 // Constructors
15
16 /** *//** default constructor */
17 public TPassport() {
18 }
19
20 /** *//** minimal constructor */
21 public TPassport(String serial, Integer expiry) {
22 this.serial = serial;
23 this.expiry = expiry;
24 }
25
26 /** *//** full constructor */
27 public TPassport(String serial, Integer expiry, TUser TUsers) {
28 this.serial = serial;
29 this.expiry = expiry;
30 this.TUsers = TUsers;
31 }
32
33 // Property accessors
34
35 public Integer getId() {
36 return this.id;
37 }
38
39 public void setId(Integer id) {
40 this.id = id;
41 }
42
43 public String getSerial() {
44 return this.serial;
45 }
46
47 public void setSerial(String serial) {
48 this.serial = serial;
49 }
50
51 public Integer getExpiry() {
52 return this.expiry;
53 }
54
55 public void setExpiry(Integer expiry) {
56 this.expiry = expiry;
57 }
58
59 public TUser getTUsers() {
60 return TUsers;
61 }
62
63 public void setTUsers(TUser users) {
64 TUsers = users;
65 }
66}
passport.hbm.xml
2>唯一外键关联:
Hibernate中的唯一外键关联有"many-to-one"节点定义。
eg:用户与用户组通过用户group_id字段与用户组的id相关联。
user实体类
1
public
class
TUser2
implements
java.io.Serializable
{
2
3 // Fields
4
5 /** *//**
6 *
7 */
8 private static final long serialVersionUID = 830689105421107608L;
9 private Integer id;
10 private TGroup TGroup;
11 private String name;
12
13 // Constructors
14
15 /** *//** default constructor */
16 public TUser2() {
17 }
18
19 /** *//** full constructor */
20 public TUser2(TGroup TGroup, String name) {
21 this.TGroup = TGroup;
22 this.name = name;
23 }
24
25 // Property accessors
26
27 public Integer getId() {
28 return this.id;
29 }
30
31 public void setId(Integer id) {
32 this.id = id;
33 }
34
35 public TGroup getTGroup() {
36 return this.TGroup;
37 }
38
39 public void setTGroup(TGroup TGroup) {
40 this.TGroup = TGroup;
41 }
42
43 public String getName() {
44 return this.name;
45 }
46
47 public void setName(String name) {
48 this.name = name;
49 }
50
51}
2
3 // Fields
4
5 /** *//**
6 *
7 */
8 private static final long serialVersionUID = 830689105421107608L;
9 private Integer id;
10 private TGroup TGroup;
11 private String name;
12
13 // Constructors
14
15 /** *//** default constructor */
16 public TUser2() {
17 }
18
19 /** *//** full constructor */
20 public TUser2(TGroup TGroup, String name) {
21 this.TGroup = TGroup;
22 this.name = name;
23 }
24
25 // Property accessors
26
27 public Integer getId() {
28 return this.id;
29 }
30
31 public void setId(Integer id) {
32 this.id = id;
33 }
34
35 public TGroup getTGroup() {
36 return this.TGroup;
37 }
38
39 public void setTGroup(TGroup TGroup) {
40 this.TGroup = TGroup;
41 }
42
43 public String getName() {
44 return this.name;
45 }
46
47 public void setName(String name) {
48 this.name = name;
49 }
50
51}
group实体类
1
public
class
TGroup
implements
java.io.Serializable
{
2
3 // Fields
4
5 /** *//**
6 *
7 */
8 private static final long serialVersionUID = 469145792788411949L;
9 private Integer id;
10 private String name;
11 private TUser2 TUser2s;
12
13
14 // Constructors
15
16 /** *//** default constructor */
17 public TGroup() {
18 }
19
20 /** *//** full constructor */
21 public TGroup(String name,TUser2 TUser2s) {
22 this.name = name;
23 this.TUser2s = TUser2s;
24 }
25
26 // Property accessors
27
28 public Integer getId() {
29 return this.id;
30 }
31
32 public void setId(Integer id) {
33 this.id = id;
34 }
35
36 public String getName() {
37 return this.name;
38 }
39
40 public void setName(String name) {
41 this.name = name;
42 }
43
44 public TUser2 getTUser2s() {
45 return TUser2s;
46 }
47
48 public void setTUser2s(TUser2 user2s) {
49 TUser2s = user2s;
50 }
51
52
53
54}
2
3 // Fields
4
5 /** *//**
6 *
7 */
8 private static final long serialVersionUID = 469145792788411949L;
9 private Integer id;
10 private String name;
11 private TUser2 TUser2s;
12
13
14 // Constructors
15
16 /** *//** default constructor */
17 public TGroup() {
18 }
19
20 /** *//** full constructor */
21 public TGroup(String name,TUser2 TUser2s) {
22 this.name = name;
23 this.TUser2s = TUser2s;
24 }
25
26 // Property accessors
27
28 public Integer getId() {
29 return this.id;
30 }
31
32 public void setId(Integer id) {
33 this.id = id;
34 }
35
36 public String getName() {
37 return this.name;
38 }
39
40 public void setName(String name) {
41 this.name = name;
42 }
43
44 public TUser2 getTUser2s() {
45 return TUser2s;
46 }
47
48 public void setTUser2s(TUser2 user2s) {
49 TUser2s = user2s;
50 }
51
52
53
54}
user.hnm.xml
1
<
hibernate-mapping
>
2 < class name ="entitys.TUser2" table ="t_user2" catalog ="tuser" >
3 < id name ="id" type ="java.lang.Integer" >
4 < column name ="id" />
5 < generator class ="native" />
6 </ id >
7 < many-to-one name ="TGroup" class ="entitys.TGroup" unique ="true" >
8 < column name ="groupid" />
9 </ many-to-one >
10 < property name ="name" type ="java.lang.String" >
11 < column name ="name" length ="50" />
12 </ property >
13 </ class >
14 </ hibernate-mapping >
15
2 < class name ="entitys.TUser2" table ="t_user2" catalog ="tuser" >
3 < id name ="id" type ="java.lang.Integer" >
4 < column name ="id" />
5 < generator class ="native" />
6 </ id >
7 < many-to-one name ="TGroup" class ="entitys.TGroup" unique ="true" >
8 < column name ="groupid" />
9 </ many-to-one >
10 < property name ="name" type ="java.lang.String" >
11 < column name ="name" length ="50" />
12 </ property >
13 </ class >
14 </ hibernate-mapping >
15
group.hbm.xml
1
<
hibernate-mapping
>
2 < class name ="entitys.TGroup" table ="t_group" catalog ="tuser" >
3 < id name ="id" type ="java.lang.Integer" >
4 < column name ="id" />
5 < generator class ="native" />
6 </ id >
7 < property name ="name" type ="java.lang.String" >
8 < column name ="name" length ="50" />
9 </ property >
10 < one-to-one name ="TUser2s" class ="entitys.TUser2" property-ref ="TGroup" >
11 </ one-to-one >
12 </ class >
13 </ hibernate-mapping >
14
2 < class name ="entitys.TGroup" table ="t_group" catalog ="tuser" >
3 < id name ="id" type ="java.lang.Integer" >
4 < column name ="id" />
5 < generator class ="native" />
6 </ id >
7 < property name ="name" type ="java.lang.String" >
8 < column name ="name" length ="50" />
9 </ property >
10 < one-to-one name ="TUser2s" class ="entitys.TUser2" property-ref ="TGroup" >
11 </ one-to-one >
12 </ class >
13 </ hibernate-mapping >
14