-->
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.  [ 3 posts ] 
Author Message
 Post subject: help needed mapping two tables and classess
PostPosted: Tue Aug 22, 2006 4:41 pm 
Newbie

Joined: Fri Aug 18, 2006 6:47 am
Posts: 11
Hibernate version:3



Hibernate version: 3.1

I am trying to map two tables together, created as two POJO classess. here iis the relationship:

tblUser tblGroup
id id
name userid
description


the two tables are linked through the userid on the group table. The relationship is many users to one group i.e. many to one. so in the class named Group.java I have the following code:

private Set userList();

//a getter method and a setter method resides below the above line.


What do i need to put in the Group.hbm.xml file to map the relatioship between the two tables?!?! Because I want to be able do something like this in a servlet:

....//session is created here

List result = session.createQuery("from Group").list();

for (int i = 0; i < result.size(); i++) {
Group myGroup = (Group) result.get(i);
Users userList = myGroup.getUserList();
User firstUser = (User)userList.get(i);
out.println(firstUser.getName());

//note that the User POJO class has already been created with a name to //map to the column name in the tblUser table.
}



I thought there would be an easy way of retrieveing all the users for a group like such without create a hql query, if so how is it done?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 22, 2006 7:06 pm 
Beginner
Beginner

Joined: Sat Aug 19, 2006 8:04 pm
Posts: 30
hi

this is exactly the type of thing hibernate is supposed to do for you.

i don't write mapping files, so i can't help you if all you want to do is write the *.hbm.xml files. but you don't have to do that, take a look at xdoclet.

xdoclet is a code generator, that reads javadoc comments from your java files and can generate these clumsy mapping files for you. it's really nice, no need to write xml and keep those files in sync when you change your classes.

first you'll need to download xdoclet and place these files

xdoclet.jar
xdoclet-hibernate-module.jar
xdoclet-xdoclet-module.jar
xjavadoc.jar

in your class path. if you're using java 1.5 (you should be!) you'll have to download xjavadoc1-5.jar from their site seperately. once you have those files set, you'll need ant.

i hope you're using eclipse which has ant in it, if not you'll have to download it and use it from the command line.

here's the build.xml to run xdoclet (generate the .hbm.xml and the hibernate.cfg.xml) AND run the schema export task to create the tables and link them up for you (how nice is that?!)

Code:
<project>
<target name="generate-hbm">
      <taskdef name="hibernatedoclet"
classname="xdoclet.modules.hibernate.HibernateDocletTask"
               classpathref="project.classpath"  />
      <property file="${conf.dir}/hibernate.properties" />
      <hibernatedoclet destdir="${classes.dir}"
                        verbose="true">
         <fileset dir="${src.dir}">
            <include name="**/*.java" />
         </fileset>
         <hibernate version="3.0" />
         <hibernatecfg dialect="${hibernate.dialect}"
                     jdbcUrl="${hibernate.connection.url}"
                        driver="${hibernate.connection.driver_class}"
                        userName="${hibernate.connection.username}"
                        password="${hibernate.connection.password}"
                        showSql="true"
                        version="3.0" />
      </hibernatedoclet>
   </target>
   <target name="schema-export" >
      <taskdef name="schemaexport"
               classname="org.hibernate.tool.hbm2ddl.SchemaExportTask"
               classpathref="project.classpath" />
      <schemaexport quiet="no"
                     output="${conf.dir}/schema-export.sql"
                     properties="${conf.dir}/hibernate.properties">
         <fileset dir="${classes.dir}">
            <include name="**/*.hbm.xml"/>
         </fileset>
      </schemaexport>
   </target>
</project>


so if you run ant generate-hbm it will scan your java files and make those files for you. if you run ant schema-export it will create the tables and set all the foreign keys and stuff.

now to answer your question, to tell xdoclet what is what and how they relate, you have to comment your classes.

Code:
/**
* @hibernate.class
*/
public class ManyClass implements Serializable {
    private Long id;
    private String something;
    private OneClass oneThing;

    /**
     * @hibernate.id generator-class="native"
     */
    public Long getId()  {
        return id;
    }
     
    /**
      * @hibernate.property
      */
    public String getSomething()  {
         return something;
    }

    /**
     * @hibernate.many-to-one column="oneClass_id"
     */
     public OneClass getOneThing()  {
          return oneThing;
     }

     // setter methods for all properties ommitted
}

/**
   * @hibernate.class
   */
    public class OneThing implements Serializable  {
           private Long id;
           private Date date;

         /**
           * @hibernate.id generator-class="native"
           */
           public Long getId()  {
                  return id;
           }

         /**
          * @hibnerate.property
          */
          public Date getDate()  {
                  return date;
          }

         // setter methods
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 23, 2006 4:31 am 
Newbie

Joined: Fri Aug 18, 2006 6:47 am
Posts: 11
thanks for the detailed response, i was hoping however that there would be an easy way to do this rather than using another program.....


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