Hibernate JPA 序列(非 Id) [英] Hibernate JPA Sequence (non-Id)

查看:33
本文介绍了Hibernate JPA 序列(非 Id)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以对不是标识符/不是复合标识符的一部分的某些列使用数据库序列?

我使用 hibernate 作为 jpa 提供程序,我有一个表,其中有一些列是生成的值(使用序列),尽管它们不是标识符的一部分.

我想要的是使用序列为实体创建一个新值,其中序列的列是NOT(部分)主键:

@Entity@Table(name = "MyTable")公共类 MyEntity {//...@Id//... 等等公共长 getId() {返回标识;}//注意这里没有@Id!但这不起作用......@GeneratedValue(strategy = GenerationType.AUTO, generator = "myGen")@SequenceGenerator(name = "myGen", sequenceName = "MY_SEQUENCE")@Column(name = "SEQ_VAL", unique = false, nullable = false, insertable = true, updatable = true)公共长 getMySequencedValue(){返回 myVal;}}

然后当我这样做时:

em.persist(new MyEntity());

将生成 id,但我的 JPA 提供程序也会生成 mySequenceVal 属性.

只是为了说明清楚:我希望 Hibernate 生成 mySequencedValue 属性的值.我知道 Hibernate 可以处理数据库生成的值,但我不想使用触发器或除 Hibernate 本身以外的任何其他东西来为我的属性生成值.如果 Hibernate 可以为主键生成值,为什么不能为简单的属性生成值?

解决方案

在寻找这个问题的答案时,偶然发现了 此链接

Hibernate/JPA 似乎无法自动为您的非 id 属性创建值.@GeneratedValue 注释仅与 @Id 结合使用以创建自动编号.

@GeneratedValue 注释只是告诉 Hibernate 数据库正在自己生成这个值.

该论坛中建议的解决方案(或变通方法)是使用生成的 ID 创建一个单独的实体,如下所示:

<前>@实体公共类 GeneralSequenceNumber {@ID@GeneratedValue(...)私人长号;}@实体公共类 MyEntity {@ID ..私人长ID;@OneToOne(...)私人 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