简体   繁体   中英

how to retrieve association data in DAO?

My question is about how to retrieve association data in DAO.

I have 3 tables: user, role and user_role. user_role is the association table of user and role.

create table user (user_id varchar(50) not null primary key, password varchar(50) not null)
create table role (role_id int not null primary key, role_name varchar(50) not null)
create table user_role (user_role_id int not null primary key, user_id varchar(50) not null, role_id int not null)

I have 2 classes: User and Role . User has a roles property which is List of objects of Role type.

public class User {
   private String userId;
   private String password;
   private List<Role> roles;

   public String getUserId() { return this.userId; }
   public void setUserId(String userId) { this.userId = userId; }
   public String getPassword() { return this.password; }
   public void setPassword(String password) { this.password = password; }
   public List<Role> getRoles() { return this.roles; }
   public void setRoles(List<Role> roles) { this.roles = roles; }
}

public class Role {
   private int roleId;
   private String roleName;

   public int getRoleId() { return this.roleId; }
   public void setRoleId(int roleId) { this.roleId = roleId; }
   public String getRoleName() { return this.roleName; }
   public void setRoleName(String roleName) { this.roleName = roleName; }
}

Now I am trying to build DAOs and getting confused on how to populate roles property of User class.

I think I need these DAO methods for User class:

1) User getUserMethod1(String userId) - retrieve a row from user table for a specific user_id. Dont populate roles property of User class.

2) User getUserMethod2(String userId) - retrieve a row from user table for a specific user_id. Also populate roles property of User class.

3) void updateUser(String userId) - update a row in user table for a specific user_id.

4) void deleteUser(String userId) - delete a row in user table for a specific user_id.

I think I need these DAO methods for Role class:

1) Role getRole(int roleId) - retrieve a row from role table for a specific role_id.

2) List<Role> getAllRoles() - retrieve all rows from role table.

3) void updateRole(int roleId) - update a row in role table for a specific role_id.

4) void deleteRole(int roleId) - delete a row in role table for a specific role_id.


Out of the above 8 DAO methods I am having issue with User getUserMethod2(String userId) method.

I can retrieve 1 row from user table for the specific user_id; then retrieve all role_id associated to that specific user_id from user_role table. But then do I have to loop through them and call Role getRole(int roleId) method of Role class for each role_id? If yes then how can I get a hold of that method? If not then what is the solution?

Is there a better way to solve this issue then calling Role getRole(int roleId) method of Role class for each role_id?

Thank you for reading this question.

If you have set your hibernate.cfg properly, you must have that one-to-many associations to associate your set in the user class.

        <set name="role" table="role" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="role_id" />
            </key>
            <one-to-many class="com.model.Role" />
        </set>

Anyways, if this has been done correctly, you can enable lazy loading to not bring sets and only bring them when you try and access them. Note that you will have to keep the session open for lazy loading to work.

For question 2: you can use "fetch" in your hibernate query to specify eager loading of your set.

You can use criteria API or HQL to handle cases like to bring users who have no roles associated with them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM