-->
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.  [ 5 posts ] 
Author Message
 Post subject: many-to-many with data
PostPosted: Thu Oct 02, 2003 12:00 am 
Newbie

Joined: Wed Oct 01, 2003 11:29 pm
Posts: 3
I'm trying to implement a many-to-many association with data in the association class/table. I'm using a composite-element, but this doesn't allow many-to-many, so I'm using two many-to-ones, basing my config on the customer/order/product example in the reference documentation.

First problem: I can't figure out how to set/get data in the composite-element class (LineItem in the example, Pmask in my code). When I generate the Role, Moan, and Pmask classes with hbm2java, there's no mention of Pmask in Role or Moan, so I don't see how I can manipulate the Pmask instances.

Second problem: using my mappings, the schema is defined, but when I run the code below with the reflection optimiser turned off, I get

Code:
java.lang.IllegalArgumentException: object is not an instance of declaring class
...
at java.lang.reflect.Method.invoke(Method.java:324)
...
rethrown as net.sf.hibernate.PropertyAccessException: IllegalArgumentException occurred calling: object is not an instance of declaring class getter of com.spherion.ap.esb.auth.Pmask.mask


at tx.commit(). Running in the Eclipse debugger, it appears that method in Getter is defined as Pmask.getMask(), but the invoke target is the "role1" instance of Role, hence the exception. At this point I don't have a clue what's going on.

What am I doing wrong?

Code:

Code:
        Role role1 = new Role("role1", 10);
        ArrayList roles = new ArrayList();
        roles.add(role1);
       
        Moan root = new Moan();
       
        Moan m1 = new Moan("child1", root);
        m1.setRoles(roles);
               
        Session session = sessions.openSession();
        Transaction tx = null;
        try
        {
            tx = session.beginTransaction();
           
            session.save(root);
            session.save(m1);
           
            session.save(role1);

            tx.commit();
            session.close();


HBM files:

Code:
<hibernate-mapping>
   
  <class name="com.spherion.ap.esb.auth.Moan" table="esb_moans">

    <id name="id" column="id" type="long">
       <generator class="native"/>
    </id>

    <property name="name" type="string" not-null="true"/>

    <many-to-one name="parent" class="com.spherion.ap.esb.auth.Moan" column="parentid"/>

   <list name="roles" table="esb_moans_roles" lazy="false">
      <key column="moanid"/>
      <index column="idx"/>
      <composite-element class="com.spherion.ap.esb.auth.Pmask">
         <property name="mask" type="long"/>
         <many-to-one class="com.spherion.ap.esb.auth.Role" name="role" column="roleid"/>
      </composite-element>
   </list>
  </class>
 
</hibernate-mapping>


Code:
<hibernate-mapping>
   
  <class name="com.spherion.ap.esb.auth.Role" table="esb_roles">
     
     <id name="id" column="id" type="long">
        <generator class="native"/>
     </id>

   <property name="name" type="string" not-null="true"/>
   <property name="rank" type="integer"/>
   <list name="moans" table="esb_moans_roles" lazy="false" inverse="true">
      <key column="roleid"/>
      <index column="idx"/>
      <composite-element class="com.spherion.ap.esb.auth.Pmask">
         <property name="mask" type="long"/>
         <many-to-one class="com.spherion.ap.esb.auth.Moan" name="role" column="moanid"/>
      </composite-element>
   </list>

  </class>

</hibernate-mapping>


Thanks.

PJDM


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2003 12:44 am 
Beginner
Beginner

Joined: Wed Aug 27, 2003 4:17 pm
Posts: 27
Location: California, USA
An alternative that worked for me was to define and map three classes, one for each table. Roughly:

Code:
  <class name="Moan" table="moans">
    <id name="id" column="id" type="long">
       <generator class="native"/>
    </id>
    <property name="name" type="string" not-null="true"/>
    <map name="roleAssociations">
      <key column="moanId"/>
      <index column="idx"/>
      <one-to-many class="MoanRoleAssociation"/>
   </map>
  </class>
  <class name="MoanRoleAssociation" table="moans_roles">
    <id name="idx" column="idx" type="long">
       <generator class="native"/>
    </id>
    <property name="mask" column="mask" type="long"/>
    <many-to-one name="moan" class="Moan" column="moanId"/>
    <many-to-one name="role" class="Role" column="roleId"/>
  </class>
  <class name="Role" table="roles">
    <id name="id" column="id" type="long">
       <generator class="native"/>
    </id>
    <property name="name" type="string" not-null="true"/>
    <map name="moanAssociations">
      <key column="roleId"/>
      <index column="idx"/>
      <one-to-many class="MoanRoleAssociation"/>
   </map>
  </class>


This is rather different from your first choice. Perhaps it's worse. :-)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2003 4:43 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
The idea is that the collections will actually contain instances of the composite element class at runtime!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2003 7:54 pm 
Newbie

Joined: Wed Oct 01, 2003 11:29 pm
Posts: 3
That looks fairly straightforward, I'll give it a go. Thanks.

For curiosity's sake, if nothing else, I'd still like to know what's going on with my original mapping, and how to deal with the association class.

PJDM


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 03, 2003 1:48 am 
Newbie

Joined: Wed Oct 01, 2003 11:29 pm
Posts: 3
I couldn't figure out how to do the mapping from the moan and role (Hibernate was attempting to update a pmask row without storing it first), so I'm just doing the many-to-one mappings from the pmask.

It seems less OO this way, but at least I can get it to work (so far).

Thanks.

PJDM


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