Hibernate加载与外键相关的另一列

huangapple go评论59阅读模式
英文:

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 = &quot;by&quot;)
  User user;
}

class User{
 ...
  @OneToMany(mappedBy = &quot;user&quot;, cascade = CascadeType.ALL)
  private List&lt;Action&gt; 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(&quot;action&quot;)
class Action{
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = &quot;id&quot;)
  private int id;

  @Column(name = &quot;amount&quot;)
  double amount;

  @OneToOne
  @JoinCoumn(name=&quot;by&quot;)
  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=&quot;by&quot;)
int userId;

@???
String userName;

with

@ManyToOne
@JoinColumn(name = &quot;by&quot;)
private User user;

You can also add:

// Cascade to propagate wanted actions to many side
@OneToMany(mappedBy = &quot;user&quot;, cascade = CascadeType.ALL)
private List&lt;Action&gt; 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.

huangapple
  • 本文由 发表于 2020年7月22日 02:08:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63020562.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定