spring 创建名为'entityManagerFactory'的Bean时出错调用init方法没有为实体指定标识符

zpgglvta  于 2022-10-30  发布在  Spring
关注(0)|答案(2)|浏览(174)

创建在类路径资源[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
mepcadol

mepcadol1#

您的实体中缺少ID字段

@Id
    @Column(name="id")<---- this "id" value should match the id from your database
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

请确保导入为javax.persistence.Id

py49o6xq

py49o6xq2#

在您的实体类中没有定义具有@Id注解的字段。使用@Id注解的字段是SQL表的主键
下面是正确的代码-

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 {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="course_id")
private Integer courseId

@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;
}

相关问题