简体   繁体   中英

JPA query ends up with null values

Long story short:

  • Recipe contains

     @OneToMany(cascade = {CascadeType.MERGE, CascadeType.REMOVE}, mappedBy = "recipe") private Set<RecipeRow> rows = new HashSet<>(); 
  • Row contains:

     @ManyToOne(cascade = CascadeType.REFRESH)` @JoinColumn(name = "recipe_id") private Recipe recipe;` 
  • The Row extends a mapped superclass with a tree structure (ie it contains a mapped parent field which is of same type)

Right before the entity manager merge (of the recipe) all rows (and their information) are there. When it merges, it throws this (it's with regards to the row!!!):

Caused by: org.postgresql.util.PSQLException: ERROR: null value in column "name" violates
    not-null constraint Detail: Failing row contains (11, 2015-08-22 13:53:33.276, 
    304170813849900523, 0, null, null, null, null, null, null, 1000, null, 
    null, null, null, null, null, null, null, null).

which is pretty much impossible because all mandatory fields are there.

Also, the ID sequence (of the row table) is increased, but no rows are persisted in the DB (postgres).

Am I missing something here? Is my question too broad?


  • The JPA UID in the error message DOES NOT correspond to either of my recipe rows which are being saved. Where does this ghost entity come from?
  • All mandatory fields are filled in the rows before saving!

Alright, so this was a beginner's mistake, and also a stupid one.

The rows have a tree structure, right? And the saved collection contained ALL of them, as flat tree (ie list).

The problem is that the top rows had the abstract root as parent (which isn't transient), and the abstract root was trying to map itself.

Of course I didn't notice this in the debugging, because I was only looking at the rows, and at their mandatory fields, but not at the parent (which isn't mandatory).

Yep...

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