1

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

Sabin Antohe
  • 41
  • 1
  • 6
  • @Many to many relationship i use Embedded table in order to define it's own repository , i wasn't able to do it in this way – Basil Battikhi May 14 '18 at 15:49
  • @BasilBattikhi But I am not using any extra columns, why should I use an Embedded table? – Sabin Antohe May 14 '18 at 16:01
  • this is not the correct way. Follow the following link to see how manytomany mapping is done https://www.thoughts-on-java.org/hibernate-tips-map-bidirectional-many-many-association/ – sam May 14 '18 at 17:03

1 Answers1

0

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?

sam
  • 1,800
  • 1
  • 25
  • 47
Sabin Antohe
  • 41
  • 1
  • 6
  • Your scenario works fine in the Hibernate test suite, so I suspect it has something to do with Spring Data wrapper or the Spring boot naming strategy overrides they use that changes default Hibernate behavior. – Naros May 14 '18 at 18:03
  • 1
    @Naros, https://stackoverflow.com/questions/25283198/spring-boot-jpa-column-name-annotation-ignored/26546541#26546541 This might be the issue. – Sabin Antohe May 14 '18 at 20:17
  • Yep, we've had conflicting issues with the naming strategies being used by Spring. Its generally best to specifically override those to the defaults used by Hibernate unless you're upgrading a legacy application that requires old naming behavior. – Naros May 15 '18 at 12:48