Hibernate JPA IdentifierGenerationException: null id generated for class with @embeddedid

后端 未结 3 1670
迷失自我
迷失自我 2021-01-12 19:21

I am having trouble mapping my database domain model to the program entities in one case where the entity is essentially a join table (a period) which combines two other ent

3条回答
  •  别那么骄傲
    2021-01-12 19:35

    I was able to create the following mapping for my case (scala code) and could totally throw away the @Embeddable class:

    @Entity
    @Table(name = "payment_order_item", schema = "pg")
    @IdClass(classOf[PaymentOrderItem])
    final class PaymentOrderItem extends Serializable{
    
      @Id
      @ManyToOne
      @JoinColumn(name = "order_item_id", referencedColumnName = "id")
      var orderItem: OrderItem = _
    
      @Id
      @ManyToOne
      @JoinColumn(name = "payment_id", referencedColumnName = "id")
      var payment: Payment = _
    }
    

    So the following should work for you then

    @Entity
    @Table(name = "period")
    @IdClass(Period.class)
    public class Period extends Serializable{
    
        @Id
        @ManyToOne(fetch = FetchType.EAGER)
        @JoinColumn(name = "day_idday", referencedColumnName = "id", nullable = false)
        private Day day;
    
        @Id
        @ManyToOne(fetch = FetchType.EAGER)
        @JoinColumn(name = "timeslot_idtimeslot", referencedColumnName = "id", nullable = false)
        private Timeslot timeslot;
    
        //constructors, getters, setters, hashcode, and equals
    }
    

提交回复
热议问题