-->
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.  [ 3 posts ] 
Author Message
 Post subject: Unable to insert entity with a null embedded component
PostPosted: Sun Apr 30, 2006 12:15 am 
Newbie

Joined: Tue Dec 07, 2004 3:14 pm
Posts: 1
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


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 01, 2006 2:10 pm 
Beginner
Beginner

Joined: Wed Apr 26, 2006 2:41 pm
Posts: 30
I would replace

Code:
private Child child;

with
Code:
private Child child = new Child();


My understanding of the spec is that Hibernate may return null for the getChild() if both are null but it still shouldn't know that the implementation of setId depends on an inner class. That would break encapsulation.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 01, 2006 4:25 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
an int cannot be null by its very nature, so use Integer and the DB will be generated correctly

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.