-->
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.  [ 7 posts ] 
Author Message
 Post subject: Many-to-many mapping problem
PostPosted: Mon Apr 26, 2004 11:31 am 
Beginner
Beginner

Joined: Tue Mar 02, 2004 1:12 pm
Posts: 25
Location: Newcastle Upon Tyne
Hi

I am trying to create a many-to-many mapping between a Person and a Study class, where the association would have some attributes of its own. I followed the example provided in the Hibernate FAQ. My mappings look as follows:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
   <class name="uk.org.mygrid.mir.infomodel.PeopleAndOrganizations.Person" table="person">
      <id name="LSID" type="java.lang.String" column="person_id" length="80">
         <generator class="assigned"/>
      </id>
      <property name="firstName" type="java.lang.String" column="fName" not-null="true" length="20"/>
      <property name="lastName" type="java.lang.String" column="lName" not-null="true" length="30"/>
      <property name="email" type="java.lang.String" column="email" not-null="true" length="80"/>
      <property name="x509DN" type="java.lang.String" column="x509dn" not-null="true" length="50"/>
      <!-- associations -->
      <!-- bi-directional many-to-many association to Address -->
      <set name="addresses" table="address_persons">
         <key>
            <column name="person_id" length="80"/>
         </key>
         <many-to-many column="address_id" class="uk.org.mygrid.mir.infomodel.PeopleAndOrganizations.Address"/>
      </set>
      <!-- bi-directional many-to-many association to StudyParticipationEpisode (association class) -->
      <set name="studyparticipations" table="studyparticipationepisode">
         <key>
            <column name="person_id" length="80"/>
         </key>
         <composite-element class="uk.org.mygrid.mir.infomodel.EScienceProcess.StudyParticipationEpisode">
            <property name="startDate" type="java.sql.Date"/>
            <property name="endDate" type="java.sql.Date"/>
            <property name="role" type="java.lang.String" length="20"/>
            <property name="description" type="java.lang.String" length="30"/>
            <many-to-one name="study" class="uk.org.mygrid.mir.infomodel.EScienceProcess.Study"/>
         </composite-element>
      </set>
      <!-- bi-directional one-to-many association to Affiliation -->
      <set name="affiliations" lazy="true" inverse="true">
         <key>
            <column name="affiliated_person_id"/>
         </key>
         <one-to-many class="uk.org.mygrid.mir.infomodel.PeopleAndOrganizations.Affiliation"/>
      </set>
      <!-- bi-directional one-to-many association to LabBook (one person can have multiple LabBooks) -->   
      <set name="labBooks" lazy="true" inverse="true">
         <key>
            <column name="person_id"/>
         </key>
         <one-to-many class="uk.org.mygrid.mir.infomodel.EScienceProcess.LabBookView"/>
      </set>
   </class>
</hibernate-mapping>




When I am trying to load the Person and Study class as the configuration (Configuration.addClass...), it complains about a missing setter in the StudyParticipationEpisode class, although I have all the setters and getters in all the three classes.

Do I need to create a hbm file for this association separately?

I am using HIbernate 2.1.2 and MySql.

Please help.....

Cheers
Arijit


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 27, 2004 5:00 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Please read the red header and particulary the stacktrace and code (your POJO one)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 27, 2004 5:24 am 
Beginner
Beginner

Joined: Tue Mar 02, 2004 1:12 pm
Posts: 25
Location: Newcastle Upon Tyne
emmanuel wrote:
Please read the red header and particulary the stacktrace and code (your POJO one)


Sorry, my mistake. Forgot to add the other important elements...here are all of them:

