-->
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.  [ 4 posts ] 
Author Message
 Post subject: Duplicate select results in when user is in multi-roles
PostPosted: Sun May 11, 2008 10:14 pm 
Newbie

Joined: Sun May 11, 2008 8:51 pm
Posts: 4
Location: Brisbane
I have a "User " model class, a "Role" model class, and a "Gene" model class which are maped to a user table, a role table, a user-role table and gene table

There are four roles (ROLE_ADMIN, ROLE_RESEARCHER and ROLE_COLLABORATOR and ROLE_PUBLIC.

A user may have multi-roles

User {
// private ......
private Set<Role> roles = new HashSet<Role>();
....



@ManyToMany(targetEntity = Role.class, fetch = FetchType.EAGER)
@JoinTable(name = "user_role",
joinColumns = { @JoinColumn(name = "user_id") },
inverseJoinColumns = { @JoinColumn(name = "role_id") })
public Set<Role> getRoles() {
return roles;
}

public void setRoles(Set<Role> roles) {
this.roles = roles;
}

......
}

Gene implements Serializable {
..........
private User

@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="user_id", nullable=false)
@org.hibernate.annotations.ForeignKey(name="FK_GENE_FROM_USER")
public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}


A generic Dao, hibernateDao, generic manager and managerImpl implementation were used. fetch in User and Gene class are set to FetchType.EAGER.

There are two users in User table. user A with id=1 has four roles , user B with id=2 has only one role (ROLE_RESEARCHER).
In the Gene table, there are 339 records all with user_id=1,

The geneManager.getAll(); it show 1356 records, which is 4x339 records. However, if just smiply set user_id=2 in the Gene table, the function will get the right 339 records.

or keep the user_id=1 and just simply set the FetchType to FETCHTYPE.LAZY in the Gene Class, the function will get the right 339 records.


Any one can give some suggestion? Is this a bug in Hibernate?


Top
 Profile  
 
 Post subject: DistinctRootEntityResultTransformer is needed
PostPosted: Sun May 11, 2008 11:11 pm 
Newbie

Joined: Sun May 11, 2008 8:51 pm
Posts: 4
Location: Brisbane
To this problem, a DistinctRootEntityResultTransformer is needed


Top
 Profile  
 
 Post subject: question
PostPosted: Tue May 13, 2008 7:22 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
what is the code in geneManager.getAll()?


Top
 Profile  
 
 Post subject: Re: question
PostPosted: Wed May 14, 2008 8:40 pm 
Newbie

Joined: Sun May 11, 2008 8:51 pm
Posts: 4
Location: Brisbane
sagimann wrote:
what is the code in geneManager.getAll()?


If you have used the "GenericDao" in the books of "Java Persistence and with Hibernate". you can find in the GenericDaoHibernate. The code is as following:

/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
public List<T> getAll() {

// return super.getHibernateTemplate().loadAll(this.persistentClass);
List<T> list =super.getHibernateTemplate().loadAll(this.persistentClass);
return list;
}

I have change the code as following:

/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
public List<T> getAll() {

// return super.getHibernateTemplate().loadAll(this.persistentClass);
List<T> list =super.getHibernateTemplate().loadAll(this.persistentClass);
DistinctRootEntityResultTransformer transformer = new DistinctRootEntityResultTransformer();
list = transformer.transformList(list);
return list;
}


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