Hibernate/jpa:许多人在左外连接而不是外键时表现得很厉害

发布于 2025-02-13 18:02:01 字数 946 浏览 1 评论 0原文

我有两个实体:Person电子邮件。它们之间的关系是许多人。这个想法是每个家庭都会收到一封电子邮件 - 房屋中的所有人都将共享同一封电子邮件。

我也很少有家庭没有电子邮件地址。

现在,当我创建Person时,它会使用CondectArtaintViolationException失败,因为有时不存在家庭电子邮件。

我尝试过可选= truenullable = 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文