创建在类路径资源[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]中定义的名为“entityManagerFactory”的Bean时出错:调用init方法失败;嵌套的异常错误是org. hib。没有为实体指定标识符:com.example.demo.entities.courses.Course
课程类别
package com.example.demo.entities.courses;
import java.math.BigDecimal;
import javax.persistence.*;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import com.example.demo.entities.BaseEntities;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Entity
@Table(name = "courses")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Course extends BaseEntities {
@NotNull
@NotBlank
@Column(name="title", length=100)
@Size(max=100, message ="Title must be less than 100 charachters")
private String title;
//public String getTitle() {
// // TODO Auto-generated method stub
// return null;
//}
@NotNull
@NotBlank
@Column(name="description", length=1000)
@Size(max=1000, message ="Title must be less than 1000 charachters")
private String description;
@NotNull(message ="Price must not be null")
@DecimalMin("1.00")
private BigDecimal price;
}
pom.xml文件
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
应用程序亲
spring.datasource.url=jdbc:mysql://localhost:3306/iAcademy? createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=pass
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
2条答案
按热度按时间mepcadol1#
您的实体中缺少ID字段
请确保导入为
javax.persistence.Id
py49o6xq2#
在您的实体类中没有定义具有
@Id
注解的字段。使用@Id
注解的字段是SQL表的主键。下面是正确的代码-