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 usernameI 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!