简体   繁体   English

如何在DAO中检索关联数据?

[英]how to retrieve association data in DAO?

My question is about how to retrieve association data in DAO. 我的问题是关于如何在DAO中检索关联数据。

I have 3 tables: user, role and user_role. 我有3个表:user,role和user_role。 user_role is the association table of user and role. user_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 . 我有2个课程: UserRole User has a roles property which is List of objects of Role type. User具有一个roles属性,它是Role类型的对象列表。

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. 现在,我正在尝试构建DAO,并对如何填充User类的role属性感到困惑。

I think I need these DAO methods for User class: 我想我需要User类的这些DAO方法:

1) User getUserMethod1(String userId) - retrieve a row from user table for a specific user_id. 1) User getUserMethod1(String userId) -从用户表中为特定的user_id检索一行。 Dont populate roles property of User class. 不要填充User类的roles属性。

2) User getUserMethod2(String userId) - retrieve a row from user table for a specific user_id. 2) User getUserMethod2(String userId) -从用户表中为特定的user_id检索一行。 Also populate roles property of User class. 还填充User类的roles属性。

3) void updateUser(String userId) - update a row in user table for a specific user_id. 3) void updateUser(String userId) -在用户表中为特定的user_id更新一行。

4) void deleteUser(String userId) - delete a row in user table for a specific user_id. 4) void deleteUser(String userId) -删除用户表中特定user_id的行。

I think I need these DAO methods for Role class: 我认为Role类需要这些DAO方法:

1) Role getRole(int roleId) - retrieve a row from role table for a specific role_id. 1) Role getRole(int roleId) -从角色表中为特定的role_id检索一行。

2) List<Role> getAllRoles() - retrieve all rows from role table. 2) List<Role> getAllRoles() -从角色表中检索所有行。

3) void updateRole(int roleId) - update a row in role table for a specific role_id. 3) void updateRole(int roleId) -为特定的role_id更新角色表中的一行。

4) void deleteRole(int roleId) - delete a row in role table for a specific role_id. 4) void deleteRole(int roleId) -为特定的role_id删除角色表中的一行。


Out of the above 8 DAO methods I am having issue with User getUserMethod2(String userId) method. 在上述8种DAO方法中, User getUserMethod2(String userId)方法存在问题。

I can retrieve 1 row from user table for the specific user_id; 我可以从用户表中为特定的user_id检索1行; then retrieve all role_id associated to that specific user_id from user_role table. 然后从user_role表中检索与该特定user_id相关联的所有role_id。 But then do I have to loop through them and call Role getRole(int roleId) method of Role class for each role_id? 但后来我通过他们必须循环,并呼吁Role getRole(int roleId)的方法, Role类各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? 有没有更好的办法来解决这个问题,然后调用Role getRole(int roleId)的方法, Role类各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. 如果您正确设置了hibernate.cfg,则必须具有一对多关联才能在用户类中关联您的设置。

        <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. 对于问题2:您可以在休眠查询中使用"fetch"来指定集合的​​紧急加载。

You can use criteria API or HQL to handle cases like to bring users who have no roles associated with them. 您可以使用标准API或HQL来处理各种情况,例如带走没有与其关联的角色的用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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