-->
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: newbie question
PostPosted: Mon Sep 13, 2004 11:17 am 
Newbie

Joined: Mon Sep 13, 2004 11:11 am
Posts: 3
Hello everybody.

I try to make a first example with Hibernate on my database ans I can't manage to make it work.
My english is quite poor and I'm sure I missed something in tutorials or documentation.
So, I ask my question but you might find it trivial and I apologize.

I have two tables called ParentTable and ChildTable. Here is the Table description :

ParentTable :
PARENT_ID_PARENT NUMBER(13) -- PK Parent

ChildTable :
CHILD_ID_CHILD NUMBER(13) -- PK Child
CHILD_ID_PARENT NUMBER(13) -- FK : link between child and parent (contains parent id values)

I want to make two classes called Parent and Child with those definitions :

Parent :
long id
Vector childs


Child :
long id


I don't manage to write the XML files properly.

I guess that it is a <one-to-many> relation but I tried to write some bag or set or etc. and I didn't win.

Could you please try to explain me what I've missed ?

Thanks in advance.

St


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 14, 2004 2:42 am 
Newbie

Joined: Thu Aug 12, 2004 9:32 am
Posts: 17
Hi,

as far for my short knowledge it should be somthing like :
Code:
<class name="Parent" table=".......
    <id name="parent_id" column="PARENT_ID_PARENT" type="long">
         <generator .................../>
    </id>
    <set name="children">
           <key column="CHILD_ID_PARENT"/>
           <one-to-many class="Child"/>
    </set>
</class>

<class name="Child" table=".......
   <id name="child_id" column="CHILD_ID_CHILD" type="long">
       <generator .....
   </id>
   ......
   ......    OTHER PROPERTIES OF CHILD
</class>


and the class should be simple like :
Code:
public class Parent {
   public long parent_id;
   public Set  children;
   ...

public class Child {
   public long child_id;
   ....
   ....


I think that should do it.....

give it a try...

Nir


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 14, 2004 4:41 am 
Newbie

Joined: Mon Sep 13, 2004 11:11 am
Posts: 3
I doesn't seem to work :-(

Either, how can Hibernate found the link ? In the Parent.hbm.xml, we speak about ParentTable table and we mention CHILD_ID_PARENT which is a ChidTable column ?!?

<class name="Parent" table=".......
<id name="parent_id" column="PARENT_ID_PARENT" type="long">
<generator .................../>
</id>
<set name="children">
<key column="CHILD_ID_PARENT"/> ---> This is ChildTable col !!!
<one-to-many class="Child"/>
</set>
</class>

Thanks for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 14, 2004 6:24 am 
Newbie

Joined: Thu Aug 12, 2004 9:32 am
Posts: 17
i used default uuid generators take a look :

MAPPING
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
    <class name="test.hibernate.Child" table="Child1">
        <id type="string" unsaved-value="null" >
            <column name="child_id_child" sql-type="varchar2(32)" not-null="true"/>
               <generator class="uuid.hex"/>
        </id>
        <property name="childName">
            <column name="childName" sql-type="char(30)" not-null="true"/>
        </property>
    </class>
</hibernate-mapping>   


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
    <class name="test.hibernate.Parent" table="Parent1">
        <id type="string" unsaved-value="null" >
            <column name="parent_id_parent" sql-type="varchar2(32)" not-null="true"/>
               <generator class="uuid.hex"/>
        </id>
        <property name="parentName" access="field">
            <column name="ParentName" sql-type="char(30)" not-null="true"/>
        </property>
        <set name="children">
              <key column="CHILD_ID_PARENT"/>
              <one-to-many class="test.hibernate.Child"/>
       </set>
    </class>
</hibernate-mapping>   



Tables :
Code:
SQL> desc child1
Name                            Type
---------------------------- --------
CHILD_ID_CHILD           CHAR(32)
CHILD_ID_PARENT         CHAR(32)
CHILDNAME                   CHAR(30)

SQL> desc parent1
Name                             Type
--------------------------- ---------
PARENT_ID_PARENT     CHAR(32)
PARENTNAME               CHAR(30)


CLASSES:
Code:
public class Parent {
    public String parentName;
    public Set children = new HashSet();

                public void addChild(Child child) {
             this.children.add(child);
                }
   public Set getChildren() {
      return children;
   }
   public void setChildren(Set children) {
      this.children = children;
   }
   public String getParentName() {
      return parentName;
   }
   public void setParentName(String parentName) {
      this.parentName = parentName;
   }
}


public class Child {
   private String childName;
   
   public String getChildName() {
      return childName;
   }
   public void setChildName(String childName) {
      this.childName = childName;
   }
}


RUNNING :
Code:
    Child child = new Child();
    child.setChildName("Child");
      
    Parent parent = new Parent();
    parent.setParentName("Parent");
    parent.addChild(obj);

    sess.save(child);
    sess.save(parent);
    t.commit();

OUTPUT :
Code:
SQL> select * from parent1;

PARENT_ID_PARENT                  PARENTNAME
--------------------------------  -----------
8a8080e7fefc5b3100fefc5b528b0002 Parent

SQL> select * from child1;

CHILD_ID_CHILD                     CHILD_ID_PARENT                   CHILDNAME
--------------------------------   --------------------------------   ----------
8a8080e7fefc5b3100fefc5b3c670001 8a8080e7fefc5b3100fefc5b528b0002    Child     


hibernate goes :

Hibernate: insert into Child1 (childName, child_id_child) values (?, ?)
Hibernate: insert into Parent1 (ParentName, parent_id_parent) values (?, ?)
Hibernate: update Child1 set CHILD_ID_PARENT=? where child_id_child=?



hope it works now.....(works for me....)....

Nir


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 14, 2004 8:28 am 
Newbie

Joined: Mon Sep 13, 2004 11:11 am
Posts: 3
Okay

It works fine for me now !

I had a mistake in the children file (kept during the lot of tests I've done)

Thank you for your precious help !!!


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.