Hibernate JPA序列(非Id) [英] Hibernate JPA Sequence (non-Id)
问题描述
是否可以为不是标识符/不是组合标识符的一部分的某个列使用DB序列?
我使用hibernate作为jpa提供程序,并且我有一个表有一些生成值的列(使用序列),尽管它们不是标识符。
我想要的是使用序列为实体创建新值,其中序列的列是 NOT (部分)主键:
@Entity
@Table(name =MyTable)
public class MyEntity {
// ...
@Id // ... etc
public Long getId(){
return id;
}
// note注意@Id here!但这不起作用...
@GeneratedValue(strategy = GenerationType.AUTO,generator =myGen)
@SequenceGenerator(name =myGen,sequenceName =MY_SEQUENCE)
@Column(name =SEQ_VAL,unique = false,nullable = false,insertable = true,updatable = true)
public Long getMySequencedValue(){
return myVal;
}
}
然后当我这样做时:
em.persist(new MyEntity());
将生成id,但是 mySequenceVal
属性也将由我的JPA提供程序生成。
为了清楚起见:我希望 Hibernate 生成 mySequencedValue
属性。我知道Hibernate可以处理数据库生成的值,但我不想使用触发器或除Hibernate之外的其他任何东西来为我的属性生成值。如果Hibernate可以为主键生成值,为什么它不能为一个简单的属性生成?
寻找这个答案问题,我偶然发现了这个链接
看起来Hibernate / JPA不能为你的非id属性自动创建一个值。 @GeneratedValue
注释仅与 @Id
一起用于创建自动编号。
@GeneratedValue
注解只是告诉Hibernate数据库本身正在生成这个值。
该论坛建议的解决方案(或解决方法)是使用生成的ID创建一个单独的实体,如下所示:
@Entity
公共类GeneralSequenceNumber {
@Id
@GeneratedValue(...)
私人长号码;
}
@Entity
public class MyEntity {
@Id ..
private Long id;
@OneToOne(...)
private GeneralSequnceNumber myVal;
}
Is it possible to use a DB sequence for some column that is not the identifier/is not part of a composite identifier?
I'm using hibernate as jpa provider, and I have a table that has some columns that are generated values (using a sequence), although they are not part of the identifier.
What I want is to use a sequence to create a new value for an entity, where the column for the sequence is NOT (part of) the primary key:
@Entity
@Table(name = "MyTable")
public class MyEntity {
//...
@Id //... etc
public Long getId() {
return id;
}
//note NO @Id here! but this doesn't work...
@GeneratedValue(strategy = GenerationType.AUTO, generator = "myGen")
@SequenceGenerator(name = "myGen", sequenceName = "MY_SEQUENCE")
@Column(name = "SEQ_VAL", unique = false, nullable = false, insertable = true, updatable = true)
public Long getMySequencedValue(){
return myVal;
}
}
Then when I do this:
em.persist(new MyEntity());
the id will be generated, but the mySequenceVal
property will be also generated by my JPA provider.
Just to make things clear: I want Hibernate to generate the value for the mySequencedValue
property. I know Hibernate can handle database-generated values, but I don't want to use a trigger or any other thing other than Hibernate itself to generate the value for my property. If Hibernate can generate values for primary keys, why can't it generate for a simple property?
Looking for answers to this problem, I stumbled upon this link
It seems that Hibernate/JPA isn't able to automatically create a value for your non-id-properties. The @GeneratedValue
annotation is only used in conjunction with @Id
to create auto-numbers.
The @GeneratedValue
annotation just tells Hibernate that the database is generating this value itself.
The solution (or work-around) suggested in that forum is to create a separate entity with a generated Id, something like this:
@Entity public class GeneralSequenceNumber { @Id @GeneratedValue(...) private Long number; } @Entity public class MyEntity { @Id .. private Long id; @OneToOne(...) private GeneralSequnceNumber myVal; }
这篇关于Hibernate JPA序列(非Id)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!