Many to Many Invalid Object name

杀马特。学长 韩版系。学妹 提交于 2019-12-23 05:51:46

问题


I have a Many-To-Many relationship created between Employee-Project in my code, but when I want to use its throws this exception:

com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'employee_project'.

I tried, but I did not succeed finding the root cause of this exception. SO, please help me.

Below are the POJOs for Employee and Project and also the code that throws this exception

Employee 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);
}

Project 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);
}

Problem code:

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

I am using Spring Data Jpa and SQL Server. : Here is a picture of my current database


回答1:


So, I solved the problem myself. Apparently, if I add an underscore inside @JoinTable(Employee_Project) and also change that in database its works.

Can somebody please explain why I had to do this?



来源:https://stackoverflow.com/questions/50334319/many-to-many-invalid-object-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!