N.Y. N.Y. - 1 year ago 228
Java Question

Spring Boot EntityManagerFactory not defined and @ManyToOne Not Allowed

I am working on restfull web services with Spring boot and JPA but I have some problems like that


[ERROR] Failed to execute goal
org.springframework.boot:spring-boot-maven-plugin:1.5.3.RELEASE:run
(default-cli) on project SpringRest: An exception occurred while
running. null: InvocationTargetException: Error creating bean with
name 'entityManagerFactory' defined in class path resource
[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]:
Invocation of init method failed; nested exception is
org.hibernate.AnnotationException: @Column(s) not allowed on a
@ManyToOne property: com.maven.nurcanyilmaz.models.Employee.department


pom.xml

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- JSTL for JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>

<!-- For JSP compilation -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>


entities

@Entity
@Table(name="DEPARTMENT")
public class Department {

@Id
@Column(name="Id",nullable=false,unique=true)
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@Column(name="Description",nullable=false)
private String description;

@OneToMany(mappedBy = "department", cascade = CascadeType.ALL)
private List<Employee> employees;

public Department() {
// TODO Auto-generated constructor stub
}

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

@Id
@Column(name="Id",nullable=false,unique=true)
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;

@Column(name="Name",nullable=false)
private String name;

@Column(name="Surname",nullable=false)
private String surname;
@Column(name="Salary",nullable=false)
private double salary;

@Column(name="Department" , nullable=false)
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "Id",referencedColumnName="Id")
private Department department;

public Employee() {
// TODO Auto-generated constructor stub
}


repositories

import com.maven.nurcanyilmaz.models.Employee;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

@RepositoryRestResource
public interface EmployeeRepository extends CrudRepository<Employee, Long> {

Employee findByName(String name);

}


import org.springframework.data.repository.CrudRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import com.maven.nurcanyilmaz.models.Department;

@RepositoryRestResource
public interface DepartmentRepository extends CrudRepository<Department, Long> {

Department findByName(String name);
}


application.properties

spring.mvc.view.prefix: /WEB-INF/
spring.mvc.view.suffix: .jsp
server.port=8088

spring.datasource.url = jdbc:mysql://localhost:3306/EmployeeDb
spring.datasource.username =root
spring.datasource.password =test
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

# Show or not log for each sql query
spring.jpa.show-sql = true

# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = create-drop

spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy


spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
server.error.whitelabel.enabled=false

Answer Source

In Employee Entity for private department department, You are using @column and @joinColumn together which seems to be the problem. Looking at your entities you are supposed to use @JoinColumn there but not @Column.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download