Hi i am try to insert data using hibernate 4.x and spring 4.x , here is my code
@RunWith(SpringJUnit4ClassRunner.class)
//ApplicationContext will be loaded from "classpath:/app-config.xml"
@ContextConfiguration("/applicationContext.xml")
@ActiveProfiles("dev")
@Transactional
public class UserDAOTest {
@Autowired
private UserDAO userDAOImpl;
@Rollback(false)
@Test
public void testSave()
{
System.out.println(userDAOImpl);
User user =new User();
//User us=userDAOImpl.get(1);
User us=userDAOImpl.save(new User("anil", "[email protected]", "9505485444", "56482", null, null));
//userDAOImpl.saveOrUpdate(new User("anil", "[email protected]", "9505485444", "56482", null, null));
System.out.println(us.getUserId());
}
}
here is save method
public E save(E object) {
return (E)getCurrentSession().merge(object);
}
and gessionmethod is
public Session getCurrentSession() {
return sessionFactory.openSession();
}
it working fine if i use saveOrUpdate instead of merge , why it fails for merge?
But if i change my save method like as follows
public E save(E object) {
// return (E)getCurrentSession().saveOrUpdate(object);.merge(object);
Session session = getCurrentSession();
session.saveOrUpdate(object);
return (E) session.merge(object);
}
that have syn data with database