@OneToMany映射和使用相同类型实体创建JOIN表失败,JPA

时间:2017-03-28 14:52:48

标签: java hibernate jpa

我有一个Employee课程。 Worker类扩展EmployeeLeader类扩展Worker

每个Leader都有一个列表public List workersResponsibleFor = new ArrayList<>();。我想创建一个Join表,以说明Leader是否对一个或多个Workers负责。

@Entity
@Inheritance
@DiscriminatorColumn(name="EMP_TYPE")
@Table(name="EMPLOYEES")
public abstract class Employee implements Printable, Serializable {

    @Id
    @GeneratedValue
    @Column(name = "employee_id", unique = true)
    private int id;

    @Column(name = "position")
    @Enumerated(EnumType.STRING)
    private Position position;

    @Column(name = "name")
    private String name;

    @Column(name = "salary")
    private Double salary;

    @Transient
    public List<Project> projectsWorkingOn = new ArrayList<>();

    public Employee() {
    }
}

Worker

@Entity
public class Worker extends Employee {

    @Id
    @GeneratedValue
    @Column(name = "worker_id", unique = true)
    private int id;

    public Worker() {
    }
}

Leader

@Entity
public abstract class Leader extends Worker {

    public Leader() {
    }

    public Leader(Position position, String name, Double salary) {
        super(position, name, salary);
    }

    @OneToMany
    @JoinTable(name="WORKERS_RESPONSIBLE_FOR",
            joinColumns = {@JoinColumn(name="responsible_for")})
    public List<Worker> workersResponsibleFor = new ArrayList<>();
}

我敢肯定,Leader类中的List注释是错误的,因为每次尝试使用它时我都会遇到很多错误。如何正确地对其进行注释,将责任收集到新的联结表中?

0 个答案:

没有答案