【问题标题】:Hibernate Sequence generation in ms sql serverms sql server 中的 Hibernate 序列生成
【发布时间】:2023-02-18 00:18:09
【问题描述】:

我们正在使用@sequencegenerator 注释在 mssql 数据库中生成序列,但是当我们执行序列生成为表而不是序列时,我们使用了 2012Dialect 但我们仍然面临同样的问题并且应用程序抛出异常无效对象名称 - 序列名称 -。请帮忙解决

【问题讨论】:

标签: java spring-boot spring-data-jpa


【解决方案1】:

Have you used GeneratedValue annotation to specify that the id generation value witll be from a database sequence?

example:

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_name")
@SequenceGenerator(name = "seq_name", sequenceName = "seq_name_in_database", allocationSize = 1)
@Column(name = "id")
private Long id;
    【解决方案2】:

    It seems that Hibernate (v 5.6.3) is using its own tables to manage sequences in sql server, you must create the table with the same name as your sequence with one column named next_val then insert a line with the init value. Hibernate use the query to select :

    select next_val as id_val from sequence_name with (updlock,rowlock)
    

    then increment the next_val :

    update sequence_utilisateur set next_val= ? where next_val=?
    

    Also don't define the column for wich you use the generated value as IDENTITY.

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-08
      • 1970-01-01
      • 1970-01-01
      • 2011-04-04
      • 2011-04-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多