-->
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.  [ 11 posts ] 
Author Message
 Post subject: composite-id
PostPosted: Wed Jan 03, 2007 12:36 pm 
Newbie

Joined: Tue Jun 20, 2006 11:51 am
Posts: 14
I'm trying to create a composite-id as described in the "Mapping the join table to an intermediate entity" section on p. 303 but Hibernate is not finding the inner Id class. I used Listing 7.1 as a template to create the Java class (I'm using Survey->SurveyQuestion<-Question instead of Category->CategorizedItem<-Item) and am using XML (from p. 305) instead of annotations. In the composite-id class attribute I've tried using SurveyQuestion.Id as well as SurveyQuestion$Id (both with and without package names).

What am I missing?

Thanks,
Gary

P.S. Please let me know if I've not included enough info.

Hibernate version: 3.1.3

Mapping documents:

Code:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class
      name="xxx.model.SurveyQuestion"
      table="SURVEY_QUESTION">

      <composite-id
         name="id"
         class="xxx.model.SurveyQuestion.Id">
         <key-property
            name="surveyId"
            access="field"
            column="SURVEY_ID" />
         <key-property
            name="questionId"
            access="field"
            column="QUESTION_ID" />
      </composite-id>

      <many-to-one
         name="survey"
         column="SURVEY_ID"
         not-null="true"
         insert="false"
         update="false" />

      <many-to-one
         name="question"
         column="QUESTION_ID"
         not-null="true"
         insert="false"
         update="false" />

      <property
         name="questionNumber"
         formula="QUESTION_NUM" />

   </class>
</hibernate-mapping>



Java code:

Code:
public class SurveyQuestion extends BaseObject {

   public static class Id implements Serializable {
      private static final long serialVersionUID = -5060137236621292400L;
      private Long surveyId;
      private Long questionId;
      public Id() {}
      public Id(Long surveyId, Long questionId) {
         this.surveyId = surveyId;
         this.questionId = questionId;
      }
      @Override
      public boolean equals(final Object other) {
         if (this == other)
            return true;
         if (!(other instanceof Id))
            return false;
         Id castOther = (Id) other;
         return new EqualsBuilder().append(surveyId, castOther.surveyId)
               .append(questionId, castOther.questionId).isEquals();
      }
      @Override
      public int hashCode() {
         return new HashCodeBuilder().append(surveyId).append(questionId)
               .toHashCode();
      }
   }
   
   private static final long serialVersionUID = 913381528098814703L;
   private Id id = new Id();
   private Survey survey;
   private Question question;
   private Integer questionNumber;
   
   public SurveyQuestion(){}
   
   public SurveyQuestion(Survey survey, Question question) {
      this.survey = survey;
      this.question = question;
      this.id.surveyId = survey.getId();
      this.id.questionId = question.getId();
   }
.
.
.
}



Full stack trace of any exception that occurs:

Code:
[sandbox] ERROR [main] ContextLoader.initWebApplicationContext(211) | Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext-hibernate.xml]: Initialization of bean failed; nested exception is org.hibernate.MappingException: class mil.jfcom.model.SurveyQuestion.Id not found while looking for property: surveyId
org.hibernate.MappingException: class mil.jfcom.model.SurveyQuestion.Id not found while looking for property: surveyId
   at org.hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:80)
   at org.hibernate.mapping.SimpleValue.setTypeUsingReflection(SimpleValue.java:276)
   at org.hibernate.cfg.HbmBinder.createProperty(HbmBinder.java:2138)
   at org.hibernate.cfg.HbmBinder.bindComponent(HbmBinder.java:1867)
   at org.hibernate.cfg.HbmBinder.bindCompositeId(HbmBinder.java:1700)
   at org.hibernate.cfg.HbmBinder.bindCompositeId(HbmBinder.java:433)
   at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(HbmBinder.java:347)
   at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:282)
   at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:153)
   at org.hibernate.cfg.Configuration.add(Configuration.java:386)
   at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:427)
   at org.springframework.orm.hibernate3.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:679)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1091)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:396)
   at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:233)
   at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:145)
   at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:277)
   at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:313)
   at org.springframework.web.context.support.AbstractRefreshableWebApplicationContext.refresh(AbstractRefreshableWebApplicationContext.java:139)
   at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:252)
   at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:190)
   at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:49)
   at mil.jfcom.webapp.listener.StartupListener.contextInitialized(StartupListener.java:51)
   at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3729)
   at org.apache.catalina.core.StandardContext.start(StandardContext.java:4187)
   at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:759)
   at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:739)
   at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:524)
   at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:904)
   at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:867)
   at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:474)
   at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1122)
   at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:310)
   at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
   at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1021)
   at org.apache.catalina.core.StandardHost.start(StandardHost.java:718)
   at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1013)
   at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:442)
   at org.apache.catalina.core.StandardService.start(StandardService.java:450)
   at org.apache.catalina.core.StandardServer.start(StandardServer.java:709)
   at org.apache.catalina.startup.Catalina.start(Catalina.java:551)
   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 org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:294)
   at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:432)