1. Mapping for Study:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
   <class name="uk.org.mygrid.mir.infomodel.EScienceProcess.Study" table="study">
      <id name="LSID" type="java.lang.String" column="study_id" length="80">
         <generator class="assigned"/>
      </id>
      <property name="name" type="java.lang.String" column="name" length="80"/>
      <property name="startTime" type="java.sql.Date" column="startTime" length="10"/>
      <property name="endTime" type="java.sql.Date" column="endTime" length="10"/>
      <property name="status" type="java.lang.String" column="status" length="10"/>
      <property name="description" type="java.lang.String" column="description" length="100"/>
      <!-- associations -->
      <!-- bi-directional one-to-many association to Experimentinstance -->
      <set name="experimentinstances" lazy="true" inverse="true">
         <key>
            <column name="parent_study_id"/>
         </key>
         <one-to-many class="uk.org.mygrid.mir.infomodel.EScienceProcess.ExperimentInstance"/>
      </set>
      <!-- bi-directional many-to-one association to Labbookview -->
      <many-to-one name="labbookview" class="uk.org.mygrid.mir.infomodel.EScienceProcess.LabBookView" not-null="true">
         <column name="associated_labbook_id"/>
      </many-to-one>
      <!-- bi-directional many-to-one association to Programme -->
      <many-to-one name="programme" class="uk.org.mygrid.mir.infomodel.EScienceProcess.Programme" not-null="true">
         <column name="related_programme_id"/>
      </many-to-one>      
   </class>
</hibernate-mapping>


2. Mapping for Person:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
   <class name="uk.org.mygrid.mir.infomodel.PeopleAndOrganizations.Person" table="person">
      <id name="LSID" type="java.lang.String" column="person_id" length="80">
         <generator class="assigned"/>
      </id>
      <property name="firstName" type="java.lang.String" column="fName" not-null="true" length="20"/>
      <property name="lastName" type="java.lang.String" column="lName" not-null="true" length="30"/>
      <property name="email" type="java.lang.String" column="email" not-null="true" length="80"/>
      <property name="x509DN" type="java.lang.String" column="x509dn" not-null="true" length="50"/>
      <!-- associations -->
      <!-- bi-directional many-to-many association to Address -->
      <set name="addresses" table="address_persons">
         <key>
            <column name="person_id" length="80"/>
         </key>
         <many-to-many column="address_id" class="uk.org.mygrid.mir.infomodel.PeopleAndOrganizations.Address"/>
      </set>
      <!-- bi-directional many-to-many association to StudyParticipationEpisode (association class) -->
      <set name="studyparticipations" table="studyparticipationepisode">
         <key>
            <column name="person_id" length="80"/>
         </key>
         <composite-element class="uk.org.mygrid.mir.infomodel.EScienceProcess.StudyParticipationEpisode">
            <property name="startDate" type="java.sql.Date"/>
            <property name="endDate" type="java.sql.Date"/>
            <property name="role" type="java.lang.String" length="20"/>
            <property name="description" type="java.lang.String" length="30"/>
            <many-to-one name="study" class="uk.org.mygrid.mir.infomodel.EScienceProcess.Study"/>
         </composite-element>
      </set>
      <!-- bi-directional one-to-many association to Affiliation -->
      <set name="affiliations" lazy="true" inverse="true">
         <key>
            <column name="affiliated_person_id"/>
         </key>
         <one-to-many class="uk.org.mygrid.mir.infomodel.PeopleAndOrganizations.Affiliation"/>
      </set>
      <!-- bi-directional one-to-many association to LabBook (one person can have multiple LabBooks) -->
      <set name="labBooks" lazy="true" inverse="true">
         <key>
            <column name="person_id"/>
         </key>
         <one-to-many class="uk.org.mygrid.mir.infomodel.EScienceProcess.LabBookView"/>
      </set>
   </class>
</hibernate-mapping>


3. POJO of Study:

Code:
public class Study extends Resource implements Serializable {
   private String name;
   private Date startTime;
   private Date endTime;
   private String status;
   private String description;
   private LabBookView labbookview;
   private Programme programme;   
   private Set experimentinstances;
   
                public String getDescription() {
      return description;
   }
   
   public Date getEndTime() {
      return endTime;
   }
   
   public Set getExperimentinstances() {
      return experimentinstances;
   }
   
   public LabBookView getLabbookview() {
      return labbookview;
   }

   public String getName() {
      return name;
   }
   
   public Programme getProgramme() {
      return programme;
   }

   public Date getStartTime() {
      return startTime;
   }

   public String getStatus() {
      return status;
   }

   public void setDescription(String string) {
      description = string;
   }

   public void setEndTime(Date date) {
      endTime = date;
   }

   public void setExperimentinstances(Set set) {
      experimentinstances = set;
   }

   public void setLabbookview(LabBookView view) {
      labbookview = view;
   }

   public void setName(String string) {
      name = string;
   }

   public void setProgramme(Programme programme) {
      this.programme = programme;
   }

