के बजाय डालने का प्रदर्शन करता है मेरे पास एक साधारण परीक्षण है, जहां मैं ऑब्जेक्ट को अपडेट करने का प्रयास कर रहा हूं, लेकिन विलय अद्यतन के बजाय एक सम्मिलन कर रहा है।जेपीए हाइबरनेट विलय अद्यतन
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/app-context.xml","classpath:spring/testdb-context.xml"})
public class UserJPATest {
@Test
public void testUpdate() throws Exception {
System.out.println("update");
User entity = ObjectManager.USER_DAO.findById(3L);
entity.setUsername("harryUpdate");
ObjectManager.USER_DAO.update(entity);
User selEntity = ObjectManager.USER_DAO.findById(3L);
Assert.assertEquals(entity.getUsername(),selEntity.getUsername());
}
}
यह मेरा अद्यतन विधि
@Override
@Transactional(propagation= Propagation.REQUIRES_NEW)
public T update(T entity) throws Exception {
try {
T merged = entityManager.merge(entity);
return merged;
} catch (Exception e) {
e.printStackTrace();
throw new Exception(e);
}
}
कोड को अद्यतन है
@Override
@Transactional(propagation= Propagation.REQUIRES_NEW)
public T update(T entity) throws Exception {
try {
T merged = null;
BaseEntity baseEntity = null;
if(entity instanceof BaseEntity){
baseEntity = (BaseEntity)entity;
merged = entityManager.find(entityClass, baseEntity.getId());
}
merged = entityManager.merge(entity);
entityManager.flush();
return merged;
} catch (Exception e) {
e.printStackTrace();
throw new Exception(e);
}
}
अब मैं प्राप्त निम्न त्रुटि जेपीए लेन-देन स्वीकार नहीं कर सकीं; नेस्टेड अपवाद javax.persistence.RollbackException: रोलबैक के रूप में चिह्नित लेनदेन केवल
अपडेट करने और हटाने के साथ सभी समस्याएं आप कैसे निर्धारित करते हैं कि यह सम्मिलित करता है? – axtavt
मैं ** वास्तव में ** ** REQUIRES_NEW ** प्रचार की तरह नहीं हूं, तो आप एक TX में इकाई बना सकते हैं और फिर एक अलग TX में "विलय या अद्यतन" कर सकते हैं, जो मूल के बारे में नहीं जानता इकाई। – Augusto
मैंने लॉग की जांच की है और यह – user373201