0

我有一对一的休眠注释映射。喜欢例如。

     @Entity
     @Table(name="person")
     class Person{
       @OneToOne( mappedBy = "person", cascade = CascadeType.ALL)    
       private Passport passport;
       ......
     }

     @Entity
     @Table(name="passport")
     class Passport{
     @OneToOne
     @JoinColumn(name = "statusid")
     private Person person;
     ......
     .....
     }

这里Person是拥有者,Passport也是所有者。当我执行select操作时,person为什么休眠会触发两个查询,即 select frompersonpassport. 根据休眠文档

the owner is responsible for the association column(s) update
4

2 回答 2

0

由于您添加了 -cascad all 字段(在一对一映射中)hibernate 在此处执行的操作,因此会触发两个查询。.它带来了所有引用字段的数据。

1-假设你选择一个id=3的人;

2-那么你将在你的 dao 层中有一个具有以下语法的方法

Person finbyid(Integer personid) { ......... ........ return person; }

3-和相应的sql查询会是这样的

select * from Person where id=3;

4-休眠在这里做了一件事-

它还带来了与之相关的所有护照对象。

所以你可以做 Passport pass=person.getPassport();

现在 Passport 的传递对象将包含所有列 现在不会触发单独的查询。. 你可以直接使用 pass.getPassportid(); pass.getPassportnumber();

ETC。 。希望你的疑惑被清除

于 2012-09-03T10:16:13.033 回答
0

Concept of relationship's owner is not connected to your issue. As also excerpt you took from documentation hints, owning side is consulted when persisting relationship. It does not mean that other side of relationship is lazily loaded.

What you would need for your use case is lazy loading of one-to-one relationship. Unfortunately Hibernate does not support it. Typical hack to get around this limitation is to define relationship that is one-to-one on the one side and one-to-many (lazy) on the other side.

于 2012-09-02T17:04:48.003 回答