• 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 ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Reading old data - Hibernate First Level Cache

 
Greenhorn
Posts: 6
  • 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: 21248
137
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

there.


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.
 
Time flies like an arrow. Fruit flies like a banana. Steve flies like a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!