-->
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: Unidirectional many-to-one association
PostPosted: Thu Jan 25, 2007 9:52 pm 
Newbie

Joined: Thu Jan 25, 2007 9:24 pm
Posts: 2
Hey everyone,

I'm having a problem creating a simple unidirectional many-to-one association in Hibernate. If there is an example of hibernate mappings and code that I can reference, I would gladly check out those resources.

The situation is that a Division has a ContactPerson. Here is the mapping for my Division and Person entities, respectively. (Please note that the Person mapping's Address and Contact properties need to be changed to many-to-one and one-to-one mappings after I figure this problem out.)

Division.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
   <class name="data.Division" table="DIVISIONS">
      <id name="divisionID" column="DIVISION_ID">
         <generator class="native"/>
      </id>
      <property name="divisionName" column="DIVISION_NAME"/>
      <property name="seasonID" column="SEASON_ID"/>
      <property name="rank" column="RANK"/>
      <property name="imageID" column="IMAGE_ID"/>
      <property name="message" column="MESSAGE"/>
      <many-to-one name="personID" class="data.Person" column="PERSON_ID"/>
   </class>
</hibernate-mapping>


Person.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
   <class name="data.Person" table="PERSON">
      <id name="personID" column="PERSON_ID">
         <generator class="native"/>
      </id>
      <property name="lastName" column="LAST_NAME"/>
      <property name="firstName" column="FIRST_NAME"/>
      <property name="middleInitial" column="MIDDLE_INITIAL"/>
      <property name="addressID" column="ADDRESS_ID"/>
      <property name="contactID" column="CONTACT_ID"/>
      <property name="dob" column="DATE_OF_BIRTH"/>
      <property name="heightPrim" column="HEIGHT_PRIMARY"/>
      <property name="heightSec" column="HEIGHT_SECONDARY"/>
      <property name="weight" column="WEIGHT"/>
   </class>
</hibernate-mapping>



Here is the code for the Division and Person entities:

Division.java
Code:
public class Division
{
   private int divisionID;
   private String divisionName = "";
   private int seasonID;
   private int rank;
   private int imageID;
   private String message = "";
   private int personID;
   
   public Division()
   {
      
   }

   public int getDivisionID()
   {
      return divisionID;
   }

   private void setDivisionID(int divisionID)
   {
      this.divisionID = divisionID;
   }

   public String getDivisionName()
   {
      return divisionName;
   }

   public void setDivisionName(String divisionName)
   {
      this.divisionName = divisionName;
   }

   public int getSeasonID()
   {
      return seasonID;
   }

   public void setSeasonID(int seasonID)
   {
      this.seasonID = seasonID;
   }

   public int getImageID()
   {
      return imageID;
   }

   public void setImageID(int imageID)
   {
      this.imageID = imageID;
   }

   public String getMessage()
   {
      return message;
   }

   public void setMessage(String message)
   {
      this.message = message;
   }

   public int getRank()
   {
      return rank;
   }

   public void setRank(int rank)
   {
      this.rank = rank;
   }

   public int getPersonID()
   {
      return personID;
   }

   public void setPersonID(int personID)
   {
      this.personID = personID;
   }

   
}



Person.java

Code:
public class Person
{
   private int personID;
   private String firstName;
   private String lastName;
   private String middleInitial;
   private int addressID;
   private int contactID;
   private Date dob;             // date of birth
   private int heightPrim;         // primary height (eg. 5 feet or 183 cm)
   private int heightSec;         // secondary height (eg. 10 inches)
   private double weight;

   /* Empty Constructor */
   public Person()
   {
      
   }

   public int getPersonID()
   {
      return personID;
   }

   private void setPersonID(int personID)
   {
      this.personID = personID;
   }
   
   public int getAddressID()
   {
      return addressID;
   }

   public void setAddressID(int addressID)
   {
      this.addressID = addressID;
   }

   public int getContactID()
   {
      return contactID;
   }

   public void setContactID(int contactID)
   {
      this.contactID = contactID;
   }

   public Date getDob()
   {
      return dob;
   }

   public void setDob(Date dob)
   {
      this.dob = dob;
   }

   public String getFirstName()
   {
      return firstName;
   }

   public void setFirstName(String firstName)
   {
      this.firstName = firstName;
   }

   public int getHeightPrim()
   {
      return heightPrim;
   }

   public void setHeightPrim(int heightPrim)
   {
      this.heightPrim = heightPrim;
   }

   public int getHeightSec()
   {
      return heightSec;
   }

   public void setHeightSec(int heightSec)
   {
      this.heightSec = heightSec;
   }

   public String getLastName()
   {
      return lastName;
   }

   public void setLastName(String lastName)
   {
      this.lastName = lastName;
   }

   public String getMiddleInitial()
   {
      return middleInitial;
   }

   public void setMiddleInitial(String middleInitial)
   {
      this.middleInitial = middleInitial;
   }

   public double getWeight()
   {
      return weight;
   }

   public void setWeight(double weight)
   {
      this.weight = weight;
   }
   
   
}


And here is the servlet code that I use to attempt to add a new division to the divisions table. (Please note that the divContactPersonID variable is referencing an entry in the Person table that exists.)


AddDivision.java


Code:
         // Begin unit of work - Store Division
         Session addDivSession = HibernateUtil.getSessionFactory().openSession();
         Transaction addDivTx = addDivSession.beginTransaction();
         
         // Create and store division
         Division newDivision = new Division();
         
         newDivision.setDivisionName(divisionName);
         newDivision.setSeasonID(seasonID);
         newDivision.setRank(rank);
         newDivision.setImageID(imageID);
         newDivision.setMessage(message);
           newDivision.setPersonID(divContactPersonID);         
         
         addDivSession.save(newDivision);
         
         addDivSession.flush();
         // End unit of work
         addDivTx.commit();


Finally, here is the error that is being generated. I believe this line
Quote:
23:41:26,365 ERROR [BasicPropertyAccessor:167] - IllegalArgumentException in class: data.Person, getter method of property: personID
is where the problem is coming from, but I don't know why.


Code:
23:41:26,360 DEBUG [SessionImpl:220] - opened session at timestamp: 11779116863
23:41:26,361 DEBUG [JDBCTransaction:54] - begin
23:41:26,361 DEBUG [DriverManagerConnectionProvider:93] - total checked-out connections: 1
23:41:26,362 DEBUG [DriverManagerConnectionProvider:99] - using pooled JDBC connection, pool size: 0
23:41:26,362 DEBUG [JDBCTransaction:59] - current autocommit status: false
23:41:26,362 DEBUG [DefaultSaveOrUpdateEventListener:161] - saving transient instance
23:41:26,363 DEBUG [SQL:393] - select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')
23:41:26,364 DEBUG [SequenceGenerator:82] - Sequence identifier generated: 5
23:41:26,364 DEBUG [AbstractSaveEventListener:113] - generated identifier: 5, using strategy: org.hibernate.id.SequenceGenerator
23:41:26,365 DEBUG [AbstractSaveEventListener:152] - saving [data.Division#5]
23:41:26,365 ERROR [BasicPropertyAccessor:167] - IllegalArgumentException in class: data.Person, getter method of property: personID
23:41:26,368 DEBUG [SessionImpl:220] - opened session at timestamp: 11779116863
23:41:26,369 DEBUG [ThreadLocalSessionContext:290] - allowing method [getTransaction] in non-transacted context
23:41:26,369 DEBUG [ThreadLocalSessionContext:300] - allowing proxied method [getTransaction] to proceed to real session
23:41:26,370  WARN [AbstractExceptionHandler:92] - Unhandled exception
org.hibernate.TransactionException: Transaction not successfully started
        at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:149)
        at components.leagueDataMgmt.divisions.AddDivision.execute(AddDivision.java:140)
        at org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:53)
        at org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:64)
        at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:48)
        at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
        at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
        at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
        at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:280)
        at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1858)
        at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:459)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
        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 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:869)
        at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
        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)

