Home > database >  Error creating bean with name 'entityManagerFactory' Invocation of init method No identifi
Error creating bean with name 'entityManagerFactory' Invocation of init method No identifi

Time:10-16

Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: com.example.demo.entities.courses.Course

Courses Class

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>

application pro

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

CodePudding user response:

You are missing id field in your entity

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

Make sure that import is javax.persistence.Id

CodePudding user response:

There is no field having @Id annotation defined in your entity class. The field annotated with @Id is the primary key of your SQL table.

Here is the correct code -

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;
}
  • Related