-->
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.  [ 10 posts ] 
Author Message
 Post subject: help write a criteria code
PostPosted: Fri Sep 29, 2006 12:41 pm 
Newbie

Joined: Fri Sep 29, 2006 12:36 pm
Posts: 6
I have 3 classes. User, RoleRef, Role

Code:
public class User {

    public void setId(int id) {
        ...
    }

    public int getId() {
        return ...;
    }

}


Code:
public class Role {

    public void setId(int id) {
        ...
    }

    public int getId() {
        return ...;
    }

}


Code:
public class RoleRef {

    private User user;
    private Role role;

    public User getUser() {
        return user;
    }

    public Role getRole() {
        return role;
    }

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

    public void setRole(Role role) {
        this.role = role;
    }

    public boolean equals(Object o) {
        if(o == this) {
            return true;
        } else if(o == null) {
            return false;
        } else if(o instanceof RoleRef) {
            RoleRef referrence = (RoleRef) o;
            return user.equals(referrence.user) && role.equals(referrence.role);
        } else {
            return false;
        }
    }

}




The mapped tables are respective role, roleref, user.

Now assume that I have an instance of User, how can I write criteria code which is the same as sql expression as below.

"select role.* from user, roleref, role where user.id = roleref.uid && roleref.rid = role.id".


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 29, 2006 8:55 pm 
Newbie

Joined: Fri Sep 29, 2006 12:36 pm
Posts: 6
Can anyone help me? thanks;


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 29, 2006 11:00 pm 
Newbie

Joined: Thu Sep 28, 2006 12:27 pm
Posts: 13
You want all Role-Objects for a given User, right?

Why don't you take the easy approach then and map a Set of RoleRef-Objects into your User and your Role Classes?

That way you could iterate over the RoleRef-Set in your User-Object and get all the Role-Objects out of them, without having to use Criteria at all.

BTW: If your RoleRef-Table doesnt contain anything besides the primary key columns of the User and the Role table then it shouldn't be neccessary
to create an extra Class for it. Please refer to the official documentation (1.3.1 - 1.3.6) for an example.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 29, 2006 11:14 pm 
Newbie

Joined: Fri Sep 29, 2006 12:36 pm
Posts: 6
HSchmidt wrote:
You want all Role-Objects for a given User, right?

Why don't you take the easy approach then and map a Set of RoleRef-Objects into your User and your Role Classes?

That way you could iterate over the RoleRef-Set in your User-Object and get all the Role-Objects out of them, without having to use Criteria at all.

BTW: If your RoleRef-Table doesnt contain anything besides the primary key columns of the User and the Role table then it shouldn't be neccessary
to create an extra Class for it. Please refer to the official documentation (1.3.1 - 1.3.6) for an example.


many thanks for your reply.

I was considering removing RoleRef class, but there is an another special need so that I cannot remove it.

Now I haven't understood your meaning yet. Could you give me some sample code?

Of course, maybe HQL is more easy. But what I want is just to learn how to solve assosiation between multiple tables in Criteria way.
best regards


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 29, 2006 11:52 pm 
Newbie

Joined: Thu Sep 28, 2006 12:27 pm
Posts: 13
Code:
public class User {

    private Set RoleRefs = new HashSet();

    public void setId(int id) {
        ...
    }

    public int getId() {
        return ...;
    }

    public Set getRoleRefs()
    {
        return this.RoleRefs;
    }

    public void setRoleRefs(set rrefs)
    {
        this.RoleRefs = rrefs;
    }

}


Code:
public class Role {

    private Set RoleRefs = new HashSet();

    public void setId(int id) {
        ...
    }

    public int getId() {
        return ...;
    }

    public Set getRoleRefs()
    {
        return this.RoleRefs;
    }

    public void setRoleRefs(set rrefs)
    {
        this.RoleRefs = rrefs;
    }

}


Class RoleRef remains the same.

Code:
//The "query"
User someUser = //Get User-Object from somewhere
List allRolesForUser = new ArrayList();
for(int foo = 0; foo < someUser.getRoleRefs.size(); foo++)
{
    allRolesForUser.add( ((RoleRef)someUser.getRoleRefs().get(foo)).getRole());
}


In your Class-mappings you'd have to include the sets so that Hibernate actually knows about them.

Note: It's really late here and I hope my postings still make sense...
Good night


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 30, 2006 1:02 am 
Newbie

Joined: Fri Sep 29, 2006 12:36 pm
Posts: 6
thanks,

but how can i do if the user dosen't have any referrence to roleref?

In otherwords, if I want to get all roles from the specified user 'U', I'll do all step as below:

    First, I must look up all roleRefs whose user is the 'U'. Notice that this step must be done in roleref table not user table
    Then secode, I should iterate all roleRefs to get the relative role

IF fetching them from database in query way, we may use subquery or assosiation. To learn Hibernate Criteria, I want to know how to fetch them with Criteria way.

I don't know how to insert image here, otherwise i can give you my table E-R diagram.

have you ICQ or MSN? If you do, it sounds better


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 30, 2006 8:03 pm 
Newbie

Joined: Fri Sep 29, 2006 12:36 pm
Posts: 6
Can anyone help me?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 02, 2006 1:34 am 
Newbie

Joined: Fri Sep 29, 2006 12:36 pm
Posts: 6
up


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 02, 2006 2:30 am 
Beginner
Beginner

Joined: Tue Oct 18, 2005 3:44 am
Posts: 27
Don't forget to implement the hashCode() method when you override the equals() method.
Greetz
Cathy


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 02, 2006 3:06 am 
Beginner
Beginner

Joined: Sun Jan 22, 2006 6:56 am
Posts: 29
NakoRuru wrote:
thanks,

but how can i do if the user dosen't have any referrence to roleref?

In otherwords, if I want to get all roles from the specified user 'U', I'll do all step as below:

    First, I must look up all roleRefs whose user is the 'U'. Notice that this step must be done in roleref table not user table
    Then secode, I should iterate all roleRefs to get the relative role
IF fetching them from database in query way, we may use subquery or assosiation. To learn Hibernate Criteria, I want to know how to fetch them with Criteria way.

I don't know how to insert image here, otherwise i can give you my table E-R diagram.

have you ICQ or MSN? If you do, it sounds better


Whether you're using HQL or Criteria, you need to map the associations so hibernate knows about them. What I'm guessing you're looking for is a many-to-many assoc. between user and role (more precisely one-to-many from user to role-ref and many-to-one from role-ref to role).

These associations can be bi-directional in java even if the query is done from the role-ref table. See the documentation about a many-to-many association using a link table - this looks like the classic example.

After you have the associations mapped you can use Criteria on the association.


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