简体   繁体   English

与共享的一对一关系在执行时不起作用

[英]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. 我在MySql中使用EclipseLink(JPA 2.0),但模型中存在某些问题。 I have 2 entities: Delincuente, PerfilFisico. 我有2个实体:Delincuente,PerfilFisico。 They share the Delincuente's Pk. 他们共享Delincuente的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. 数据库正常工作,但是当我插入一个Delincuente然后再插入Perfil Fisico时,我试图显示该Delincuente的PerfilFisico(使用Delincuente.PerfilFisico。),它只是空的。 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!! 如果我再次运行该系统(我在Netbeans 7.2中进行开发)并且咨询了我刚刚在DB上注册的Delincuente,它将显示所有字段,并且一切都完美!

There is the code: 有代码:

Delincuente.java Delincuente.java

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

PerfilFisico.java 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 正如我所说的那样,但是当您运行它并注册一个新的Delincuente时,您必须重新运行系统才能成功完成咨询并查看PerfilFisico

JPA EntityManagers have a first level cache for managed entities, and providers like EclipseLink have a second level cache. JPA EntityManager具有用于托管实体的第一级缓存,而诸如EclipseLink的提供程序具有第二级缓存。 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. 该应用程序必须保持双向关系,JPA不会为您做到,因为它们本来就是pojos。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM