-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Problems persisting XML
PostPosted: Fri Nov 11, 2005 5:00 am 
Newbie

Joined: Fri Nov 11, 2005 4:42 am
Posts: 17
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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 11, 2005 7:24 am 
Beginner
Beginner

Joined: Fri Oct 28, 2005 10:46 am
Posts: 37
Where does your Session get opened and closed, and does it get flushed? The exception you're getting on calling persist is because the object which you're passing to persist is not transient. See a description of entity states here:
http://www.hibernate.org/hib_docs/v3/re ... ure-states

What does your test case look like?


Top
 Profile  
 
 Post subject: test case code
PostPosted: Sun Nov 13, 2005 12:56 am 
Newbie

Joined: Fri Nov 11, 2005 4:42 am
Posts: 17
public void testSaveXML() throws Exception {
SAXReader reader = new SAXReader();
Document doc = reader.read(new File("userXML.xml"));
Element userXML = doc.getRootElement();
userXML.element("rank").setText("CAPTIAN");

User user = dao.saveXML(userXML);
}


You can see the debug output in the stacktrace.

the file userXML.xml is the same with
<?xml version="1.0" encoding="UTF-8" ?>
at the top. THis file is generated using dom4jSession.get().... which works great.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 13, 2005 8:53 pm 
Beginner
Beginner

Joined: Fri Oct 28, 2005 10:46 am
Posts: 37
I'm not familiar with Hibernate's dom4j stuff, but it looks like you're reading an existing record, modifying it, and then trying to save the change. Is that right? That's not what the persist method is for. You'll want update or saveOrUpdate for that. And if saveOrUpdate is doing nothing, then you can concentrate on figuring out why that is. Are you doing any transaction or Session management? You didn't answer my question of where the Session is opened and closed. Are you just letting the HibernateTemplate do it for you?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 14, 2005 1:33 am 
Newbie

Joined: Fri Nov 11, 2005 4:42 am
Posts: 17
yes - saveOrUpdate is what I want to use. HibernateTemplate is handling the tx and session management - but since I need to get a new dom4jSession from the existing session I might need to flush, clear, close this myself but I am not sure. I have tried wrapping the saveOrUpdate method call in a new tx but that didn't help. I really want this to work! Today if I get a little time I will try turning on some more debugging and see if I can figure out what is going on in HibernateTemplate.

Shane


Top
 Profile  
 
 Post subject: yea!! this works
PostPosted: Tue Nov 22, 2005 1:23 am 
Newbie

Joined: Fri Nov 11, 2005 4:42 am
Posts: 17
I had to flush and clear the session do my saveOrUpdate then flush the dom4jSession.

These are methods in

public class BaseDAOHibernate<T, ID extends Serializable>
extends HibernateDaoSupport
implements DAO<T, ID>
{
// xml methods


public Document getXML(final ID id) throws ObjectRetrievalFailureException
{
return (Document) getHibernateTemplate().execute(new HibernateCallback()
{
public Object doInHibernate(Session session)
{
session.flush();
session.clear();
Session dom4jSession = session.getSession(EntityMode.DOM4J);

Document document = DocumentFactory.getInstance().createDocument();

Element element = (Element)dom4jSession.get(modelClass, id);
document.add(element);
return document;
}
});
}

public T saveXML(final Element element)
{
return (T) getHibernateTemplate().execute(new HibernateCallback()
{
public Object doInHibernate(Session session)
{
session.flush();
session.clear();
Session dom4jSession = session.getSession(EntityMode.DOM4J);

dom4jSession.saveOrUpdate(modelClass.getName(), element);
dom4jSession.flush();

return (T)session.get(modelClass,element.attributeValue("id"));
}
});
}
}

Hope this helps somebody out there!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.