题
更新:例,以澄清 我要把一个例子发生了什么,只是为了澄清情况在我的春天+休眠中的应用。想象一下我有这两个实体(假定吸气,并制定者存在过)
@Entity
public class Class1(){
private Integer id;
@OneToOne
private Class2 object2;
}
@Entity
public class Class2(){
private Integer id;
@OneToOne
private Class1 object2;
}
所以在我的数据库我有两个表,以地图的每个实例的对象。假设我有数据库中保存这些物体:
- objectA(类1级):id=1,object2=objectB
- objectB(类等级2):id=2,object2=objectA
- objectC(类等级2):id=3,object2=null 这意味着一个anb B是相互关联的。假设现在我要链接objectA到objectC.这意味着我已经删除的链接objectB。
这样做我做的以下:
//I get the objectA from database
Class1 storedObjectA = myservice.find(objectAId);
//Now in storedObject1 I have the same as what it is stored as objectA
//I change only the object2 attribute of it
storedObject1.setObject2(objectC);
//I search again in the database to see objectA
Class1 reStoredObjectA = myservice.find(objectAId);
restoredObjectA现在是:
- restoredObjectA(class=1级)的id=1,object2=objectC
虽然数据库中有:
- restoredObjectA(class=1级)的id=1,object2=objectB
该方法寻找有没有搜索的数据库和我已经得到了先前搜索不过我有我的查询和第二级缓存的残疾人。该方法找到的只是道它在哪里执行的。.找到(Class1.class id) 我怎样才能让我找到第二求索的数据库?
感谢
(完整的解释)
你好,
我有一个应用程序,它使用弹簧3和休眠状态。我有一个实体命名为1级,这是存储在数据库。这一实体具有OneToOne链接到另一个实体,称为2级.2级具有另一个OneToOne链接到1级,这两个链接有独特的财产设定为真实的。
我有以下代码在我的控制器:
//I have an object called dataFromUser which is instance of Class1 and
// it is obtained via @ModelAttribute annotation from a form
Integer id = dataFromUser.getId();
//I get correctly the stored object from the database
Class1 storedData = myService.find(id); //this will call em.find of hibernate
//I set to the stored data the attribute I want to change
storedData.setObject2();
//I save again the object
myService.save(storedData);
该方法保存在myService将使用该实体的管理持续存在的数据,但在此之前,它将检查以前的协会之间的1级对象和2级对象要删除它的情况下,一个新的2级目已经设置的实例1级.因此,我要呼叫找到方法再看什么的在我的数据库
在服务:
@Transactional
public void save(Class1 object1){
Class1 objectFromDB = myDAO.find(object1.getId());
// .... update the old object and save
}
问题是,objectFromDB在服务是不同时候,我有没有myService.找到(id)在控制器。问题是,它不是寻找它在数据库中,但它是在寻找的记忆。我知道这是因为没有SQL发言时,会记录第二find()方法。我做了setObject2()上storedData对象的,我得到的正是这一点对象,当我打电话寻找方法的第二时间,并且该数据库没有包含这一点,因为它没有被保留。
我已经尝试了禁用缓存在我的配置只是为了尝试,但它仍然是发生了。我有一个拖用这种配置:
在persistence.xml 我有这样的性质(我已经设置虚假两缓存):
<properties>
<!-- Hibernate 3.5.0 - try org.hibernate.cache.* package -->
<property name="hibernate.cache.provider_class"
value="net.sf.ehcache.hibernate.SingletonEhCacheProvider" />
<property name="hibernate.cache.use_query_cache" value="false" />
<property name="hibernate.cache.use_second_level_cache" value="false" />
<property name="hibernate.generate_statistics" value="true" />
<property name="hibernate.cache.use_structured_entries" value="false" />
<property name="hibernate.format_sql" value="true" />
<!-- http://www.jroller.com/eyallupu/entry/hibernate_s_hbm2ddl_tool -->
<!-- create, create-drop, update, validate -->
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
我ehcache.xml 我已经设定为0maxElementsInMemory和overflowToDisk要假以禁止这一点。
是否有任何方法来避免这种情况?
谢谢。
解决方案 2
好吧,我找出来。问题是,休眠看起来的对象在其届会议之前搜索的数据库。所以我不得不离的对象,本届会议之前,修改它。这样做在休眠:
//being em my EntityManager instance
Session session = (Session) em.getDelegate();
session.evict(plan);
//Now the find object will not get the object wich is in session
//but the one in database
其他提示
我不确定我理解正确的,但是只是以防万一...
还有 两个方法载的对象.有一个区别,如果对象是不是在目前存的情况下:
- 一个对确定数据库,返回的实例,如果存在或null otherwise.
- 其他仅仅是创建一个代理与正确的id,而不需要立即数据库,并将处理它需要在今后的业务(可能时冲水).
你似乎是使用第二个,也许你可以尝试的第一个看看能否解决你的问题吗?