   public void setStartTime(Date date) {
      startTime = date;
   }

   public void setStatus(String string) {
      status = string;
   }   
}


4. POJO of Person:

Code:
public class Person extends Resource implements Serializable {
    private String x509DN;
    private String firstName;
    private String lastName;
    private String email;
    private Set addresses;
    private Set affiliations;
    private Set labBooks;
    private Set studyparticipations;

    public Set getAddresses() {
        return addresses;
    }

    public Set getAffiliations() {
        return affiliations;
    }

    public String getEmail() {
        return email;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getX509DN() {
        return x509DN;
    }

    public void setAddresses(Set set) {
        addresses = set;
    }

    public void setAffiliations(Set set) {
        affiliations = set;
    }

    public void setEmail(String string) {
        email = string;
    }

    public void setFirstName(String string) {
        firstName = string;
    }

    public void setLastName(String string) {
        lastName = string;
    }

    public void setX509DN(String string) {
        x509DN = string;
    }

    public Set getLabBooks() {
        return labBooks;
    }

    public void setLabBooks(Set set) {
        labBooks = set;
    }

    public Set getStudyparticipations() {
        return studyparticipations;
    }

    public void setStudyparticipations(Set set) {
        studyparticipations = set;
    }
}


5. POJO of StudyParticipationEpisode:

Code:
public class StudyParticipationEpisode implements Serializable {
   private Date startDate;
   private Date endDate;
   private String role;
   private String description;
   private Study study;
   
   public String getDescription() {
      return description;
   }

   public Date getEndDate() {
      return endDate;
   }

   public String getRole() {
      return role;
   }

   public Date getStartDate() {
      return startDate;
   }

   public void setDescription(String string) {
      description = string;
   }

   public void setEndDate(Date date) {
      endDate = date;
   }

   public void setRole(String string) {
      role = string;
   }

   public void setStartDate(Date date) {
      startDate = date;
   }

   public Study getStudy() {
      return study;
   }

   public void setStudy(Study study) {
      this.study = study;
   }
}


6. StackTrace:

Code:
test:
    [javac] Compiling 7 source files to F:\Work\working\mygrid\MIR\build
     [echo] remember to place your JDBC driver in the lib directory
     [copy] Copying 1 file to F:\Work\working\mygrid\MIR\build\uk\org\mygrid\mir\test
     [java] Creating configuration...
     [java] - Hibernate 2.1.2
     [java] - loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=org.gjt.mm.mysql.Driver, hibernate.cglib.use_re
flection_optimizer=true, hibernate.cache.provider_class=net.sf.hibernate.cache.HashtableCacheProvider, hibernate.cache.use_query_cache=true, hibernate
.max_fetch_depth=1, hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect, hibernate.jdbc.use_streams_for_binary=true, hibernate.jdbc.batch_size=0,
hibernate.query.substitutions=true 1, false 0, yes 'Y', no 'N', hibernate.proxool.pool_alias=pool1, hibernate.connection.username=arijitm, hibernate.c
onnection.url=jdbc:mysql://localhost:3306/mir, hibernate.connection.password=password, hibernate.connection.pool_size=1}
     [java] - using java.io streams to persist binary types
     [java] - using CGLIB reflection optimizer
     [java] - Mapping resource: uk/org/mygrid/mir/infomodel/PeopleAndOrganizations/Person.hbm.xml
     [java] - Mapping class: uk.org.mygrid.mir.infomodel.PeopleAndOrganizations.Person -> person
     [java] - Mapping collection: uk.org.mygrid.mir.infomodel.PeopleAndOrganizations.Person.addresses -> address_persons
     [java] - Mapping collection: uk.org.mygrid.mir.infomodel.PeopleAndOrganizations.Person.studyparticipations -> studyparticipationepisode
     [java] - Mapping resource: uk/org/mygrid/mir/infomodel/PeopleAndOrganizations/Address.hbm.xml
     [java] - Mapping class: uk.org.mygrid.mir.infomodel.PeopleAndOrganizations.Address -> address
     [java] - Mapping collection: uk.org.mygrid.mir.infomodel.PeopleAndOrganizations.Address.addressee -> address_persons
     [java] - Mapping resource: uk/org/mygrid/mir/infomodel/PeopleAndOrganizations/OrganizationalStructure.hbm.xml
     [java] - Mapping class: uk.org.mygrid.mir.infomodel.PeopleAndOrganizations.OrganizationalStructure -> organizationalstructure
     [java] - Mapping resource: uk/org/mygrid/mir/infomodel/PeopleAndOrganizations/OrganizationalUnit.hbm.xml
     [java] - Mapping class: uk.org.mygrid.mir.infomodel.PeopleAndOrganizations.OrganizationalUnit -> organizationalunits
     [java] - Mapping resource: uk/org/mygrid/mir/infomodel/PeopleAndOrganizations/Affiliation.hbm.xml
     [java] - Mapping class: uk.org.mygrid.mir.infomodel.PeopleAndOrganizations.Affiliation -> affiliation
     [java] - Mapping resource: uk/org/mygrid/mir/infomodel/PeopleAndOrganizations/AffiliationEpisode.hbm.xml
     [java] - Mapping class: uk.org.mygrid.mir.infomodel.PeopleAndOrganizations.AffiliationEpisode -> affiliationepisode
     [java] - processing one-to-many association mappings
     [java] net.sf.hibernate.PropertyNotFoundException: Could not find a setter for property startDate in class uk.org.mygrid.mir.infomodel.EScienceProcess.StudyParticipationEpisode
     [java]     at net.sf.hibernate.property.BasicPropertyAccessor.getSetter(BasicPropertyAccessor.java:131)
     [java]     at net.sf.hibernate.mapping.Property.getSetter(Property.java:182)
     [java]     at net.sf.hibernate.cfg.Binder.bindComponent(Binder.java:881)
     [java]     at net.sf.hibernate.cfg.Binder.bindCollectionSecondPass(Binder.java:1191)
     [java]     at net.sf.hibernate.cfg.Binder$CollectionSecondPass.secondPass(Binder.java:1352)
     [java]     at net.sf.hibernate.cfg.Binder$SecondPass.doSecondPass(Binder.java:1328)
     [java]     at net.sf.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:600)
     [java]     at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:743)
     [java]     at uk.org.mygrid.mir.test.HibernateTest.main(HibernateTest.java:267)
     [java] java.lang.RuntimeException: couldn't get connection
     [java]     at uk.org.mygrid.mir.test.HibernateTest.main(HibernateTest.java:274)
     [java] Exception in thread "main"


