使用JPA创建外键关系?

时间:2018-04-21 20:12:14

标签: hibernate spring-boot jpa jaxb spring-data-jpa

我有2个型号。用户模型和目标模型。

目前,当他们使用我的测试数据进行持久化时,目标表中的user_id列为空。我想知道每个目标都与特定用户相关联。

理想情况下,我希望能够引用我的UserJPA对象并说出(让我获得与此用户ID相关的所有目标)

我正在加载此XML并使用JaxB将其转换为POJO

用户

/** User entity that will reference */
@Entity
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "user")
public class User {

  @Column(name="user_id")
  @Id @GeneratedValue private Long id;

  private String username;

  private String password;

  private String gender;

  private String phone;

  private String email;

  @ElementCollection
  @OneToMany(cascade = CascadeType.ALL)
  @XmlElementWrapper(name="goals")
  @XmlElement(name="goal")
  private List<Goal> assignedUserGoals;

  public String getUserName() {
    return username;
  }

  public void setUserName(String username) {
    this.username = username;
  }

  public List<Goal> getAssignedUserGoals() {
    return assignedUserGoals;
  }

  public void setAssignedUserGoals(List<Goal> assignedUserGoals) {
    for (Goal g : assignedUserGoals) {
      g.setUser(this);
      addUserGoal(g);
    }
  }

  public void addUserGoal(Goal goal) {
    this.assignedUserGoals.add(goal);
  }

  public String getEncryptedPassword() {
    return password;
  }

  public void setEncryptedPassword(String encryptedPassword) {
    this.password = encryptedPassword;
  }

  public String getGender() {
    return gender;
  }

  public void setGender(String gender) {
    this.gender = gender;
  }

  public String getPhone() {
    return phone;
  }

  public void setPhone(String phone) {
    this.phone = phone;
  }

  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }

  @Override
  public String toString() {
    return this.getClass().getSimpleName();
  }
}

目标

/** Goal entity that will reference */
@Entity
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "goal")
public class Goal {

  @Id
  @GeneratedValue
  private Long id;

  private String title;

  @ManyToOne
  @JoinColumn(name = "user_id")
  private User user;

  private String description;

  @XmlElement(name="accountable")
  private String accountability;

  private String interval;

  public String getTitle() {
    return title;
  }

  public User getUser() {
    return this.user;
  }

  public void setUser(User user) {
    this.user = user;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  public String getAccountability() {
    return accountability;
  }

  public void setAccountability(String accountability) {
    this.accountability = accountability;
  }

  public String getInterval() {
    return interval;
  }

  public void setInterval(String interval) {
    this.interval = interval;
  }

  @Override
  public String toString() {
    return this.getClass().getSimpleName();
  }
}

用户XML通过JaxB

运行
<?xml version='1.0' encoding='UTF-8'?>
<UserContainer>
  <user>
    <username>user_1</username>
    <goals>
      <goal>
        <title>goal title 2</title>
        <description>
          Hi, this is my goal description.
        </description>
        <accountable>
          testeruser@gmail.com
        </accountable>
        <interval>daily</interval>
      </goal>
    </goals>
    <password>encrypted_password1</password>
    <gender>male</gender>
    <phone>4805551111</phone>
    <email>user1@gmail.com</email>
  </user>
  <user>
    <username>user_2</username>
    <password>encrypted_password2</password>
    <gender>female</gender>
    <phone>4805551111</phone>
    <email>user2@gmail.com</email>
  </user>
  <user>
    <username>user_3</username>
    <password>encrypted_password3</password>
    <gender>trans</gender>
    <phone>1113334454</phone>
    <email>user3@gmail.com</email>
  </user>
</UserContainer>

0 个答案:

没有答案