JPA GenerationType.AUTO不考虑带自动增量的列 [英] JPA GenerationType.AUTO not considering column with auto increment

查看:2857
本文介绍了JPA GenerationType.AUTO不考虑带自动增量的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



实体的ID使用 @Id注释。 @GeneratedValue

  @Id 
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name =id,length = 4,precision = 10,nullable = false)
private Integer id;

在SQL Server中,列正确设置为Identity with Seed 增量等于1。



当我尝试持久化这个实体的实例时,Hibernate尝试查询hibernate_sequence表以获取ID值。由于我没有在我的模式中创建该表,我得到一个错误:



无法读取hi值:com.microsoft。 sqlserver.jdbc.SQLServerException:无效的对象名称'MySchema.hibernate_sequence'



如果我将生成类型更改为IDENTITY, p>

  @Id 
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name =id length = 4,precision = 10,nullable = false)
private Integer id;

我不能这样改变,因为我的应用程序将在MS SQL和ORACLE上运行,后者不支持自动递增的列。



据我所知,AUTO类型应该使用自动增量行为,如果底层数据库支持它,知道为什么不工作。



更新:



这花了我一些时间,



我正在使用具有以下行为的旧数据库:




  • MSSQL:ID生成使用表IDENTITY

  • ORACLE:ID生成使用触发器。触发器查询和更新存储所有下一个ids的自定义表。此表称为SEQ。



以下是使用一些id生成策略的结果:




  • AUTO:在MSSQL中无效,如上所述

  • IDENTITY:在MSSQL中有效,但不受Oracle支持

  • native:在MSSQL中工作,但在ORACLE中失败。它失败,因为Hibernate激活它的默认序列策略,它使用hibernate_sequences.nextval。由于这是遗留应用程序,所以来自SEQ表(上面提到的)和hibernate_sequence 的值不同步(该特定表的SEQ's值在6120,hibernate_sequences'为1,这是预期的因为它直到现在还没有使用。)



所以我需要找出一种方法来配置该实体: p>


  • 使用MSSQL身份特征
    OR

  • 使用Oracle时,不要自动设置任何值


  • 这可能会导致我在Oracle上的严重问题,当我需要插入依赖于主实体的实体(通过外键),因为Hibernate不知道哪个ID值是由外部触发器生成的。

    解决方案

    我有一个类似的问题,发现这个信息(更深入解释此处)。



    将此属性添加到我的persistence.xml文件中修复了问题:

      property name =hibernate.id.new_generator_mappingsvalue =false/> 


    I have a table with a simple int id column with Identity auto increment in SQL Server.

    The entity's Id is annotated with @Id and @GeneratedValue

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", length = 4, precision = 10, nullable = false)
    private Integer id;
    

    In SQL Server the column is properly set as Identity with Seed and Increment equals to 1.

    When I try to persist an instance of this entity, Hibernate tries to query the hibernate_sequence table to obtain the ID value. Since I haven't created that table in my schema I'm getting an error:

    could not read a hi value: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'MySchema.hibernate_sequence'

    If I change the generation type to IDENTITY everything works as expected

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", length = 4, precision = 10, nullable = false)
    private Integer id;
    

    I cannot change it this way, since my App will run both on MS SQL and ORACLE, and the latter does not support auto incremented columns.

    As far as I know the AUTO type should use the auto increment behaviour if the underlying database has support to it, so I don't know why is not working.

    UPDATE:

    It took me some time but I was able to understand exactly what is going on.

    I am working with legacy databases with the following behaviours:

    • MSSQL: id generation uses table IDENTITY
    • ORACLE: id generation uses a trigger. The trigger queries and updates a custom table where all the "next ids" are stored. This table is called SEQ.

    Here is the outcome of using some id generation strategies:

    • AUTO: does not work in MSSQL, as explained above
    • IDENTITY: works in MSSQL but is not supported by Oracle
    • "native": works in MSSQL but fails in ORACLE. It fails because Hibernate activates its default sequence strategy, which uses hibernate_sequences.nextval. Since this is a legacy application the values from the SEQ table (mentioned above) and the hibernate_sequences are not synchronized (SEQ's value for that particular table is at 6120, and hibernate_sequences' is at 1, which is expected since it was not used until now).

    So what I need to figure out is a way to configure that entity to:

    • Use MSSQL Identity feature OR
    • When using Oracle, do not automatically set any value to the ID variable and leave everything up to the pre-existing trigger

    This can cause me serious issues on Oracle when I need to insert entities that depend on the main entity (via foreign key), because Hibernate won't know which ID value was generated by the "external" trigger.

    解决方案

    I had a similar problem and found this information (deeper explained in here).

    Adding this property into my persistence.xml file fixed the issue:

    <property name="hibernate.id.new_generator_mappings" value="false" />
    

    这篇关于JPA GenerationType.AUTO不考虑带自动增量的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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