0

I have 2 models. A user model and a goal model.

Currently, when they are persisted with my test data, the user_id column is null in the goal table. I want to know that every goal is associated with a particular user.

Ideally, I want to be able to have a reference to my UserJPA object and say (Get me all goals associated with this user ID)

I'm loading this XML and turning it into a POJO using JaxB

User

/** 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

/** 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();
  }
}

User XML ran through 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>
Ryan S
  • 155
  • 2
  • 16

0 Answers0