23:41:26,371  WARN [ExceptionCatcher:162] - Exception from exceptionCommand 'servlet-exception'
org.hibernate.TransactionException: Transaction not successfully started
        at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:149)
        at components.leagueDataMgmt.divisions.AddDivision.execute(AddDivision.java:140)
        at org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:53)
        at org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:64)
        at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:48)
        at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
        at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
        at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
        at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:280)
        at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1858)
        at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:459)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
        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 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:869)
        at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
        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)
23:41:26,371 ERROR [[action]:253] - Servlet.service() for servlet action threw exception
org.hibernate.TransactionException: Transaction not successfully started
        at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:149)
        at components.leagueDataMgmt.divisions.AddDivision.execute(AddDivision.java:140)
        at org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:53)
        at org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:64)
        at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:48)
        at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
        at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
        at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
        at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:280)
        at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1858)
        at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:459)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
        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 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:869)
        at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
        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)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 25, 2007 10:09 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I haven't read all of your post, so there may be multiple errors in there, but the basic error is that you've put a person ID in the Division object, but put a person in the hibernate mapping. Also, you've called the person a personID, but that won't break the mapping (it'll just confuse users).

Change the type of personID in Division to be Person, not int. Change the getters and setters, and you might as well rename all of those to getPerson(), setPerson(), etc.

The only class that knows about the action person ID is the Person class. All classes that refer to Perons know only about Person objects.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 26, 2007 1:24 pm 
Newbie

Joined: Thu Jan 25, 2007 9:24 pm
Posts: 2
Hey tenwit,

Thanks for the response. I tried that approach before, but for some reason, it didn't work... perhaps I was missing something. However, I tried it again under your advice and it seemed to work out fine. Thanks for the help.

Regards,

Jay


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.