I have this two classes and I'm trying to perfom a cascade save-update by using hibernate and JPA annotations, but when I try to save my Order object I'm getting an exception described as following:
Here is the Order class
@Entity
@Table(name="orders")
public class Order {
@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
private Integer idOc;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL ,mappedBy = "order")
@Valid
private List<OrderLine> lines = new ArrayList<OrderLine>(Arrays.asList(new OrderLine()));
// getters and setters
}
And here is my OrderLine class
@Entity
@Table(name="orderLines ")
public class OrderLine {
@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
private Integer idOl;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "idOc")
private Order order;
// getters and setters
}
Here are the exceptions I'm getting:
1 - org.hibernate.exception.ConstraintViolationException: could not execute statement.
2 - Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'idOl' cannot be null.
The idOl is supose to be assigned by hibernate generator but somehow it is not working. Any idea?
Update: On console I can see Hibernate is doing the insert on the parent table but when it tries to do the insert on the child table the exception is thrown. I can see Hibernate is not assinging the autoincrement value for every element contained on the List<>, but I can't figure it out why.