Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » One to One Relationship With Shared PK not Working at Execution time(JPA, One To One, MySql)
One to One Relationship With Shared PK not Working at Execution time [message #1046723] Mon, 22 April 2013 09:23 Go to next message
Jose Garcia is currently offline Jose GarciaFriend
Messages: 2
Registered: April 2013
Junior Member


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
Re: One to One Relationship With Shared PK not Working at Execution time [message #1047051 is a reply to message #1046723] Mon, 22 April 2013 18:12 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
This is asked on http://stackoverflow.com/questions/16143405/one-to-one-relationship-with-shared-not-working-at-execution-time as well.

The problem is likely due to not maintaining both sides of the bidirectional relationship. When setting the PerfilFisico.delincuente relationship, the application must also set the Delincuente.perfilFisico backpointer to keep the entities in synch with each other and the database. Not doing so runs the risk of having the cached entities out of synch with what is in the database - depending on which side is set, the database might not be updated at all.

Caching examples are provided here http://wiki.eclipse.org/EclipseLink/Examples/JPA/Caching but it is usually more performant to maintain relationships, or just not map the back pointer and query for it when needed rather than to force a refresh or invalidate the entity in the cache.

Best Regards,
Chris
Re: One to One Relationship With Shared PK not Working at Execution time [message #1047501 is a reply to message #1047051] Tue, 23 April 2013 09:44 Go to previous message
Jose Garcia is currently offline Jose GarciaFriend
Messages: 2
Registered: April 2013
Junior Member
Thank you!!
I just fixed it and u were right!

I wasnt mapping or doing the bidirectional assign of the entities.
Previous Topic:EclipseLink composite persistence unit class declaration
Next Topic:Query Cache in 2.4.1 Help
Goto Forum:
  


Current Time: Fri Apr 19 23:51:15 GMT 2024

Powered by FUDForum. Page generated in 0.03203 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top