HiLo generator strategy not working

后端 未结 7 486
伪装坚强ぢ
伪装坚强ぢ 2021-02-04 09:02

I am new to hibernate. What I am trying to do is use @CollectionIdto generate an identifier for my Address class. I have used Collection interface for

相关标签:
7条回答
  • 2021-02-04 09:29

    You must choose from one of the Hi/Lo strategy :

    • SequenceHiLoGenerator : to back the Hi with a Sequence
    • MultipleHiLoPerTableGenerator : to back the Hi with a Table

    To be as close as your tutorial as possible, I would simply change "hilo" to "seqhilo" in your code.

    0 讨论(0)
  • 2021-02-04 09:32

    Hilo is not supported anymore, this should work

    @GenericGenerator(name="sequence-gen",strategy="sequence")
    
    0 讨论(0)
  • 2021-02-04 09:42

    FYI, MYSQL supports sequence strategy however table name should not contain hyphen '-' (which is name provided in @GenericGenerator(name) could cause DDL exception while creating sequence table in mysql.

    0 讨论(0)
  • 2021-02-04 09:45

    If we are using mysql it would be better to use the @GenericGenerator of increment strategy.

    1. sequence - This sort of strategy supports by Oracle, Postgresql.
    2. increment - This sort of strategy supports by MySql.

      @ElementCollection
      @JoinTable(name="USER_ADDRESS", joinColumns=@JoinColumn(name="USER_ID"))
      @GenericGenerator(name = "increment-gen", strategy = "increment")
      @CollectionId(columns = { @Column(name="ADDRESS_ID") }, generator = "increment-gen", type = @Type(type="long"))
      private Collection<Address> listOfAddress = new ArrayList<>();
      

    When I have used the sequence strategy with MySql I came across an issue where my ADDRESS_ID is not getting incremented properly.

    0 讨论(0)
  • 2021-02-04 09:47

    This is what works for apps on PostgreSQL. Though I have not tested it, it should work for all DBs. Note that increment-gen is used not sequence.

    @ElementCollection
        @JoinTable( name = "user_address", joinColumns = @JoinColumn( name = "user_id"))
        @GenericGenerator(name="increment-gen",strategy="increment")
        @CollectionId( columns = { @Column( name ="address_id") }, generator ="increment-gen", type =@Type( type ="long"))
        private Collection<Address> listOfAddresses = new ArrayList<Address>();
    
    0 讨论(0)
  • 2021-02-04 09:52

    I would recommend you to try any of the below 2 solutions and it will fix your issue. it is as per the specification provided in Hibernate 5.2.X.

    Source Of Info -https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html

    Solution 1 -

     @GenericGenerator(name = "product_generator",strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator")
     @CollectionId(columns = { @Column(name="ADDRESS_ID") }, generator = "product_generator", type = @Type(type="long"))*
    

    Solution 2 -

    *@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "product_generator")
     @CollectionId(columns = { @Column(name="ADDRESS_ID") }, generator = "product_generator", type = @Type(type="long"))*
    

    Let me know if it helps you.

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