我正在使用Spring Boot和JPA开发RESTful Web服务,但是遇到了以下问题:
[ERROR] 执行目标org.springframework.boot:spring-boot-maven-plugin:1.5.3.RELEASE:run (default-cli) 时出错,位于项目SpringRest中:运行时发生异常。空指针异常:创建EntityManagerFactory时出错,请参考[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class] 中的定义。初始化方法调用失败;嵌套异常信息为:@ManyToOne属性上不允许使用@Column(s)注解: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>
实体
@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
}
代码库
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