I'm new to Hibernate (ver 2), and having session troubles.
I have a Struts action that is trying to load 2 code table lookups into an http-session-scoped form bean
before it forwards control to the JSP.
I am getting the exception "identifier of an instance of Carrier altered from 1 to null".
Do I have to close and open a new session for every code table I load?
Here's the guts of the Struts action:
Code:
Session sess = HibernateUtil.currentSession(); // opens session and stores into ThreadLocal
form.setCarriers(Carrier.getAllCarriers(sess));
form.setAirports(Airport.getAllAirports(sess)); // exception is thrown here
sess.flush();
HibernateUtil.closeSession();
Here's my class and mapping file for the first drop down (shortened for brevity):
Code:
public class Carrier
{
private Long id;
private String abbrev;
private String name;
public static List getAllCarriers(Session sess) throws Exception
{
return sess.find("from Carrier");
}
}
Code:
<hibernate-mapping>
<class
name="Carrier"
table="CARRIERS"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="id"
column="ID"
type="java.lang.Long"
>
<generator class="native">
</generator>
</id>
<property
name="name"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="NAME"
length="80"
/>
<property
name="abbrev"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="ABBREV"
length="8"
/>
<property
name="version"
type="int"
update="true"
insert="true"
access="property"
column="VERSION"
/>
</class>
</hibernate-mapping>
...and for the second drop down (a legacy code table that uses a String as it's ID):
Code:
public class Airport
{
private String code;
private String city;
private int countryId;
private int provinceId;
public Airport()
{
}
public static List getAllAirports(Session sess) throws Exception
{
return sess.find("from Airport");
}
}
Code:
<hibernate-mapping>
<class
name="Airport"
table="AIRPORT"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="code"
column="AIRPORT_CODE"
type="string"
length="5"
>
<generator class="assigned">
</generator>
</id>
<property
name="city"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="CITY"
length="40"
/>
<property
name="countryId"
type="int"
update="true"
insert="true"
access="property"
column="COUNTRY_ID"
/>
<property
name="provinceId"
type="int"
update="true"
insert="true"
access="property"
column="PROVINCE_ID"
/>
</class>
</hibernate-mapping>
It hits the exception when I try and load the second code table, and tells me I am modifying the objects from the first table:
net.sf.hibernate.HibernateException: identifier of an instance of com.xxx.admin.flightplanner.beans.Carrier altered from 1 to null
at net.sf.hibernate.impl.SessionImpl.checkId(SessionImpl.java:2638)
at net.sf.hibernate.impl.SessionImpl.flushEntity(SessionImpl.java:2461)
at net.sf.hibernate.impl.SessionImpl.flushEntities(SessionImpl.java:2454)
at net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2256)
at net.sf.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:1801)
at net.sf.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:1567)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1532)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1520)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1512)
at com.xxx.admin.flightplanner.beans.Airport.getAllAirports(Airport.java:38)
at com.xxx.admin.flightplanner.struts.FlightScheduleStartAction.performAction(FlightScheduleStartAction.java:68)
at com.xxx.arch.struts.BaseAction.doExecute(BaseAction.java:76)
at com.xxx.arch.struts.BaseAction.execute(BaseAction.java:46)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at jrun.servlet.ServletInvoker.invoke(ServletInvoker.java:91)
at jrun.servlet.JRunInvokerChain.invokeNext(JRunInvokerChain.java:42)
at jrun.servlet.JRunRequestDispatcher.invokeNext(JRunRequestDispatcher.java:408)
at jrun.servlet.JRunRequestDispatcher.forwardInvoke(JRunRequestDispatcher.java:378)
at jrun.servlet.JRunRequestDispatcher.forward(JRunRequestDispatcher.java:157)
at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1069)
at org.apache.struts.tiles.TilesRequestProcessor.doForward(TilesRequestProcessor.java:274)
at org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:455)
at org.apache.struts.tiles.TilesRequestProcessor.processForwardConfig(TilesRequestProcessor.java:320)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:279)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at jrun.servlet.ServletInvoker.invoke(ServletInvoker.java:91)
at jrun.servlet.JRunInvokerChain.invokeNext(JRunInvokerChain.java:42)
at jrun.servlet.JRunRequestDispatcher.invoke(JRunRequestDispatcher.java:226)
at jrun.servlet.ServletEngineService.dispatch(ServletEngineService.java:527)
at jrun.servlet.http.WebService.invokeRunnable(WebService.java:172)
at jrunx.scheduler.ThreadPool$DownstreamMetrics.invokeRunnable(ThreadPool.java:348)
at jrunx.scheduler.ThreadPool$ThreadThrottle.invokeRunnable(ThreadPool.java:451)
at jrunx.scheduler.ThreadPool$UpstreamMetrics.invokeRunnable(ThreadPool.java:294)
at jrunx.scheduler.WorkerThread.run(WorkerThread.java:66)
Can anyone shed some light on this exception? I'm not modifying any objects at all, so why am I seeing this exception? The message seems misleading.
Thanks in advance
M.