-->
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.  [ 2 posts ] 
Author Message
 Post subject: Using any and many-to-any mappings
PostPosted: Thu May 06, 2004 3:29 pm 
Newbie

Joined: Thu May 06, 2004 2:57 pm
Posts: 7
I'm trying to understand how to use the any and many-to-any mappings, but haven't found any complete examples of how they should be used. I tried to put together a simple test, but can't get it working.

The idea I'm trying to implement is that two unrelated objects can have collections of children of the same type. For this example, I created ParentA and ParentB (only parent A is included below because B is an identical copy).

The parent tables have only an ID column, and the child table has an ID column, parent entity name, and parent primary key column.

Sorry for the long post...

Here is the example code:

Quote:
Code:
public class ParentA {

    protected int parentAId;
    protected Set children;


    public int getParentAId() {
        return this.parentAId;
    }

    public void setParentAId(int parentAId) {
        this.parentAId = parentAId;
    }


    public Set getChildren() {
        return children;
    }
   
    public void setChildren(Set children) {
        this.children = children;
    }
}


Quote:
Code:
public class Child {

    protected int childId;
    protected String parentEntityName;
    protected int parentId;
    protected Object parent;

    public int getChildId() {
        return this.childId;
    }

    public void setChildId(int childId) {
        this.childId = childId;
    }

    public String getParentTableName() {
        return this.parentTableName;
    }

    public void setParentEntityName(String parentEntityName) {
        this.parentEntityName = parentEntityName;
    }

    public int getParentId() {
        return this.parentId;
    }
   
    public void setParentId(int parentId) {
        this.parentId = parentId;
    }

    public Object getParent() {
        return parent;
    }

    public void setParent(Object parent) {
        this.parent = parent;
    }
}


And mapping files:

Quote:
Code:
<hibernate-mapping>
  <class name="test.ParentA" table="PARENT_A" dynamic-update="false" dynamic-insert="false">
    <id name="parentAId" column="PARENT_A_ID" type="int">
      <generator class="hilo"/>
    </id>

    <set name="children" table="CHILD" lazy="false" inverse="false" cascade="all" sort="unsorted">
      <key column="CHILD_ID" />
      <one-to-many class="test.Child" />
    </set>
  </class>
</hibernate-mapping>


Quote:
Code:
<hibernate-mapping>
  <class name="test.Child" table="CHILD" dynamic-update="false" dynamic-insert="false">
    <id name="childId" column="CHILD_ID" type="int">
      <generator class="hilo"/>
    </id>

    <any name="parent" meta-type="class" id-type="int">
      <column name="PARENT_ENTITY_NAME"/>
      <column name="PARENT_ID"/>
    </any>
  </class>
</hibernate-mapping>


When I run the following test, I get a strange error indicating its trying to update the child which has not been inserted into its table yet.

Quote:
Code:
        Set children = new HashSet();
        Child child1 = new Child();
        children.add(child1);
        ParentA parent = new ParentA();
        parent.setChildren(children);
        session.save(parent);


The error output is:

Quote:
Code:
    [junit] Hibernate: insert into PARENT_A (PARENT_A_ID) values (?)
    [junit] Hibernate: update CHILD set PARENT_ENTITY_NAME=?, PARENT_ID=? where CHILD_ID=?
    [junit] - Could not synchronize database state with session
    [junit] net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
    [junit]     at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
    ...
    [junit] ------------- ---------------- ---------------
    [junit] Testcase: testPersistence(test.MultiParentTest):        Caused an ERROR
    [junit] SQL insert, update or delete failed (row not found)
    [junit] net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
    [junit]     at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
    [junit]     at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:687)
    ...


Any ideas why it is trying to do an update instead of insert? I'm using an Oracle 8i database. Do the mappings seem corerct, or am I completely misunderstanding how to use them?

Thanks,

Jonathan


Top
 Profile  
 
 Post subject: Primary key problem fixed
PostPosted: Thu May 06, 2004 6:35 pm 
Newbie

Joined: Thu May 06, 2004 2:57 pm
Posts: 7
It seems the error I listed was being caused by not initializing my primary keys correctly.

By setting the initial value of each class' id to "-1" and setting initial-value="-1" on the id xml tag, I was able to get past that. I've got a new problem now...

It's trying to insert the child with a null value for PARENT_ENTITY_NAME. How do I get the child to automatically take the name of the parent entity? Do I have to manually set the value on every child?

Thanks,

Jonathan


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