Some knowledge points of jpa

jpa is a persistence layer framework, formerly hibernate

Used in the project:

(1), introduction of pom.xml

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

(two). application.yml file configuration

#Environmental configuration dev is a newly created application- dev.yml 
spring: 
  profiles: 
    active: dev 
  jpa: 
    hibernate: 
# create - drop, every time the field is modified, the table will be emptied 
      ddl -auto: update

(3) Create an entity class

@Entity
public class Banner {
    // 这是表的主键
    @Id
    private String id;
    private String name;
    private String description;
    private String img;
    private String title;
}

Run the project, you can create a table corresponding to the entity class in the database

Remarks:

Sometimes encounter this error

org.springframework.beans.factory.BeanCreationException: 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.zb.missyou.model.Banner

This is because the primary key is not specified in the entity class, which is @Id in Banner

Guess you like

Origin www.cnblogs.com/zhaobao1830/p/12703764.html