我正在使用Spring启动和JPA处理restfull Web服务,但我遇到了类似的问题
[错误]无法执行目标 org.springframework.boot:弹簧启动了Maven插件:1.5.3.RELEASE:运行 项目SpringRest上的(default-cli):发生异常时 运行。 null:InvocationTargetException:创建bean时出错 在类路径资源中定义的名称“entityManagerFactory” [组织/ springframework的/引导/自动配置/ ORM / JPA / HibernateJpaAutoConfiguration.class]: 调用init方法失败;嵌套异常是 org.hibernate.AnnotationException:@Column不允许在 @ManyToOne属性: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
答案 0 :(得分:2)
在私人部门的员工实体中,您正在使用@column和@joinColumn,这似乎是问题所在。看看你的实体,你应该在那里使用@JoinColumn而不是@Column。
答案 1 :(得分:1)
Hibernate不允许@Column
注释与@ManyToOne
一起使用。
如果要指定列名
,可以使用@JoinColumn
答案 2 :(得分:1)
由于错误说明建议您不应在@Column
媒体资源上添加@ManyToOne
注释。你需要@JoinColumn
。