Note that there are some explanatory texts on larger screens.

plurals
  1. POCascadeType.Persist not working with primary key generator type table in JPA
    primarykey
    data
    text
    <p>I have an issue using CascadeType.Persist for my JPA/Standalone project. I have a Parent entity which has a List of Child entities that should be persisted along with the Parent and the primary key of Parent is generated using the Table(GeneratorType.TABLE) and I use a table to generate the Primary Key. </p> <p>In Parent I have :</p> <pre><code> @OneToMany(mappedBy="parent",fetch=FetchType.EAGER,cascade= {CascadeType.PERSIST,CascadeType.DETACH}) List&lt;Child&gt; children; //Getter and Setter @PostPersist public void setParentID(){ System.out.println("Inside Postpersist"); for(Child ch : this.children){ ch.setParent(this); System.out.println(ch.getParent().getParentId()); } } </code></pre> <p>In Child :</p> <pre><code> @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "Parent_ID") private Parent parent; </code></pre> <p>In the DB I have a not null set to Parent_ID column of the Parent table. When I persist a Parent entity and it has been set with list of Child entities it throws a </p> <pre><code> ConstraintViolationException: Cannot insert the value NULL into column 'Parent_ID', table 'jpaTest.Child', column does not allow nulls. </code></pre> <p>as the Parent is null for all the Child entities though it prints the ParentId set to every Child entity in the method annotated with @PostPersist </p> <p>However this does not occur and persistence of parent and child takes place when I change the primary key generation strategy from TABLE type to AUTO. How and why is this happening. </p> <p>The provider is Hibernate. And my DB server is the mssql.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload