I’ve got entity, which has @EmbeddedId. In the id class there are a few fields, and one of them is Long id with Generation type sequence. Problem apperas when I’m trying to insert instance of this entity, without setting id in embeeded id (which should be inserted here from sequence).
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
When id in this embeddedId is set to any value then insert is successfull.
What can I do to make it work?
Edit: code
@Entity
@Table(name = "TestIds")
public class TestIds implements Serializable {
@EmbeddedId
private TestId testId;
public TestIds(TestId testId) {
this.setTestId(testId);
}
public TestId getTestId() {
return testId;
}
public void setTestId(TestId testId) {
this.testId = testId;
}
}
@Embeddable
public class TestId implements Serializable {
private static final long serialVersionUID = 1L;
@GeneratedValue(strategy=GenerationType.AUTO, generator="my_entity_seq_gen")
@SequenceGenerator(name="my_entity_seq_gen", sequenceName="MY_ENTITY_SEQ")
@Column(name = "column_name")
private Long id;
public TestId() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
I’m executing
TestIds a = new TestIds(new TestId());
Session session = sessionFactory.getCurrentSession();
session.save(a);
session.flush();
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
Caused by:
java.sql.BatchUpdateException: Batch entry 0 insert into TestIds (column_name) values (NULL) was aborted
It seems that it is impossible to use @GeneratedValue in @EmbeddedId