如何使用JPA Hibernate注释创建ManyToOne关系?

时间:2017-03-30 12:16:57

标签: java mysql hibernate jpa javabeans

我有两个名为Task和Team的POJO类。一个团队可以相应地执行许多任务,但每个任务只能映射一个团队。

// Task POJO
@Entity
@Table(name = "Task")
public class Task extends Domain implements Serializable {

@GeneratedValue(strategy = GenerationType.AUTO)
@Id
private Long taskId;

//Mapping To Team 
@ManyToOne
@JoinColumn(name="teamId")
private Team teamId;

POJO团队

@Entity
@Table(name = "Team")
public class Team extends Domain implements Serializable {

@GeneratedValue(strategy = GenerationType.AUTO)
@Id
private Long teamId;
@Column(nullable = false)

@OneToMany(mappedBy="teamId")
private List<Task> task = new ArrayList<Task>();

问题是我无法映射/保存/更新,这不会影响我的Team Pojo。

  //Task Table
  +-----------------+--------------+------+-----+---------+----------------+
  | Field           | Type         | Null | Key | Default | Extra          |
  +-----------------+--------------+------+-----+---------+----------------+
  | taskId          | bigint(20)   | NO   | PRI | NULL    | auto_increment |
  | taskName        | varchar(255) | NO   |     | NULL    |                |
  | teamId          | bigint(20)   | YES  | MUL | NULL    |                |
  +-----------------+--------------+------+-----+---------+----------------+

团队表

 +----------+--------------+------+-----+---------+----------------+
 | Field    | Type         | Null | Key | Default | Extra          |
 +----------+--------------+------+-----+---------+----------------+
 | teamId   | bigint(20)   | NO   | PRI | NULL    | auto_increment |
 | teamName | varchar(255) | NO   |     | NULL    |                |
 +----------+--------------+------+-----+---------+----------------+

那么如何映射这两个Pojo类,并在表中保存/删除数据时产生影响。

先谢谢。

3 个答案:

答案 0 :(得分:1)

我发现你已经在一对多关联属性(任务)中定义了@Column,我认为这是一个类型错误。

对于双向一对多关联,更新关系的一方,另一方也应更新,并保持同步。下面显示了如何使两个实体同步:

@Entity
@Table(name = "Task")
public class Task extends Domain implements Serializable {

    @GeneratedValue(strategy = GenerationType.AUTO)
    @Id
    private Long taskId;

    //Mapping To Team 
    @ManyToOne
    @JoinColumn(name="teamId")
    private Team team;

    public void setTeam(Team team) {
        this.team = team;
        if (!team.getTasks().contains(this)) { 
            team.getTasks().add(this);
        }
    }

}

@Entity
@Table(name = "Team")
public class Team extends Domain implements Serializable {

    @GeneratedValue(strategy = GenerationType.AUTO)
    @Id
    private Long teamId;
    @Column(nullable = false)

    @OneToMany(mappedBy="team")
    private List<Task> tasks = new ArrayList<Task>();

    public void addTask(Task task) {
        this.tasks.add(task);
        if (task.getTeam() != this) {
            task.setTeam(this);
        }
    }
}

答案 1 :(得分:0)

@OneToMany(mappedBy =&#34; teamId&#34;,cascade = CascadeType.ALL),尝试将此注释添加到类Team

的成员(属性)任务之上

答案 2 :(得分:0)

通过在Team实体中添加引用的列,在Task实体中完成@manyToOne映射;并将该成员命名为“团队”,而不是“teamId”,即:

@ManyToOne
@JoinColumn(name="teamId",
   referencedColumnName="teamId")
private Team team;

要了解JPA中的关系映射,请参阅:https://en.wikibooks.org/wiki/Java_Persistence/Relationships#Nested_Collections.2C_Maps_and_Matrices