多对多无效对象名称

时间:2018-05-14 15:47:05

标签: java hibernate jpa orm many-to-many

我的代码中的Employee-Project之间创建了多对多关系,但是当我想使用它时抛出此异常:

com.microsoft.sqlserver.jdbc.SQLServerException:无效的对象名称' employee_project'。

我试过,但我没有成功找到此异常的根本原因。所以,请帮助我。

以下是Employee和Project的POJO以及抛出此异常的代码

员工pojo:

@Entity
@Table(name = "Employee")
public class Employee {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "EMPLOYEE_ID_PK")
private int employeeIdPk;

@ManyToMany(fetch = FetchType.LAZY,  cascade = {
        CascadeType.PERSIST,
        CascadeType.MERGE
        },
        mappedBy = "workers")
private Collection<Project> projects = new HashSet<Project>(0);
}

项目pojo:

@Entity
@Table(name = "Project")
public class Project {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "PROJECT_ID_PK")
private int projectIdPk;

@ManyToMany(fetch = FetchType.LAZY, cascade = {
        CascadeType.PERSIST,
        CascadeType.MERGE
    })
@JoinTable(name = "EmployeeProject", joinColumns = { 
        @JoinColumn(name = "PROJECT_ID_PK") }, 
        inverseJoinColumns = { @JoinColumn(name = "EMPLOYEE_ID_PK") })
Collection<Employee> workers = new HashSet<Employee>(0);
}

问题代码:

Project project = projectRepository.findByProjectIdPk(24);
Collection<Employee> employees = project.getWorkers();

我正在使用Spring Data Jpa和SQL Server。 : Here is a picture of my current database

1 个答案:

答案 0 :(得分:0)

所以,我自己解决了这个问题。显然,如果我在@JoinTable(Employee_Project)内添加下划线并在数据库中更改它的作用。

有人可以解释为什么我必须这样做吗?