I would like to get back all the attributes of a user when I make the method getUser. At the moment, I get back only the attributes which are not collections (set). I manage to get back the attributes id, pseudo, email and telephone but all other attributes are null While all these collections are filled well in the database. The problem would be situated such in my request hql or in my user.hbm.xml file? Thank you for your help. Here are some files:
User.java
Code:
public class User {
/** Attributs */
private Integer id;
private String pseudo;
private String telephone;
private String email;
@XmlTransient
private Set<Demande> demandes;
@XmlTransient
private Set<Invitation> aInvite;
@XmlTransient
private Set<Notification> notifications;
private DernierePosition dernierePosition;
@XmlTransient
private Set<Group> groups;
@XmlTransient
private Set<Group> proprietaire;
@XmlTransient
private Set<Marqueur> marqueurs;
/** Constructeur */
public User() {
}
public User(String telephone, String pseudo, String email) {
super();
this.pseudo = pseudo;
this.telephone = telephone;
this.email = email;
this.demandes = new HashSet<Demande>();
this.aInvite = new HashSet<Invitation>();
this.notifications = new HashSet<Notification>();
this.groups = new HashSet<Group>();
this.proprietaire = new HashSet<Group>();
}
public Integer getId() {
return id;
}
@XmlElement
public void setId(Integer id) {
this.id = id;
}
public String getPseudo() {
return pseudo;
}
@XmlElement
public void setPseudo(String pseudo) {
this.pseudo = pseudo;
}
public String getTelephone() {
return telephone;
}
@XmlElement
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getEmail() {
return email;
}
@XmlElement
public void setEmail(String email) {
this.email = email;
}
@XmlTransient
public Set<Demande> getDemandes() {
return demandes;
}
public void setDemandes(Set<Demande> demandes) {
this.demandes = demandes;
}
public DernierePosition getDernierePosition() {
return dernierePosition;
}
@XmlElement
public void setDernierePosition(DernierePosition dernierePosition) {
this.dernierePosition = dernierePosition;
}
@XmlTransient
public Set<Group> getProprietaire() {
return proprietaire;
}
public void setProprietaire(Set<Group> proprietaire) {
this.proprietaire = proprietaire;
}
@XmlTransient
public Set<Notification> getNotifications() {
return notifications;
}
public void setNotifications(Set<Notification> notifications) {
this.notifications = notifications;
}
@XmlTransient
public Set<Group> getGroups() {
return groups;
}
public void setGroups(Set<Group> groups) {
this.groups = groups;
}
@XmlTransient
public Set<Invitation> getaInvite() {
return aInvite;
}
public void setaInvite(Set<Invitation> aInvite) {
this.aInvite = aInvite;
}
public void addToGroups(Group group) {
this.getGroups().add(group);
group.getInvites().add(this);
}
public void removeFromGroups(Group group) {
this.getGroups().remove(group);
group.getInvites().remove(this);
}
@XmlTransient
public Set<Marqueur> getMarqueurs() {
return marqueurs;
}
public void setMarqueurs(Set<Marqueur> marqueurs) {
this.marqueurs = marqueurs;
}
@Override
public boolean equals(Object other) {
if (this.getId() == ((User) other).getId())
return true;
else
return false;
}
@Override
public int hashCode() {
return id.hashCode();
}
}
user.hbm.xml
Code:
<hibernate-mapping>
<class name="modele.User" table="user">
<meta attribute="class-description">
This class contains the user detail.
</meta>
<id name="id" type="java.lang.Integer" column="id">
<generator class="native"/>
</id>
<property name="pseudo" column="pseudo" type="string"/>
<property name="telephone" column="telephone" type="string" not-null="true" unique="true"/>
<property name="email" column="email" type="string"/>
<!-- Mapping Set<Demande> demandes -->
<set name="demandes" lazy="false" inverse="true" cascade="all" >
<key column="demandeur_id"/>
<one-to-many class="modele.Demande"/>
</set>
<!-- Mapping Set<Invitation> aInvite -->
<set name="aInvite" lazy="false" inverse="true" cascade="all">
<key column="inviteur_id"/>
<one-to-many class="modele.Invitation"/>
</set>
<!-- Mapping Set<Notification> notifications -->
<set name="notifications" lazy="false" inverse="true" cascade="all">
<key column="user_id"/>
<one-to-many class="modele.Notification"/>
</set>
<!-- Mapping Set<Group> groups -->
<set name="groups" table="participation" lazy="false">
<key column="user_id"/>
<many-to-many column="group_id" class="modele.Group"/>
</set>
<!-- Mapping Set<Group> proprietaire -->
<set name="proprietaire" lazy="false" inverse="true" cascade="all">
<key column="user_id"/>
<one-to-many class="modele.Group"/>
</set>
<!-- Mapping DernierePosition dernierePosition -->
<one-to-one name="dernierePosition" class="modele.DernierePosition"
cascade="all" lazy="false"></one-to-one>
<!-- Mapping Set<Marqueur> marqueurs -->
<set name="marqueurs" lazy="false" inverse="true" cascade="all">
<key column="user_id"/>
<one-to-many class="modele.Marqueur"/>
</set>
</class>
</hibernate-mapping>
UserDao.java
Code:
public static User getUser(String telephone)
{
Session session = sessionFactory.openSession();
String query = "select u from User as u where u.telephone = :telephone";
User user = (User) session.createQuery(query)
.setString("telephone", telephone).uniqueResult();
session.close();
return user;
}
UserDaoTest.java
Code:
@Test
public void getGroupsTest()
{
User user1 = new User("telU1","u1","mail");
User prorio = new User("proprio","proprio","mail");
UserDao.addUser(user1);
UserDao.addUser(prorio);
prorio =UserDao.getUser(prorio.getTelephone());
user1 = UserDao.getUser(user1.getTelephone());
Group g1 = new Group("description", "#h",prorio,"password");
Group g2 = new Group("description", "#test",prorio,"password");
Group g3 = new Group("description", "#test2",prorio,"password");
Group g4 = new Group("description", "#test3",prorio,"password");
g1.addParticipation(user1);
g2.addParticipation(user1);
g3.addParticipation(user1);
g4.addParticipation(user1);
GroupDao.addGroup(g1);
GroupDao.addGroup(g2);
GroupDao.addGroup(g3);
GroupDao.addGroup(g4);
User user2 = UserDao.getUser(user1.getTelephone());
User pro = UserDao.getUser(prorio.getTelephone());
Set<Group> groups =user2.getGroups();
Set<Group> groups2 = pro.getProprietaire();
System.out.println(user2.getPseudo());
System.out.println(user2.getTelephone());
System.out.println(pro.getPseudo());
System.out.println(pro.getTelephone());
System.out.println(groups.size());
System.out.println(groups2.size());
}
GroupDao.java
Code:
public static int addGroup(Group group) {
int g = 0;
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
g = (Integer) session.save(group);
tx.commit();
} catch (HibernateException e) {
if (tx != null)
tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
return g;
}