Hibernate注解实现one-to-many双向关联的实例演示

目录:

  1. 概述
  2. 实体配置
  3. 单元测试

[一]、概述

本文大概讲述如何注解的方式实现one-to-many many-to-one的双向关联配置以及测试对实体CRUD基本操作,下面的演示以 Author (网络作者 为 one端)、Contact(联系方式 为many端)为例。

[二]、实体配置

Author.java

Contact.java

主要关注下代码中 @OneToMany @ManyToOne 两处配置。

[三]、单元测试

测试的类:OneToManyBidirectionalCRUDTest.java

AbstractHibernateBaseTest.java

运行单元测试,所有测试均通过,详细日志如下:

>>>> 测试 添加

Hibernate:

select

this_.ID as ID2_0_,

this_.DESCRIPTION as DESCRIPT2_2_0_,

this_.SEX_TYPE as SEX3_2_0_,

this_.TITLE as TITLE2_0_,

this_.USERNAME as USERNAME2_0_

from

DEMO_T_AUTHOR this_

Hibernate:

select

this_.ID as ID3_1_,

this_.AUTHOR_ID as AUTHOR5_3_1_,

this_.DETAILS as DETAILS3_1_,

this_.OTHER as OTHER3_1_,

this_.TYPE as TYPE3_1_,

author2_.ID as ID2_0_,

author2_.DESCRIPTION as DESCRIPT2_2_0_,

author2_.SEX_TYPE as SEX3_2_0_,

author2_.TITLE as TITLE2_0_,

author2_.USERNAME as USERNAME2_0_

from

DEMO_T_CONTACT this_

left outer join

DEMO_T_AUTHOR author2_

on this_.AUTHOR_ID=author2_.ID

Hibernate:

select

hibernate_sequence.nextval

from

dual

Hibernate:

select

hibernate_sequence.nextval

from

dual

Hibernate:

select

hibernate_sequence.nextval

from

dual

>>> one id = 75

>>> many id = 76

>>> many id = 77

Hibernate:

insert

into

DEMO_T_AUTHOR

(DESCRIPTION, SEX_TYPE, TITLE, USERNAME, ID)

values

(?, ?, ?, ?, ?)

Hibernate:

insert

into

DEMO_T_CONTACT

(AUTHOR_ID, DETAILS, OTHER, TYPE, ID)

values

(?, ?, ?, ?, ?)

Hibernate:

insert

into

DEMO_T_CONTACT

(AUTHOR_ID, DETAILS, OTHER, TYPE, ID)

values

(?, ?, ?, ?, ?)

Hibernate:

select

this_.ID as ID2_0_,

this_.DESCRIPTION as DESCRIPT2_2_0_,

this_.SEX_TYPE as SEX3_2_0_,

this_.TITLE as TITLE2_0_,

this_.USERNAME as USERNAME2_0_

from

DEMO_T_AUTHOR this_

Hibernate:

select

this_.ID as ID3_1_,

this_.AUTHOR_ID as AUTHOR5_3_1_,

this_.DETAILS as DETAILS3_1_,

this_.OTHER as OTHER3_1_,

this_.TYPE as TYPE3_1_,

author2_.ID as ID2_0_,

author2_.DESCRIPTION as DESCRIPT2_2_0_,

author2_.SEX_TYPE as SEX3_2_0_,

author2_.TITLE as TITLE2_0_,

author2_.USERNAME as USERNAME2_0_

from

DEMO_T_CONTACT this_

left outer join

DEMO_T_AUTHOR author2_

on this_.AUTHOR_ID=author2_.ID

>>>> 测试 更新

>>>> 更新 1:N中 N的数据

Hibernate:

select

contact0_.ID as ID3_1_,

contact0_.AUTHOR_ID as AUTHOR5_3_1_,

contact0_.DETAILS as DETAILS3_1_,

contact0_.OTHER as OTHER3_1_,

contact0_.TYPE as TYPE3_1_,

author1_.ID as ID2_0_,

author1_.DESCRIPTION as DESCRIPT2_2_0_,

author1_.SEX_TYPE as SEX3_2_0_,

author1_.TITLE as TITLE2_0_,

author1_.USERNAME as USERNAME2_0_

from

DEMO_T_CONTACT contact0_

left outer join

DEMO_T_AUTHOR author1_

on contact0_.AUTHOR_ID=author1_.ID

where

contact0_.ID=?

