Hibernate version: 3.2.4 shipped with JBoss AS 4.2.2
Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 29, 2008 10:21:46 AM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="com.abc.uresdemo.bean.Result" table="result" catalog="uresdemo">
<id name="transactionId" type="string">
<column name="transactionID" length="50" />
<generator class="assigned" />
</id>
<timestamp name="timeStamp" column="timeStamp" />
<many-to-one name="status" class="com.abc.uresdemo.bean.Status" fetch="select">
<column name="status" length="2" not-null="true">
<comment>Record status</comment>
</column>
</many-to-one>
<many-to-one name="parameters" class="com.abc.uresdemo.bean.Parameters" fetch="select">
<column name="deviceParameterID" length="20" not-null="true">
<comment>Device Parameter ID</comment>
</column>
</many-to-one>
<property name="deviceId" type="string">
<column name="deviceID" length="20" not-null="true">
<comment>Device ID of Parameters</comment>
</column>
</property>
<property name="parameterName" type="string">
<column name="parameterName" length="45" not-null="true">
<comment>Parameter description</comment>
</column>
</property>
<property name="parameterKey" type="string">
<column name="parameterKey" length="20" not-null="true">
<comment>Parameter key name</comment>
</column>
</property>
<property name="lastUpdatedDate" type="timestamp">
<column name="lastUpdatedDate" length="0" not-null="true">
<comment>Last updated date time</comment>
</column>
</property>
<property name="lastUpdatedBy" type="string">
<column name="lastUpdatedBy" length="20" not-null="true">
<comment>Last updated person</comment>
</column>
</property>
<property name="result" type="string">
<column name="result" length="200" />
</property>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():I uses singleton class to initiate single Session in the following class:
Code:
public class EntityHome {
/**
* {@link Log} instance
*/
private static final Log log = LogFactory.getLog(EntityHome.class);
/**
* Instance of {@link Session}
*/
private Session session;
private SessionFactory sessionFactory;
private static EntityHome entityHome;
/**
* Return instance of {@link Session}
*
* @return {@link Session}
*/
public Session getSessionInstance() {
if (session == null || !session.isOpen()) {
session = sessionFactory.openSession();
}
return session;
}
/**
* Single instance constructor
* @return {@link EntityHome}
*/
public static EntityHome getEntityHomeInstance() {
if (entityHome == null) {
entityHome = new EntityHome();
}
return entityHome;
}
/**
* Return instance of {@link SessionFactory}
*
* @return {@link SessionFactory}
*/
private SessionFactory getSessionFactory() {
try {
return (SessionFactory) new InitialContext().lookup("SessionFactory");
} catch (Exception e) {
log.error("Could not locate SessionFactory in JNDI", e);
throw new IllegalStateException("Could not locate SessionFactory in JNDI");
}
}
private EntityHome() {
sessionFactory = getSessionFactory();
}
/**
* Overriding {@link org.hibernate.Session#persist(Object)} embed with
* {@link org.hibernate.Session#beginTransaction()} and
* {@link Transaction#commit()}
*
* @param transientInstance
* {@link Object}
*/
public void writePersist(Object transientInstance) {
log.debug("writePersist");
getSessionInstance().beginTransaction();
getSessionInstance().persist(transientInstance);
getSessionInstance().getTransaction().commit();
}
/**
* Overriding {@link org.hibernate.Session#saveOrUpdate(Object)} embed with
* {@link org.hibernate.Session#beginTransaction()} and
* {@link Transaction#commit()}
*
* @param instance
* {@link Object}
*/
public void writeSaveOrUpdate(Object instance) {
log.debug("writeSaveOrUpdate");
getSessionInstance().beginTransaction();
getSessionInstance().saveOrUpdate(instance);
getSessionInstance().getTransaction().commit();
}
/**
* Overriding {@link org.hibernate.Session#merge(Object)} embed with
* {@link org.hibernate.Session#beginTransaction()} and
* {@link Transaction#commit()}
*
* @param instance
* {@link Object}
*/
public void merge(Object instance) {
log.debug("merge");
getSessionInstance().beginTransaction();
getSessionInstance().merge(instance);
getSessionInstance().getTransaction().commit();
}
}
Full stack trace of any exception that occurs:Code:
13:57:41,824 ERROR [ResultHome] persist failed
org.hibernate.PersistentObjectException: detached entity passed to persist: com.
abc.uresdemo.bean.Result
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(Default
PersistEventListener.java:79)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(Default
PersistEventListener.java:38)
at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:618)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:592)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:596)
at com.abc.uresdemo.home.EntityHome.writePersist(EntityHome.java:
85)
at com.abc.uresdemo.home.ResultHome.persist(ResultHome.java:27)
at com.abc.uresdemo.logic.impl.URESDemoBusinessImpl.submitData(UR
ESDemoBusinessImpl.java:110)
at com.abc.uresdemo.mobile.servlet.URESDemoUplink.doPost(URESDemo
Uplink.java:89)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFi
lter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:175)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(Securit
yAssociationValve.java:179)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValv
e.java:84)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:102)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedC
onnectionValve.java:157)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
a:262)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proce
ss(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:44
6)
at java.lang.Thread.run(Thread.java:619)
Name and version of the database you are using: MySQL 5.0
The generated SQL (show_sql=true):Code:
...
13:56:30,357 INFO [STDOUT] Hibernate: select users0_.loginID as loginID6_0_, us
ers0_.status as status6_0_, users0_.userFirstName as userFirs3_6_0_, users0_.use
rLastName as userLast4_6_0_, users0_.loginPassword as loginPas5_6_0_, users0_.la
stLoginDateTime as lastLogi6_6_0_, users0_.lastUpdatedDate as lastUpda7_6_0_, us
ers0_.lastUpdatedBy as lastUpda8_6_0_ from uresdemo.users users0_ where users0_.
loginID=?
13:56:47,206 INFO [STDOUT] Hibernate: update uresdemo.users set status=?, userF
irstName=?, userLastName=?, loginPassword=?, lastLoginDateTime=?, lastUpdatedDat
e=?, lastUpdatedBy=? where loginID=?
13:56:58,541 INFO [STDOUT] Hibernate: select parameters0_.deviceParameterID as
devicePa1_2_0_, parameters0_.status as status2_0_, parameters0_.deviceID as devi
ceID2_0_, parameters0_.parameterName as paramete4_2_0_, parameters0_.parameterKe
y as paramete5_2_0_, parameters0_.lastUpdatedDate as lastUpda6_2_0_, parameters0
_.lastUpdatedBy as lastUpda7_2_0_ from uresdemo.parameters parameters0_ where pa
rameters0_.deviceParameterID=?
13:57:03,507 INFO [STDOUT] Hibernate: select status0_.statusID as statusID4_0_,
status0_.statusDescription as statusDe2_4_0_, status0_.lastUpdatedDate as lastU
pda3_4_0_, status0_.lastUpdatedBy as lastUpda4_4_0_ from uresdemo.status status0
_ where status0_.statusID=?
13:57:08,240 INFO [STDOUT] Hibernate: select hilosequen0_.SEQUENCENAME as SEQUE
NCE1_7_0_, hilosequen0_.HIGHVALUES as HIGHVALUES7_0_ from uresdemo.hilosequences
hilosequen0_ where hilosequen0_.SEQUENCENAME=?
13:57:17,692 INFO [STDOUT] Hibernate: update uresdemo.hilosequences set HIGHVAL
UES=? where SEQUENCENAME=?
...
Debug level Hibernate log excerpt:N/A
Below is the business logic:
Code:
public void submitData(DataUplink dataUplink) throws LoginException, BusinessException {
log.debug("loginThenSubmitData");
Result result = new Result();
result.setDeviceId(dataUplink.getDeviceId());
Parameters parameters = parametersHome.findById(dataUplink.getDeviceParameterId());
if (null != parameters) {
result.setParameters(parameters);
}
result.setTimeStamp(dataUplink.getDateTime());
result.setParameterKey(dataUplink.getParameterKey());
result.setParameterName(dataUplink.getParameterName());
result.setResult(dataUplink.getResult());
result.setLastUpdatedBy(dataUplink.getUserId());
result.setLastUpdatedDate(GregorianCalendar.getInstance().getTime());
Status status = statusHome.findById("02");
if (null != status) {
result.setStatus(status);
}
result.setTransactionId(hilosequencesHome.getNextHiloSequenceInString(HilosequencesHome.HILOSEQ_TRANSAQID));
resultHome.persist(result);
}
Could someone pls explain to me which part of my code get object detached?
Thanks