Personal summary in the use of mysql and spring data jpa

Based on spring boot

1. Automatically create mysql data tables

When the writing of the entity class is completed , the related data table can be created automatically.

add in config file

# Hibernate ddl auto (create, create-drop, update) 
# create will recreate the related tables each time it starts 
# create-drop is created when it starts, and destroyed when it ends 
# update only updates the uncreated tables 
spring.jpa.hibernate. ddl-auto = create
 #Specify the engine used by the data table, the default is MyISAM 
spring.jpa.database-platform = org.hibernate.dialect.MySQL5InnoDBDialect

Do not use the build environment again

2. Creating an entity is adding the creation time, and updating the time when updating

    private Date createTime;
    private Date updateTime;

    @PrePersist
    protected void onCreate() {
        createTime = new Date();
    }

    @PreUpdate
    protected void onUpdate() {
        updateTime = new Date();
    }

 

3、one to one

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "org_id")
    private Org org;

    @Column(name = "org_id", insertable = false, updatable = false)
    private Long orgId;
fetch can be LAZY or EAGER. LAZY means lazy loading and will not actively load. It is recommended to use LAZY in most cases to avoid some performance problems, such as recursive loading problems. 
If you only use OneToOne, you cannot get the id of the associated object. You must load it. You can use @Column(name = "org_id", insertable = false, updatable = false) to get the corresponding id;

OneToOne ManyToMany, etc. use lazy loading method, when spring boot performs json serialization, it will throw the exception of com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer .
You can add the @JsonIgnoreProperties annotation to the entity class to ignore the serialization of related properties.

4. When the lazy loading exception
directly obtains the attributes of the LAZY annotation of the entity object, it will throw org.hibernate.LazyInitializationException: ..., could not initialize proxy - no Session.
Simply put, the context of the query has been closed, and then go to The query will throw an exception. There are several solutions. You can refer to https://vladmihalcea.com/the-hibernate-enable_lazy_load_no_trans-anti-pattern/
  1. Add configuration (this method is not recommended)
  spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
  2. Use Transactional
...
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "org_id")
private Org org;
...
@ManyToMany(mappedBy = "managedOrgs", fetch = FetchType.LAZY)
private List<User> managers = new ArrayList<>();
...


@Transactional
    public List<User> getDirectSuperiors(User user) {
        User user = userRepository.findById(user.getId());
        Org org = new Org(optionalUser.get().getOrgId());
        return org.getManagers();
    }

  Getting lazy loaded properties in a transaction will not throw an exception.

  3. Use sql methods such as NativeQueries to realize complex queries.

 

Not finished. . .



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324767728&siteId=291194637