Java (JPA) 和表列默认值

huangapple go评论68阅读模式
英文:

Java (JPA) and table column defaults

问题

I would very much appreciate your advice on the JPA. We have a database (PostgreSQL engine), and for tables it happens that a column has its DEFAULT. The database is connected to a Java backend (with JPA and Hibernate). What is the correct way to force JPA not to try to insert IDs for the primary keys that are generated by the table column DEFAULT?

我非常感激您对JPA的建议。我们有一个数据库(PostgreSQL引擎),在表中,某列具有其默认值。数据库连接到Java后端(使用JPA和Hibernate)。如何正确地让JPA不尝试插入由表列的默认值生成的主键ID?

I've tried the settings below, but unfortunately I'm having no luck:

我尝试了下面的设置,但不幸的是没有成功:

Java (JPA) 和表列默认值

Thank you in advance for any advice. 😊

非常感谢您提前提供任何建议。 😊

I tried to search for a solution on the internet and in the documentation, but unfortunately, without success.

我尝试在互联网和文档中搜索解决方案,但不幸的是没有成功。

英文:

I would very much appreciate your advice on the JPA. We have a database (PostgreSQL engine), and for tables it happens that a column has its DEFAULT. The database is connected to a Java backend (with JPA and Hibernate). What is the correct way to force JPA not to try to insert IDs for the primary keys that are generated by the table column DEFAULT?

I've tried the settings below, but unfortunately I'm having no luck:

Java (JPA) 和表列默认值

Thank you in advance for any advice 🙂

I tried to search for a solution on the internet and in the documentation, but unfortunately, without success.

答案1

得分: 0

以下是翻译好的部分:

假设这是 Hibernate 6(您没有说明),您要查找的注释是 @Generated,并且它在此处有文档:

<https://docs.jboss.org/hibernate/orm/6.2/javadocs/org/hibernate/annotations/Generated.html>

我还在这里发推文示例,几乎与您的示例完全相同:

<https://twitter.com/1ovthafew/status/1645796021585457153>

因此,类似于以下内容的代码应该可以工作:

    
@Entity
public class Entity {
        
    @Generated @Id
    @ColumnDefault("gen_random_uuid()")
    @Column(name = "id_assignment")
    UUID idAssignment;
}
英文:

Assuming that this is Hibernate 6 (you have not stated), the annotation you're looking for is @Generated, and it's documented here:

<https://docs.jboss.org/hibernate/orm/6.2/javadocs/org/hibernate/annotations/Generated.html>

I also tweeted an example almost exactly like yours here:

<https://twitter.com/1ovthafew/status/1645796021585457153>

So, something like:

    
@Entity
public class Entity {
        
    @Generated @Id
    @ColumnDefault(&quot;gen_random_uuid()&quot;)
    @Column(name = &quot;id_assignment&quot;)
    UUID idAssignment;
}

should work.

答案2

得分: 0

你应该使用注解@GenerateValue来告诉JPA不要使用默认的生成策略。

文档在这里:https://www.baeldung.com/hibernate-identifiers

英文:

You should use the annotation @GenerateValue to let JPA not use the default generate strategy.

The documentation is here: https://www.baeldung.com/hibernate-identifiers

huangapple
  • 本文由 发表于 2023年5月17日 16:00:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269793.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定