Contact [id=76, type=blog, details=http://www.micmiu.com/author/test, other=null]

Contact [id=76, type=blog, details=http://www.micmiu.com/author/test, other=update]

>>>> 更新 1:N中 1的数据

Author [id=75, username=test, title=测试人员, sexType=male, description=测试]

Hibernate:

select

contacts0_.AUTHOR_ID as AUTHOR5_2_1_,

contacts0_.ID as ID3_1_,

contacts0_.ID as ID3_0_,

contacts0_.AUTHOR_ID as AUTHOR5_3_0_,

contacts0_.DETAILS as DETAILS3_0_,

contacts0_.OTHER as OTHER3_0_,

contacts0_.TYPE as TYPE3_0_

from

DEMO_T_CONTACT contacts0_

where

contacts0_.AUTHOR_ID=?

order by

contacts0_.ID

Contact [id=76, type=blog, details=http://www.micmiu.com/author/test, other=update]

Contact [id=null, type=blog, details=http://www.micmiu.com/author/testnew, other=addnew]

Hibernate:

select

hibernate_sequence.nextval

from

dual

Hibernate:

insert

into

DEMO_T_CONTACT

(AUTHOR_ID, DETAILS, OTHER, TYPE, ID)

values

(?, ?, ?, ?, ?)

Hibernate:

update

DEMO_T_AUTHOR

set

DESCRIPTION=?,

SEX_TYPE=?,

TITLE=?,

USERNAME=?

where

ID=?

Hibernate:

update

DEMO_T_CONTACT

set

AUTHOR_ID=?,

DETAILS=?,

OTHER=?,

TYPE=?

where

ID=?

Hibernate:

delete

from

DEMO_T_CONTACT

where

ID=?

>>>> 测试读取和删除

>>>> 查询初始

Hibernate:

select

this_.ID as ID2_0_,

this_.DESCRIPTION as DESCRIPT2_2_0_,

this_.SEX_TYPE as SEX3_2_0_,

this_.TITLE as TITLE2_0_,

this_.USERNAME as USERNAME2_0_

from

DEMO_T_AUTHOR this_

Hibernate:

select

this_.ID as ID3_1_,

this_.AUTHOR_ID as AUTHOR5_3_1_,

this_.DETAILS as DETAILS3_1_,

this_.OTHER as OTHER3_1_,

this_.TYPE as TYPE3_1_,

author2_.ID as ID2_0_,

author2_.DESCRIPTION as DESCRIPT2_2_0_,

author2_.SEX_TYPE as SEX3_2_0_,

author2_.TITLE as TITLE2_0_,

author2_.USERNAME as USERNAME2_0_

from

DEMO_T_CONTACT this_

left outer join

DEMO_T_AUTHOR author2_

on this_.AUTHOR_ID=author2_.ID

>>>> size  one = 4 many =10

>>>> 查询 1:N中 N的数据

Contact [id=76, type=blog, details=http://www.micmiu.com/author/test, other=update]

>>>> 删除1:N中 N的数据后,查询N的数据

Hibernate:

delete

from

DEMO_T_CONTACT

where

ID=?

Hibernate:

select

this_.ID as ID3_1_,

this_.AUTHOR_ID as AUTHOR5_3_1_,

this_.DETAILS as DETAILS3_1_,

this_.OTHER as OTHER3_1_,

this_.TYPE as TYPE3_1_,

author2_.ID as ID2_0_,

author2_.DESCRIPTION as DESCRIPT2_2_0_,

author2_.SEX_TYPE as SEX3_2_0_,

author2_.TITLE as TITLE2_0_,

author2_.USERNAME as USERNAME2_0_

from

DEMO_T_CONTACT this_

left outer join

DEMO_T_AUTHOR author2_

on this_.AUTHOR_ID=author2_.ID

>>>> size many9

>>>> 查询 1:N中 1的数据

Author [id=75, username=test, title=测试人员, sexType=male, description=update]

>>>> 删除1:N中 1的数据后,查询1的数据

Hibernate:

select

contacts0_.AUTHOR_ID as AUTHOR5_2_1_,

contacts0_.ID as ID3_1_,

contacts0_.ID as ID3_0_,

contacts0_.AUTHOR_ID as AUTHOR5_3_0_,

contacts0_.DETAILS as DETAILS3_0_,

contacts0_.OTHER as OTHER3_0_,

contacts0_.TYPE as TYPE3_0_

from

DEMO_T_CONTACT contacts0_

where

contacts0_.AUTHOR_ID=?

order by

contacts0_.ID

Hibernate:

delete

from

DEMO_T_CONTACT

where

ID=?

Hibernate:

delete

from

DEMO_T_AUTHOR

where

ID=?

Hibernate:

select

this_.ID as ID2_0_,

this_.DESCRIPTION as DESCRIPT2_2_0_,

this_.SEX_TYPE as SEX3_2_0_,

this_.TITLE as TITLE2_0_,

this_.USERNAME as USERNAME2_0_

from

DEMO_T_AUTHOR this_

>>>> size one = 3

>>>> 删除1:N中 1的数据后,查询N的数据

Hibernate:

select

this_.ID as ID3_1_,

this_.AUTHOR_ID as AUTHOR5_3_1_,

this_.DETAILS as DETAILS3_1_,

this_.OTHER as OTHER3_1_,

this_.TYPE as TYPE3_1_,

author2_.ID as ID2_0_,

author2_.DESCRIPTION as DESCRIPT2_2_0_,

author2_.SEX_TYPE as SEX3_2_0_,

author2_.TITLE as TITLE2_0_,

author2_.USERNAME as USERNAME2_0_

from

DEMO_T_CONTACT this_

left outer join

DEMO_T_AUTHOR author2_

on this_.AUTHOR_ID=author2_.ID

>>>> size many = 8

本文介绍到此结束@Michael Sun.

原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴[ http://www.micmiu.com/ ]

本文链接地址: http://www.micmiu.com/j2ee/hibernate/hibernate-one-to-many-bidirectional-crud/

发表评论?

2 条评论。

  1. 😳 我想知道一个实体类里面可不可以有多个onetoMany

发表评论


注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">