如何使用 hibernate 进行连接
我有一个关于使用 hql 加入两个实体之间的问题。
从用户配置文件开始,我必须检索与该用户配置文件关联的角色的名称。所以我需要进行连接才能将其放入角色表中。
我有以下类 UserProfile :
@Entity
@Table(name="USER_PROFILES")
public class UserProfile extends VersionedObject implements Serializable {
public static final int CAID_LENGTH = 64;
@Column(nullable=false, length=CAID_LENGTH)
private String caid;
@Column(length=255)
private String description;
@CollectionOfElements
@JoinTable(
name="USER_ROLES",
joinColumns={
@JoinColumn(name="user_id", referencedColumnName="id"),
@JoinColumn(name="user_v_no", referencedColumnName="v_no")
})
@Column(name="role_id")
private Set<Integer> roleIds;
这是 Role.java 类:
@Entity
@Table(name="ROLES")
public class Role extends VersionedObject implements Serializable {
@Column(nullable=false, length=255)
private String name;
@Column(length=255)
private String description;
@CollectionOfElements
@JoinTable(
name="ROLES_RIGHTS",
joinColumns={
@JoinColumn(name="role_v_no", referencedColumnName="v_no"),
@JoinColumn(name="role_id", referencedColumnName="id")
})
private Set<Right> rights;
你知道如何基于 private Set
来检索关联的角色以检索名称?
非常感谢 :)
I have a question regarding join between 2 entities using hql.
Starting on a userProfile, I have to retrieve the name of the role associated to this userProfile. So I need to make a join in order to get it in the role table.
I have the following class UserProfile :
@Entity
@Table(name="USER_PROFILES")
public class UserProfile extends VersionedObject implements Serializable {
public static final int CAID_LENGTH = 64;
@Column(nullable=false, length=CAID_LENGTH)
private String caid;
@Column(length=255)
private String description;
@CollectionOfElements
@JoinTable(
name="USER_ROLES",
joinColumns={
@JoinColumn(name="user_id", referencedColumnName="id"),
@JoinColumn(name="user_v_no", referencedColumnName="v_no")
})
@Column(name="role_id")
private Set<Integer> roleIds;
Here is the Role.java class:
@Entity
@Table(name="ROLES")
public class Role extends VersionedObject implements Serializable {
@Column(nullable=false, length=255)
private String name;
@Column(length=255)
private String description;
@CollectionOfElements
@JoinTable(
name="ROLES_RIGHTS",
joinColumns={
@JoinColumn(name="role_v_no", referencedColumnName="v_no"),
@JoinColumn(name="role_id", referencedColumnName="id")
})
private Set<Right> rights;
Do you know how can I make a join based on the private Set<Integer> roleIds
within the userProfile.java to retrieve the associated role in order to retrieve the name?
Thank you very much :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想说你代表的是你的表,而不是代表现实世界的对象关系。我的意思是,您不应该映射角色的 ID,而应该映射实际的角色类。
因此,不要映射此:
尝试映射此:
您对
Set的操作正确;权限
,因此,不确定为什么您映射 ID 而不是Set中的对象角色 ID
。之后,您应该能够简单地执行
userProfile.roles
,检索完整的角色集,包括其名称。I'd say that you are representing your tables, instead of representing real world object relationships. By that, I mean that you should not map the ID of Roles, but the actual Role class.
So, instead of mapping this:
Try to map this:
You did correctly for the
Set<Right> rights
, so, not sure why you mapped the IDs instead of objects inSet<Integer> roleIds
.After that, you should be able to simply do
userProfile.roles
, retrieving a full set of Roles, including its names.