问题描述
创建或替换视图my_view为
选择cc.CCID ccid
sm.SMCODE smcode,
NVL(sm.smname,cc.ccname)sname
从CC cc
内部加入SM sm
在cc。 id = sm.id;
我使用 jpa 2.1
和 hibernate 4.3.7
将我的视图映射到我的实体。
我的实体类看起来像这样:
public class CCRequest implements Serializable {
private static final long serialVersionUID = 1L;
私人字符串ccId;
私人字符串smCode;
private String sName;
}
我的映射xml看起来像这样:
<?xml version =1.0encoding =UTF-8?>
< entity-mappings xmlns =http://xmlns.jcp.org/xml/ns/persistence/orm
version =2.1>
< entity class =CCRequestname =CCRequest001>
< table name =my_view/>
<属性>
< id name =ccId>
< column name =ccid/>
< / id>
< basic name =smCode>
< column name =smcode/>
< / basic>
< basic name =sName>
< column name =sname/>
< / basic>
< / attributes>
< / entity>
< / entity-mappings>
所以我用jpa正确地查询我的实体并返回所有记录。
这是问题所在,当我异步更改数据库中的数据时,令人震惊的是,我的jpa查询返回以前的记录。
我做错了什么?
我遇到的问题是,在4月的春天之前, JpaTemplate
用于处理jpa实体,我将 EntityManager
传递给 JpaTemplate
以编程方式从 EntityManagerFactory
的一个实例中执行,没有问题。
JpaTemplate
本身可以做任何事情来清除 EntityManager
并清除缓存。
当我迁移到Spring 4时,我遇到了 JpaTemplate
已被删除,因此我必须使用 EntityManager
直。
我通过 EntityManagerFactory
的实例以编程方式获得 EntityManager
的实例。
我有一个 EntityManagerProvider
类,它创建一个 EntityManager
的实例。 EntityManagerFactory
。
public class EntityManagerProvider {
public static EntityManager createEntityManager(EntityManagerFactory entityManagerFactory){
return entityManagerFactory.createEntityManager();
$ / code>
我得到这样的entityManager实例:
< bean id =entityManagerclass =com.tosan.novin.sipa.bo.da.jpa.EntityManagerFactoryProviderfactory-method = createEntityManager >
< constructor-arg index =0ref =entityManagerFactory/>
< / bean>
但是我明白如果我想要 EntityManager
管理事务和刷新的唯一方法是使用 @PersistenceContext
在我的bean中注入 EntityManager
。
@PersistenceContext
受保护的EntityManager em;
我对这种方式有点困惑,但是我的问题通过这种方法解决了。
I have an oracle view in which I query my db.
create or replace view my_view as
Select cc.CCID ccid
sm.SMCODE smcode,
NVL(sm.smname, cc.ccname) sname
From CC cc
Inner Join SM sm
On cc.id = sm.id;
I use jpa 2.1
and hibernate 4.3.7
to map my view to my entity.My entity class looks like this:
public class CCRequest implements Serializable {
private static final long serialVersionUID = 1L;
private String ccId;
private String smCode;
private String sName;
}
And my mapping xml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://xmlns.jcp.org/xml/ns/persistence/orm"
version="2.1">
<entity class="CCRequest" name="CCRequest001">
<table name="my_view"/>
<attributes>
<id name="ccId">
<column name="ccid"/>
</id>
<basic name="smCode">
<column name="smcode"/>
</basic>
<basic name="sName">
<column name="sname"/>
</basic>
</attributes>
</entity>
</entity-mappings>
So I query my entity with jpa properly and it returns all my records.Here is the problem, when I change my data in DB asynchronously, Shockingly my jpa query returns previous records.Have I done something wrong?
The problem I was facing was that in spring before 4, which we had JpaTemplate
for working with jpa entities, I passed EntityManager
to the instances of JpaTemplate
programmatically from an instance of EntityManagerFactory
with no problem. JpaTemplate
itself would do any thing for flushing EntityManager
and clearing cache.When I migrate to spring 4 I faced that JpaTemplate
has been dropped so I have to work with EntityManager
directly.
I get instance of EntityManager
programmatically from an instance of EntityManagerFactory
.
I have a EntityManagerProvider
class which create an instance of EntityManager
from an instance of EntityManagerFactory
.
public class EntityManagerProvider {
public static EntityManager createEntityManager(EntityManagerFactory entityManagerFactory) {
return entityManagerFactory.createEntityManager();
}
}
I get entityManager instance like this:
<bean id="entityManager" class="com.tosan.novin.sipa.bo.da.jpa.EntityManagerFactoryProvider" factory-method="createEntityManager">
<constructor-arg index="0" ref="entityManagerFactory"/>
</bean>
But I understand that if I want EntityManager
to manage transactions and flushing the only way is use of @PersistenceContext
for injecting EntityManager
into my beans.
@PersistenceContext
protected EntityManager em;
I am a little confused yet with this manner but my problem solved with this approach.
这篇关于做Jpa& Hibernate加载数据在数据库中异步变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!