Caused by: java.lang.ClassNotFoundException: mil.jfcom.model.SurveyQuestion.Id
   at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1352)
   at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1198)
   at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
   at java.lang.Class.forName0(Native Method)
   at java.lang.Class.forName(Class.java:164)
   at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:108)
   at org.hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:76)
   ... 46 more


Name and version of the database you are using: Oracle 10g


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 03, 2007 12:39 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Please ask in the regular user forum, this forum is about the book content or sample code. I suggest you start from the (working) downloadable sample code.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 03, 2007 12:41 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Your problem is most likely that you didn't specify the inner class correctly:

class="xxx.model.SurveyQuestion$Id"

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 03, 2007 12:42 pm 
Beginner
Beginner

Joined: Thu Aug 31, 2006 2:31 pm
Posts: 25
Location: USA
Im using something similar except instead of inner class i created invidual java class for all composite pk .It worked fine.
Thanks,
Vinodh

_________________
I am using a shitty e-mail filtering system that caused a lot of bounces for the admin of this forum. I need to turn on my brain next time and update my e-mail address.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 03, 2007 12:58 pm 
Newbie

Joined: Tue Jun 20, 2006 11:51 am
Posts: 14
christian wrote:
Your problem is most likely that you didn't specify the inner class correctly:

class="xxx.model.SurveyQuestion$Id"


Thanks for the response Christian. I posted here because I was basically using the samples provided in the book (with names changed to protect the innocent) and I thought it might be a quick and obvious answer. I will refer future posts to the appropriate forum.

BTW, I did try class="xxx.model.SurveyQuestion$Id" as I explained in my original post:

GDW wrote:
In the composite-id class attribute I've tried using SurveyQuestion.Id as well as SurveyQuestion$Id (both with and without package names).


I'm not familiar with the "$" syntax. Is that explained anywhere in the book (or elsewhere)?

P.S. I am a faithful customer. I've bought both HIA and JPWH (book and eBook).


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 03, 2007 12:59 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
The dollar sign is standard Java syntax.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 03, 2007 1:01 pm 
Newbie

Joined: Tue Jun 20, 2006 11:51 am
Posts: 14
vinodhts wrote:
Im using something similar except instead of inner class i created invidual java class for all composite pk .It worked fine.
Thanks,
Vinodh


Thanks Vinodh.

That was going to be my next experiment. I was trying to avoid having extra class files but at this point I just need something that works.

Gary


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 03, 2007 1:04 pm 
Beginner
Beginner

Joined: Thu Aug 31, 2006 2:31 pm
Posts: 25
Location: USA
Hi Gary,
Well I dont know about using $ to access the inner class !!
I would try the same you did like using dot operator :) I will give a short with these inner classes for my next example.
Thanks,
Vinodh.

_________________
I am using a shitty e-mail filtering system that caused a lot of bounces for the admin of this forum. I need to turn on my brain next time and update my e-mail address.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 03, 2007 1:56 pm 
Newbie

Joined: Tue Jun 20, 2006 11:51 am
Posts: 14
vinodhts wrote:
Hi Gary,
Well I dont know about using $ to access the inner class !!
I would try the same you did like using dot operator :) I will give a short with these inner classes for my next example.
Thanks,
Vinodh.


I finally found references to using $ to access inner classes. It appears that the compiler renames inner classes to OuterClass$InnerClass: http://www.unix.org.ua/orelly/java/langref/ch05_03.htm (look for "Implementation of inner classes" section)

I'm not sure I would consider this "standard Java syntax" (can it be used inside a Java class??) but rather an obscure fact about the internals of the Java compiler. Valuable information nonetheless.

Regardless, I tried both $ and dot in my code with no luck. I'm going to try to take the CaveatEmptor code, strip out the annotations and use the XML provided in the book. I'll post my findings.

BTW, what did you mean when you stated "I will give a short with these inner classes for my next example"? Are you going to try to get it to work?

Thanks again,
Gary


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 03, 2007 5:45 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
The CaveatEmptor packages contain a working example of this, with and without annotations.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 03, 2007 6:50 pm 
Newbie

Joined: Tue Jun 20, 2006 11:51 am
Posts: 14
christian wrote:
The CaveatEmptor packages contain a working example of this, with and without annotations.


Thanks. I found it and it appears to work when I run the integration tests.

Unfortunately, I'm still unable to get it to work in my code. I guess I'll take it over to the User's forum. It's frustrating that I can't see what I'm doing differently to cause it to fail, especially given that it seems so straightforward.

Gary


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