-->
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: problem with many-to-one association
PostPosted: Mon May 23, 2005 4:41 pm 
Newbie

Joined: Mon May 23, 2005 4:27 pm
Posts: 1
Hibernate version:2

I am creating a small application to learn O/R Mapping, but I failed. Hope somebody here can give me a hand.

here is the situation: there is a table named CATEGORY and its schema is CATEGORY (ID INT, NAME char(20), PARENT_ID INT). I created a POJO class named Category, which has four attributes
Code:
   private int ID;
   /**
    *   category name.
    */
   private String name;
   /**
    *   parent category
    */
   private Category parent;
   /**
    *   children category
    */
   private Set children;

There is a many-to-one association from Category to Category itself, that is, a category can have many children categories. So here is the mapping file I created
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 package="com.customizetree.hibernate.model">
   <class
      name="Category"
      table="CATEGORY">
      <id
         name = "ID"
         column = "ID"
         type = "integer">
         <generator class="increment"/>
      </id>
      
      <property
         name = "name"
         column = "NAME"
         type = "string">
      </property>

      <many-to-one
         name="parent"
         column="PARENT_ID"
         class="Category"
         not-null="true"/>
         
      <set
         name="children"
         inverse="true">
         <key column="PARENT_ID"/>
         <one-to-many class="Category"/>
      </set>
   </class>
</hibernate-mapping>

there is always a NullPointerException in the Category class


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2005 2:38 am 
Beginner
Beginner

Joined: Fri Feb 11, 2005 12:03 pm
Posts: 48
Location: Kiel, Germany
Hi,

where does the NullPointerException occur?

This mapping works for me but it looks slightly different.
Here is my mapping
Code:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping
>
    <class
        name="Category"
        table="category"
    >

        <id
            name="id"
            column="id"
            type="int"
        >
            <generator class="identity">
            </generator>
        </id>

        <set
            name="children"
            table="category"
            lazy="false"
            cascade="none"
            sort="unsorted"
        >

              <key
                  column="parent_id"
              >
              </key>

              <one-to-many
                  class="Category"
              />

        </set>

        <property
            name="name"
            type="java.lang.String"
            column="name"
        />

        <many-to-one
            name="parent"
            class="Category"
            cascade="none"
            outer-join="auto"
            column="parent_id"
        />

    </class>

</hibernate-mapping>


The difference is the table attribute in the set.


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.