I am calling this code from a class which extends org.springframework....HibernateDaoSupport.
dom4jSession.save() & saveOrUpdate() fail silently. They don't do anyting. dom4jSession.persist() gives the detached entity error below.
Any help would be greatly appreciated!!
Shane
ebersoless@yahoo.com
Hibernate 3.1:
Mapping documents:
<hibernate-mapping
>
<class
name="mil.ditv.model.User"
table="users"
node="user"
>
<meta attribute="class-description">
@author Shane Ebersole
</meta>
<meta attribute="extends">BaseObject</meta>
<meta attribute="implements-equals">true</meta>
<meta attribute="use-in-tostring">true</meta>
<meta attribute="use-in-equals">true</meta>
<id
name="username"
column="username"
type="java.lang.String"
length="50"
unsaved-value="version"
node="@username"
>
<meta attribute="use-in-tostring"></meta>
<meta attribute="use-in-equals"></meta>
<generator class="assigned">
</generator>
</id>
<version
name="version"
column="version"
type="java.lang.Integer"
node="@version"
>
<meta attribute="use-in-tostring"></meta>
<meta attribute="use-in-equals">false</meta>
</version>
<property
name="password"
type="java.lang.String"
update="true"
insert="true"
column="password"
not-null="true"
node="password"
/>
<property
name="email"
type="java.lang.String"
update="true"
insert="true"
column="user_email"
not-null="true"
unique="true"
node="email"
/>
<property
name="unit"
type="java.lang.String"
update="true"
insert="true"
column="user_unit"
length="50"
node="unit"
/>
<property
name="firstName"
type="java.lang.String"
update="true"
insert="true"
column="user_firstname"
length="50"
node="firstName"
/>
<property
name="lastName"
type="java.lang.String"
update="true"
insert="true"
column="user_lastname"
length="50"
node="lastName"
/>
<property
name="rank"
type="java.lang.String"
update="true"
insert="true"
column="user_rank"
length="50"
node="rank"
/>
<property
name="phone"
type="java.lang.String"
update="true"
insert="true"
column="user_dutyphone"
length="50"
node="phone"
/>
<property
name="lastLogin"
type="java.util.Date"
update="true"
insert="true"
column="dtg_lastlogin"
node="lastLogin"
/>
<property
name="passwordHint"
type="java.lang.String"
update="true"
insert="true"
column="password_hint"
not-null="false"
node="passwordHint"
/>
<property
name="enabled"
type="java.lang.Boolean"
update="true"
insert="true"
column="userstatus"
node="@enabled"
/>
<set
name="roles"
table="user_role"
lazy="false"
cascade="none"
sort="unsorted"
>
<key
column="username"
>
</key>
<many-to-many
class="mil.ditv.model.Role"
column="role_name"
outer-join="auto"
node="role"
/>
</set>
</class>
</hibernate-mapping>
By the way the override for <meta attribute="use-in-equals">false</meta> doesn't work!!
Code between sessionFactory.openSession() and session.close():
public User saveXML(final Element user)
{
return (User) getHibernateTemplate().execute(new HibernateCallback()
{
public Object doInHibernate(Session session)
{
log.debug("INSIDE saveXML = " + user.asXML());
Session dom4jSession = session.getSession(EntityMode.DOM4J);
//dom4jSession.saveOrUpdate(User.class.getName(), user); // FAILS Silently
dom4jSession.persists(User.class.getName(), user); // FAILS NOISILY
return (User)session.get(User.class,user.attributeValue("username"));
}
});
}
Full stack trace of any exception that occurs:
[junit] [ditv] DEBUG [main] UserDAOHibernate.doInHibernate(98) | INSIDE save
XML = <user username="adasdfasdmin" version="1" enabled="true">
[junit] <password>536c0b339345616c1b33caf454454d8b8a190d6c</password>
[junit] <email>admin@local.mil</email>
[junit] <unit>admin_unit</unit>
[junit] <firstName>DITV</firstName>
[junit] <lastName>Administrator</lastName>
[junit] <rank>CAPTIAN</rank>
[junit] <phone>822-1234</phone>
[junit] <passwordHint>A male kitty.</passwordHint>
[junit] <roles>
[junit] <role name="admin" version="1">
[junit] <description>Administrator role (can edit Users)</description>
[junit] <displayKey>role.admin</displayKey>
[junit] </role>
[junit] </roles>
[junit] </user>
[junit] ------------- ---------------- ---------------
[junit] Testcase: testSaveXML(mil.ditv.dao.UserDAOTest): Caused an ERROR
[junit] detached entity passed to persist: mil.ditv.model.User; nested excep
tion is org.hibernate.PersistentObjectException: detached entity passed to persi
st: mil.ditv.model.User
[junit] org.springframework.dao.InvalidDataAccessApiUsageException: detached
entity passed to persist: mil.ditv.model.User; nested exception is org.hibernat
e.PersistentObjectException: detached entity passed to persist: mil.ditv.model.U
ser
[junit] org.hibernate.PersistentObjectException: detached entity passed to p
ersist: mil.ditv.model.User
[junit] at org.hibernate.event.def.DefaultPersistEventListener.onPersist
(DefaultPersistEventListener.java:79)
[junit] at org.hibernate.event.def.DefaultPersistEventListener.onPersist
(DefaultPersistEventListener.java:38)
[junit] at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:5
90)
[junit] at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:568)
[junit] at mil.ditv.dao.hibernate.UserDAOHibernate$2.doInHibernate(UserD
AOHibernate.java:101)
[junit] at org.springframework.orm.hibernate3.HibernateTemplate.execute(
HibernateTemplate.java:312)
[junit] at org.springframework.orm.hibernate3.HibernateTemplate.execute(
HibernateTemplate.java:288)
[junit] at mil.ditv.dao.hibernate.UserDAOHibernate.saveXML(UserDAOHibern
ate.java:94)
[junit] at mil.ditv.dao.UserDAOTest.testSaveXML(UserDAOTest.java:67)
[junit] Test mil.ditv.dao.UserDAOTest FAILED
Name and version of the database you are using:
MS SQL 8.0