Hibernate/jpa:许多人在左外连接而不是外键时表现得很厉害
我有两个实体:Person
和电子邮件
。它们之间的关系是许多人
。这个想法是每个家庭都会收到一封电子邮件 - 房屋中的所有人都将共享同一封电子邮件。
我也很少有家庭没有电子邮件地址。
现在,当我创建Person
时,它会使用CondectArtaintViolationException
失败,因为有时不存在家庭电子邮件。
我尝试过可选= true
和nullable = true
,但这无济于事。
我的理解是@joincolumn
正在创建外键
关系。但是我实际上需要一个左Out Join
,以便如果电子邮件为null,则将被忽略。
如何在JPA / Hibernate中实现它?
编辑:DDL是由Hibernate生成的。我正在使用以下属性: spring.jpa.generate-ddl = true spring.jpa.hibernate.use-new-id-d-generator-mappings = true spring.jpa.properties.hibernate.jdbc.time_zone = utc spring.jpa.open-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-in-open spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation = true
I have two entity: Person
and Email
. The relationship between them is ManyToOne
. The idea is Every household will have one email - and all the person in a house will share same email.
I also have a case where few households doesn't have email address.
Now when I create a Person
it fails with ConstraintViolationException
because at times the email for household is not there.
I have tried optional=true
and nullable = true
but it didn't help.
My understanding is @JoinColumn
is creating a Foreign Key
relationship. But I actually require a LEFT OUTER JOIN
so that if the email is null, it is ignored.
How can I achieve it in JPA / Hibernate?
EDIT: The DDL is generated by Hibernate. I am using following properties:
spring.jpa.generate-ddl=true
spring.jpa.hibernate.use-new-id-generator-mappings=true
spring.jpa.properties.hibernate.jdbc.time_zone=UTC
spring.jpa.open-in-view=false
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论