简体   繁体   中英

One to One Relationship With Shared not Working at Execution time

Im using EclipseLink(JPA 2.0) with MySql and I'm having a problem with something in the model. I have 2 entities: Delincuente, PerfilFisico. They share the Delincuente's Pk.

The DB is working correctly but when i insert a Delincuente and then the Perfil Fisico, i tried to show the PerfilFisico of that Delincuente (Using Delincuente.PerfilFisico.), it was just empty. I checked the tables at the DB and everything was ok.

If i run the system again (I'm developing in Netbeans 7.2) and i consult the Delincuente that i just registered on the DB, it shows all the fields and everything is perfect!!

There is the code:

Delincuente.java

@OneToOne(cascade = CascadeType.ALL, mappedBy = "delincuente")
private PerfilFisico perfilFisico;  

PerfilFisico.java

@JoinColumn(name = "PK_IdDelincuente", referencedColumnName = "PK_Identificacion", insertable = false, updatable = false)
@OneToOne(optional = false)
private Delincuente delincuente;

What is wrong, i've been reading for days and i can't figure out which the problem is. Any tips?

As i said it works, but when you are running it and you register a new Delincuente, you have to re-run the System to succesfully complete the consult and see the PerfilFisico

JPA EntityManagers have a first level cache for managed entities, and providers like EclipseLink have a second level cache. So if you don't maintain both sides of your bidirectional mappings so they are out of sync with the db, it won't show unless you force a refresh, clear the cache or restart. The app must maintain bidirectional relationships, JPA won't do it for you, as they are meant to be pojos.

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