I apologize if this turns out to be user stupidity but I've read and searched and haven't been able to resolve this. Hopefully it's either a bug or has a simple solution that won't waste much time.
I have a class called Containing that contains an embedded reference to a class called Child. I want Child to be null. As I understand from the documentation, this should be possible, and when loading Hibernate will return a null child object if all of the fields of the child are null. However, I get an exception trying to insert the containing object with a null embedded child object.
Hibernate version:
Hibernate 3.2 cr1, Hibernate Annotations 3.1.0.Beta10b
Containing class:
Code:
package org.grs.temp;
import java.io.Serializable;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Containing implements Serializable {
private int id;
private Child child;
@Id @GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Embedded
public Child getChild() {
return child;
}
public void setChild(Child child) {
this.child = child;
}
}
Child class:Code:
package org.grs.temp;
import javax.persistence.Embeddable;
@Embeddable
public class Child {
private int cField1;
public int getCField1() {
return cField1;
}
public void setCField1(int field1) {
cField1 = field1;
}
}
Code between sessionFactory.openSession() and session.close():Code:
Containing c = new Containing();
hibernateSession.persist(c);
hibernateSession.flush();
Full stack trace of any exception that occurs:Code:
org.hibernate.PropertyValueException: not-null property references a null or transient value: org.grs.temp.Containing.child
at org.hibernate.engine.Nullability.checkNullability(Nullability.java:72)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:284)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:180)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:131)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:87)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:38)
at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:617)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:591)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:595)
at org.grs.gui.ConsoleBean.displayConsole(ConsoleBean.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at net.sourceforge.stripes.controller.DispatcherServlet.doPost(DispatcherServlet.java:159)
at net.sourceforge.stripes.controller.DispatcherServlet.doGet(DispatcherServlet.java:63)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at net.sourceforge.stripes.controller.StripesFilter.doFilter(StripesFilter.java:195)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.grs.common.servlet.LoginFilter.doFilter(LoginFilter.java:62)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.grs.common.servlet.AccessFilter.doFilter(AccessFilter.java:94)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.grs.common.servlet.DebugFilter.doFilter(DebugFilter.java:62)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.grs.common.servlet.ConnectionFilter.doFilter(ConnectionFilter.java:113)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
Name and version of the database you are using:
Oracle 9
The generated SQL (show_sql=true):
None