It has something to do with the many-to-many mapping with attributes between Person and Study, because once I remove this (and remove the associated POJOs), the rest works.

A related question: If I use hbm2java to generate the POJOs from my mapping files, it generates an extra POJO called StudyParticipation - I can't figure out where it is coming from...

Hoping for a solution...

Cheers
Arijit


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 27, 2004 7:14 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
what happens when you comment only this:
Code:
<property name="startDate" type="java.sql.Date"/>

?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 27, 2004 7:18 am 
Beginner
Beginner

Joined: Tue Mar 02, 2004 1:12 pm
Posts: 25
Location: Newcastle Upon Tyne
delpouve wrote:
what happens when you comment only this:
Code:
<property name="startDate" type="java.sql.Date"/>

?


In that case, the same exception is thrown for the next property, i.e. endDate. If that is commented out, then the same for role...

Arijit


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 27, 2004 7:26 am 
Beginner
Beginner

Joined: Tue Mar 02, 2004 1:12 pm
Posts: 25
Location: Newcastle Upon Tyne
arijitm wrote:
delpouve wrote:
what happens when you comment only this:
Code:
<property name="startDate" type="java.sql.Date"/>

?


In that case, the same exception is thrown for the next property, i.e. endDate. If that is commented out, then the same for role...

Arijit


Initially I was trying to model it in this way:

Person (*)-----------------(*) Study

And the association class (StudyParticipationEpisode) would have something like:

StudyParticipationEpisode (1)-----------(*) Role

I couldn't figure out how to map another class to the many-to-many association class, so I moved the role and decription within the association class itself.

Once this "setter" issue is resolved, I would need to look into this issue.

Arijit


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 27, 2004 9:17 am 
Beginner
Beginner

Joined: Tue Mar 02, 2004 1:12 pm
Posts: 25
Location: Newcastle Upon Tyne
This seems stupid - but probably it was a problem with eclipse. I cleaned all the POJOs and regenerated the setters and getters, and now this particular problem seems to have gone away.

Arijit


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