简体   繁体   中英

Exception while trying to save Hibernate entity without autogenerated Id

I have two entities, which I want to save into the database, Payment, and PaymentItem. A Payment can have more PaymentItems.

The main entity:

@Entity(name = "payment")
public class PaymentEntity {
    @Id
    private UUID id;

    @NotNull   // if I remove this... interesting things happen
    @OneToMany(cascade = CascadeType.ALL)
    @Valid
    private List<PaymentItemEntity> items;

    // getters, setters
}

The second entity:

 @Entity(name = "payment_item")
 public class PaymentItemEntity {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;

      private String code;
 }

So, I populate a Payment, with 1 PaymentItem, and try to save it. Please notice that Payment doesn't have an autogenerated id, I get the ID from somewhere else and I set it manually.

Now, what happens is that when I try to save it, Hibernate goes to the database to see if an existing entry is there along with its items (it isn't, the db is empty).

I know this because I turned on hibernate debug logging and I see:

2018-06-22 14:34:33.939 DEBUG 3915 --- [nio-8100-exec-2] o.h.e.i.DefaultMergeEventListener        : EntityCopyObserver strategy: disallow
2018-06-22 14:34:33.940 DEBUG 3915 --- [nio-8100-exec-2] org.hibernate.loader.Loader              : Loading entity: [ro.vdin.entity.PaymentEntity#60587d84-3ae6-3f1e-b63e-fa8b6658d669]

And then it fails with the exception:

javax.validation.ConstraintViolationException: Validation failed for classes [ro.vdin.entity.PaymentEntity] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
    ConstraintViolationImpl{interpolatedMessage='must not be null', propertyPath=items, rootBeanClass=class ro.vdin.entity.PaymentEntity, messageTemplate='{javax.validation.constraints.NotNull.message}'}
]
    at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:140) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:80) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.hibernate.action.internal.EntityInsertAction.preInsert(EntityInsertAction.java:205) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:82) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]

Even though my initial Payment entity has a list of 1 items (hence, it's not null).

What's interesting is that if I remove the @NotNull annotation, everything is saved to the db - the Payment, the PaymentItem, and the relations between them (Hibernate creates an association table which is properly populated).

(Basically it seems that because the id of the entity is set, hibernate tries to merge it instead of saving it to the db.)

What can I do in order to keep the @NotNull annotation on List items? And, can I do something so that it doesn't load anything from the db, and saves directly?

you haven't passed join column @joinColumn for example.

@Entity(name = "payment")
public class PaymentEntity {
@Id
@GeneratedValue(strategy = IDENTITY)
private UUID id;
@NotNull   
@OneToMany(cascade = CascadeType.ALL)
@Valid
private List<PaymentItemEntity> items;
// getters, setters
}
 //Child Class
@Entity(name = "payment_item")
public class PaymentItemEntity {
  @Id
  @GeneratedValue(strategy = IDENTITY)
  private Long id;
  private String code;
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "id")// for Example i have passed id you have pass join column name
  private PaymentEntity paymentEntity;
    // getters, setters
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM