• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Reading old data - Hibernate First Level Cache

Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello , I think this is a simple question

I have a entity A and inside this entity i have a entity B

Entity A

String Name,

One to Many
Cascade Persist
Entity B

I start the process with a valid entity A, with entity B as null

First I use a find method to get entity A from database

Them I save entity B in Database

Ps. I am not able to save entity A at this point, ok ?

The data is updated in the database, entity A has entity B as a foreign key

Them I run another find in entity A

and the data is old, entity B still null

I´ve tried cascade refresh, isolation and propagation , and nothing

I am using spring boot, jpa repository, java 11

I know if i find for entity B, and navigate to entity A is going to work

But is there any sollution

Thanks in Advance
Nelson Gomes
Saloon Keeper
Posts: 22503
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would have been much easier

to understand

what you are saying

if you hadn't put all those

extra blank lines


First, the version of Java and whether or not you're using Spring Boot do not matter. What you are calling "jpa repository" sounds like Spring JPA, so I'm going to answer according to that.

When you define a one-to-many relationship using JPA (whether it's with Spring or not) then you should also define a many-to-one relationship on your Entity B to point back to its parent. If you don't do this, then not everything may persist properly to the database. Let me use parent-child terms here, since whether A and B are actually parent/child or just organized like they are doesn't matter and so I can use simpler terms.

When you create a new A record and one or more B records, you should not be thinking in terms of SQL. JPA is handling that. Think in Java terms. You create A, and add each B to its child collection property (One-to-Many). As you do so, point the new B back to A using its parent property (Many-to-One). When you are ready to persist to the database, start a transaction, persist each B record, and then persist the A record. Then end the transaction. Your database is now properly updated.

Beyond that, there is one additional thing to be aware of. In JPA, persisting an update to a database may return a new copy of the object being persisted. After you persist, you must use this new object. The original A and B objects are no longer usable. And, in fact, if you attempt to use them, you'll see problems like the one you described.

We have a forum dedicated just for ORM systems like JPA and Hibernate, so I'm moving this thread over there. You should always try to ask questions in the forum that best matches the type of question you are asking. That's where the people who can give the answers are.
Quick! Before anybody notices! Cover it up with this tiny ad:
the value of filler advertising in 2020
    Bookmark Topic Watch Topic
  • New Topic