I have a BaseDAO that contains some generic (I think) Hibernate
compatible code. It's basically removeObject, storeObject, and
retrieveObject. I've found that Hibernate tries to do an INSERT if I
use sess.saveOrUpdate in the following method:
protected void storeObject(Object obj) throws DAOException
{
Session ses = null;
try {
ses = sessionFactory.openSession();
ses.saveOrUpdate(obj);
ses.flush();
ses.connection().commit();
} catch (Exception e) {
try {
ses.connection().rollback();
} catch (Exception ex) {
e.printStackTrace();
}
;
throw new DAOException(e);
} finally {
try {
ses.close();
} catch (Exception ex) {
ex.printStackTrace();
}
;
}
}
This causes an error, as there is already a user with this name. So I
tried changing it to just ses.update(obj), but now I'm getting the
following error:
[junit] Testcase:
testAddResume(org.appfuse.persistence.UserDAOTest): Caused an
ERROR
[junit] cirrus.hibernate.HibernateException: The given object has a
null identifier property [org.appfuse.persistenc
e.Resume]
[junit] org.appfuse.persistence.DAOException:
cirrus.hibernate.HibernateException: The given object has a null ident
ifier property [org.appfuse.persistence.Resume]
[junit] at
org.appfuse.persistence.BaseDAOHibernate.storeObject(BaseDAOHibernate.ja
va:117)
[junit] at
org.appfuse.persistence.UserDAOHibernate.addResume(UserDAOHibernate.java
:117)
[junit] at
org.appfuse.persistence.UserDAOTest.testAddResume(UserDAOTest.java:91)
All I'm trying to do is the following:
user = dao.getUser("tomcat");
user.setAddress("new address");
User savedUser = dao.saveUser(user);
assertEquals(savedUser.getAddress(), user.getAddress());
But it seems to be trying to go and persist the children - can I turn
this off? Also, I've noticed that my user's children are retrieved when
I get the user's information. Does this mean that if I create a bunch
of mappings from user to other entities, that they're all going to be
loaded when I get a user's information? This might not be desirable
since I get a user's information when they login, and if they have a
whole bunch of records associated with their name. you get the idea.
Thanks,
Matt
|