JPA manyToMany双向映射尝试使用不存在的表

时间:2019-03-16 22:11:48

标签: jpa eclipselink

双向ManyToMany映射使用的是EMPLOYEE_PROJECT关系表,而不是映射中描述的EMP_PROJ表。 EMPLOYEE_PROJECT在数据库上不存在。 Eclipselink版本是2.7.3,jpa版本是2.2

@Entity

public class Employee extends Base implements Serializable {
/** explicit set serialVersionUID */
private static final long serialVersionUID = 1L;

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

@ManyToMany
@JoinTable(name = "EMP_PROJ", joinColumns = @JoinColumn(name = "EMP_ID"), inverseJoinColumns = @JoinColumn(name = "PROJ_ID"))
private List<Project> projects;

public List<Project> getProjects() {
    return projects;
}

public void setProjects(List<Project> projects) {
    this.projects = projects;

项目类

@Entity
public class Project extends Base implements Serializable {
    /** explicit set serialVersionUID */
    private static final long serialVersionUID = 1L;

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

    @ManyToMany(mappedBy="projects")
    Set<Employee> employees = new HashSet<Employee>();

问题是查询运行时

@Test
public void save_employee(){
    EntityManager em = emf.createEntityManager();
    Employee emp1 = em.find(Employee.class, new Integer(1));

Project p =  new Project();
p.setName("Name1");
p.getEmployees().add(emp1);
p = em.merge(p);

assertNotNull(p);

Employee emp2 = em.find(Employee.class, new Integer(1));
assertEquals(1, emp2.getProjects().size());
em.close();

}

以下例外:

Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.7.3.v20180807-4be1041): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.h2.jdbc.JdbcSQLException: Table "EMPLOYEE_PROJECT" not found; SQL statement:
SELECT t1.ID, t1.DESCRIPTION, t1.NAME, t1.VERSION FROM EMPLOYEE_PROJECT t0, PROJECT t1 WHERE ((t0.Employee_ID = ?) AND (t1.ID = t0.projects_ID)) [42102-197]
Error Code: 42102

Base.java代码如下

@MappedSuperclass
public class Base {

    protected int id;
    protected int version;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    @Version
    public int getVersion() {
        return version;
    }
    public void setVersion(int version) {
        this.version = version;
    }
}

0 个答案:

没有答案