-->
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: Foreign Keys, Set Problem
PostPosted: Wed Oct 06, 2010 1:05 pm 
Beginner
Beginner

Joined: Thu Jun 17, 2010 7:36 am
Posts: 26
Hello,

Ive got a problem. I know here is a lot of code but it looks worse than it is :-)
Im working on a small webapplication with hibernate and spring.
Ive got (to explain I reduced it a little bit) two tables:

users(username, password, enabled) --> pk username
userinformation(email,username,etc) --> pk email, fk username


I used the reverse engineering wizard from netbeans for hibernate hbm.xml files and pojos.
now Ive got the following result: (snippets)

users.hbm.xml
Code:
<class catalog="project" name="project.backend.hibernate.Users" table="users">
    <id name="username" type="string">
      <column length="50" name="username"/>
      <generator class="assigned"/>
    </id>
    <property name="password" type="string">
      <column length="50" name="password" not-null="true"/>
    </property>
    <property name="enabled" type="boolean">
      <column name="enabled" not-null="true"/>
    </property>

    <set inverse="true" name="userinformations">
      <key>
        <column length="50" name="username" not-null="true" unique="true"/>
      </key>
      <one-to-many class="project.backend.hibernate.Userinformation"/>
    </set>

  </class>


users.java
Code:
public class Users  implements java.io.Serializable {

     private String username;
     private String password;
     private boolean enabled;
     private Set userinformations = new HashSet(0);

    public Users() {
    }
   
    public Users(String username, String password, boolean enabled) {
        this.username = username;
        this.password = password;
        this.enabled = enabled;
    }
    public Users(String username, String password, boolean enabled, Set userinformations) {
       this.username = username;
       this.password = password;
       this.enabled = enabled;
       this.userinformations = userinformations;
    }
 
// more getter and setter

    public Set getUserinformations() {
        return this.userinformations;
    }
   
    public void setUserinformations(Set userinformations) {
        this.userinformations = userinformations;
    }

}





userinformation.hbm.xml
Code:
<class catalog="project" name="project.backend.hibernate.Userinformation" table="userinformation">
    <id name="email" type="string">
      <column length="50" name="email"/>
      <generator class="assigned"/>
    </id>
    <many-to-one class="project.backend.hibernate.Users" fetch="select" name="users">
      <column length="50" name="username" not-null="true" unique="true"/>
    </many-to-one>
// more properties, unimportant..
</class>


userinformation.java
Code:
public class Userinformation  implements java.io.Serializable {

     private String email;
     private Users users;

    public Userinformation() {
    }

   
    public Userinformation(String email, Users users) {
        this.email = email;
        this.users = users;
    }

    public Userinformation(String email, Users users) {
   // here are more informations set, I reduced them because its shorter then but I dont really understand why there are so many constructors??
       this.email = email;
       this.users = users;
    }
   
    public String getEmail() {
        return this.email;
    }
   
    public void setEmail(String email) {
        this.email = email;
    }

  public Users getUsers() {
        return this.users;
    }
   
    public void setUsers(Users users) {
        this.users = users;
    }

// more, unimportant

}


Here is a snippet from my projectDao where I call objects from the database. I need to show it to you to explain my problem:
HibernateDao.java

Code:
public class HibernateDao {

    public Users loadUsersByUsername(String username) {
      return (Users) this.sessionFactory.getCurrentSession().get(Users.class, username);
    }
   
   public Users loadUsersByUsernameWithUserinformationJoin(String username) {
// seems to be wrong
        List<Users> list = this.sessionFactory.getCurrentSession()
                .createQuery("from Users users where users.username=? left join users.userinformation as userinformation")
                .setParameter(0, username)
                .list();
        Users u = list.get(0);
        return u;
    }

    public Userinformation loadUserinformationByUsers(Users u) {
        List<Userinformation> list = this.sessionFactory.getCurrentSession()
                .createQuery("from Userinformation userinformation where userinformation.users=?")
                .setParameter(0, u)
                .list();
        Userinformation userinf = list.get(0);
        return userinf;
    }


}




my question is now how to work with those sets. For example:
I got a username and I want to get the Userinformation for it, so I got the following possibilities:

Code:
            // 1. Possibility:
            // load first Users object by username. Then load Userinformation by Users object. These are two database queries which does not look good to me.
            //Userinformation userinf2 = projectDao.loadUserinformationByUsers(projectDao.loadUsersByUsername(username));
           
            // 2. load a Users object by username. Then call getUserinformations from the users object which returns a set. But what then? I dont know how to work with that set?
            //Set set =  projectDao.loadUsersByUsername(username).getUserinformations();
            //Userinformation userinf =  .... ?

            /* 3. Possibility:
             * load a Users object by username. Then call getUserinformations from the users object which returns a set. Cast this set to a list to work with it. Unfortunately this does not work...
             * List<Userinformation> list = (List<Userinformation>) projectDao.loadUsersByUsername(username).getUserinformations();
            Userinformation userinf = list.get(0);
             */
     
            // 4. join
           // I read about using inner joins, left joins, outer joins... to get the collections with the object, but that is not working. ?!
           // the method I tried with is public Users loadUsersByUsernameWithUserinformationJoin(String username), this returns a users object. I tried to get then the userinformation from it, but it is still a set.
           


So I would like to know
HOW to work with those sets. WHAT is the best way to get the Userinformation by the username. Please help me! I would be really grateful!


Top
 Profile  
 
 Post subject: Re: Foreign Keys, Set Problem
PostPosted: Thu Oct 07, 2010 6:43 am 
Beginner
Beginner

Joined: Thu Jun 17, 2010 7:36 am
Posts: 26
I read now about lazy loading. By loading a object, the collections within are not loaded, only when requested (by default)

So I added this method to my hibernateDao.java, so that I load the collection while the session is still open
(I was not sure, if in my class where I normally call the hibernateDao-methods the session is still open? I mean, the transaction is still open.... but I wanted to play safe)

Code:
    public Userinformation loadUsersByUsernameWithUserinformation(String username) {
        Users u = (Users) this.sessionFactory.getCurrentSession().get(Users.class, username);
         Map userinf = (Map) u.getUserinformations();
         Userinformation userinfObject = (Userinformation) userinf;
         return userinfObject;
    }


This does not work either. It always stops when I try to call u.getUserinformations(), no matter which way I try! Please, I need help!


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.