英文:
Hibernate load another column related to a foreign key
问题
我有这两个关系:
ACTION(ID, AMOUNT, BY)
USER(ID, NAME)
//BY是一个引用USER(ID)的外键
在我的JAVA项目中,我有类似这样的东西:
@Entity
@Table("action")
class Action{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "amount")
double amount;
@Column(name="by")
int userId;
@???
String userName;
}
和
@Entity
@Table("user")
class User{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "name")
String name;
}
我想知道如何链接我的Action类,以便它加载由“by”(userId)列引用的正确的“userName”?
提前感谢!
英文:
I am having these two relations:
ACTION(ID, AMOUNT, BY)
USER(ID, NAME)
//BY is a foreign key that references USER(ID)
In my JAVA project I am having something like this:
@Entity
@Table("action")
class Action{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "amount")
double amount;
@Column(name="by")
int userId;
@???
String userName;
}
and
@Entity
@Table("user")
class User{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "name")
String name;
}
What I want to know is how to link my Action class so that it loads the correct userName
which is referred by by
(userId) column?
Thanks in advance!
答案1
得分: 1
你可以在Action类中添加User
,并使用by
注解@JoinColumn
。
class Action{
...
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "by")
User user;
}
class User{
...
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private List<Action> actions;
}
英文:
You can add User
in Action class and @JoinColumn with by
class Action{
...
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "by")
User user;
}
class User{
...
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private List<Action> actions;
}
答案2
得分: 1
You're already keeping the userId
information in your Action
class - so either remove the userName field, and when you need that information fetch it separately based on the userId. So your Repository or DAO class can expose a method named: findUsernameBy(int userId)
returning a String.
你已经在你的 Action
类中保留了 userId
信息 - 所以要么删除 userName
字段,在需要该信息时根据 userId
单独获取它。因此,你的 Repository 或 DAO 类可以暴露一个名为 findUsernameBy(int userId)
的方法,返回一个字符串。
Another way is to implement OneToOne relation with a user entity:
另一种方法是与用户实体实现一对一关系:
@Entity
@Table(name = "action")
class Action {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "amount")
double amount;
@OneToOne
@JoinColumn(name = "user_id")
private User user;
}
英文:
You're already keeping the userId
information in your Action
class - so either remove the userName field, and when you need that information fetch it separately based on the userId. So your Repository or DAO class can expose a method named: findUsernameBy(int userId)
returning a String.
Another way is to implement OneToOne relation with a user entity:
@Entity
@Table("action")
class Action{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "amount")
double amount;
@OneToOne
@JoinCoumn(name="by")
private User user;
}
答案3
得分: 1
如果您的用户拥有多个操作,请使用以下方式替换:
@Column(name = "by")
int userId;
@ManyToOne
@JoinColumn(name = "by")
private User user;
您还可以添加以下内容:
// 级联以将所需的操作传播到多侧
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private List<Action> actions;
将这些代码添加到您的 User
实体中,以实现双向映射。因此,可以列出所有用户的操作。除了添加级联和将操作添加到用户操作之外,您还需要设置操作的用户,否则保存将无法正常工作。换句话说,您需要正确设置映射的两端。
英文:
If you have many actions per user, replace:
@Column(name="by")
int userId;
@???
String userName;
with
@ManyToOne
@JoinColumn(name = "by")
private User user;
You can also add:
// Cascade to propagate wanted actions to many side
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private List<Action> actions;
to your User
entity to have birectional mapping. So to list all Users actions. In addition to adding to cascade and adding action to users actions you need to set action's user or saving will not work correctly. In other words you need to set both ends of mapping correctly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论