How to use existing Oracle sequence to generate id in hibernate?

后端 未结 10 1058
夕颜
夕颜 2020-12-04 19:53

I have legacy Oracle db with a sequence named PRODUCT_ID_SEQ.

Here is the mapping of Product class for which I need generate correct ids:<

相关标签:
10条回答
  • 2020-12-04 20:28

    I had the same issue while upgrading from 3.5.5 to 5.0.6.Final.

    I solved it by re-configuring mapping in the HBM file from:

        <generator class="sequence">
            <param name="sequence">PRODUCT_ID_SEQ</param>
        </generator>
    

    to:

        <generator class="org.hibernate.id.enhanced.SequenceStyleGenerator"> 
            <param name="prefer_sequence_per_entity">true</param> 
            <param name="optimizer">none</param>
            <param name="increment_size">1</param>
            <param name="sequence_name">PRODUCT_ID_SEQ</param>
        </generator>
    
    0 讨论(0)
  • 2020-12-04 20:29

    I use following on PostgreSQL and works just fine.

     @Id
     @GeneratedValue(generator = "my_gen")
     @SequenceGenerator(name = "my_gen", sequenceName = "my_seq_in_db")
     private int userId;
    
    0 讨论(0)
  • 2020-12-04 20:31

    I'm not used to use annotations, this is what I have in my *.hbm.xml:

    <id name="id" type="java.lang.Integer">
        <column name="ID_PRODUCT" />
        <generator class="sequence-identity" >
            <param name="sequence">PRODUCT_ID_SEQ</param>
        </generator>
    </id>
    

    You can easily map this to annotations. The generator sequence-identity uses auto increment with sequences.

    0 讨论(0)
  • 2020-12-04 20:39

    The answer to the original question:

    @SequenceGenerator(name="EL_SEQ", sequenceName="EL_SEQ",allocationSize=1)
    

    It is allocationSize that sets the value to increment by.

    0 讨论(0)
提交回复
热议问题