Embarrassed to ask this.
I have a uni-directional many-to-one association. When I save a new object from the owning side (the many side), H tries to save it with a null in the association field.
It's like this. I've got some classes:
Code:
class Car {
@Id etc...
@ManyToOne
@JoinColumn(nullable=false)
Color color;
}
class Color {
@Id etc...
}
The user is creating a car and has picked a color. My webapp receives a POST from the browser. I have a session factory and am using session per thread. The code goes something like this (of course it doesn't all actually happen in one method of one class...I'm just eliding the details)
Code:
factory.getCurrentSession().beginTransaction();
Car car = new Car();
Long colorId = ... (from the POST parameters)
Color color = (Color) session.createQuery("from Color where id=:id").setParameter("id", colorId).uniqueResult();
car.setColor(color);
session.save(car);
factory.getCurrentSession().getTransaction().commit();
And that's where it fails:
Code:
SEVERE: Servlet.service() for servlet FilterConfig threw exception
java.sql.BatchUpdateException: ORA-01400: cannot insert NULL into ("MySchema"."CAR"."COLOR_ID")
at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:566)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:9365)
at oracle.jdbc.driver.OracleStatementWrapper.executeBatch(OracleStatementWrapper.java:210)
at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.livevox.config.inputfilter.web.FrameworkServlet.commit(FrameworkServlet.java:54)
at com.livevox.config.inputfilter.web.FilterConfigServlet.doService(FilterConfigServlet.java:45)
at com.livevox.config.inputfilter.web.FilterConfigServlet.doPost(FilterConfigServlet.java:35)
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(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
Through tracing I discovered that when I run the query, it triggers a flush, and it seems like at this point, the newly created Car object is stored in Hibernate/JDBC's internal stash of objects, without of course the Color. Then when I commit(), it finds that copy of Car and tries to save _it_, rather than the one I was using, which has a Color.
When I use FlushMode.COMMIT, this doesn't happen, but that causes me other problems, and why should I have to do that?
What is UP with this??
-Larry