I have trouble regarding the insertable of OneToMany.
I'm working with a legacy system which was built on Spring and hibernate4 There is OneToMany/ManyToOne relationship with no one is the "owner"(By defining JoinColumn and insertable/updatable as false on both side).
{code:java} @Entity public static class One{
@Id @GeneratedValue public Integer id;
@OneToMany( cascade = {CascadeType.ALL}, targetEntity = Many.class ) @JoinColumn(name = "one_id", insertable = false, nullable = false, updatable = false) public List<Many> manyList; } {code}
{code:java} @Entity public static class Many {
@Id @GeneratedValue public Integer id;
@ManyToOne(cascade = {CascadeType.REFRESH}, targetEntity = One.class ) @JoinColumn(name = "one_id", nullable = false, insertable = false, updatable = false) public One one; } {code}
although I don't think it's the correct usage, it works fine when try to persist the "Many" by persisting the "One".
But when the legacy system is extracted and imported as a library in a SprintBoot+Hibernate5 application, then the magic above stop working, throwing *java.sql.SQLException "Foreignkey doesn't have a default value" exception* from MySQL.
The reason is the SQL generated differs between hibernate4/hibernate5.
Hibernate4: {code:sql} insert into JPAUnitTestCase$Many (id, one_id) values (null, ?) {code} Hibernate5: {code:sql} insert into JPAUnitTestCase$Many (id) values (?) {code}
I attached testcases in the attachment for example.
And as per my investigation, the following commit change the behavior on the column insertable initialization.
https://github.com/hibernate/hibernate-orm/commit/c893577efceff237f84d7200e1ef1a2895c95639 |
|