This is the 13th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Spring Data JPA properties JdbcProperties and DataSourceProperties The first article is mainly JDBC configuration, this article is mainly JPA’s own configuration; In Spring code, spring Data JPA is configured under the Orm. JPA package and includes two configuration classes: JpaProperties and HibernateProperties.

  • JpaProperties: the prefix isspring.jpa.*
  • HibernateProperties: the prefix isspring.jpa.hibernate.*

JpaProperties

This configuration primarily serves the JPA EntityManagerFactory created by Spring; The specific attributes are as follows:

@ConfigurationProperties(prefix = "spring.jpa")
public class JpaProperties {
   private Map<String, String> properties = new HashMap<>();
   private final List<String> mappingResources = new ArrayList<>();
   private String databasePlatform;
   // The target database to operate on is automatically detected by default. You can also set it using the "databasePlatform" property.
   private Database database;
   // Whether to initialize the schema at startup
   private boolean generateDdl = false;
   // Whether to enable printing SQL statements, which is very common during testing
   private boolean showSql = false;
   / / used to decide whether or not registered OpenEntityManagerInViewInterceptor, it will be a request thread binding a JPA EntityManager
   private Boolean openInView;
Copy the code

database

Database property, you can see all database enumerated types through IDEA, as shown in the following figure:

In general, we do not need to specify the type explicitly; the default is dafault.

mappingResources

In the previous xmL-based configuration, multiple orm. XML was defined using the mappingResources attribute, roughly as follows:

<property name="mappingResources">
    <list>
        <value>sql/user.xml</value>
        <value>Sql/user1.xml</value>
    </list>
</property>
Copy the code

generateDdl

When DDL is enabled, DDLS that create the Schema are automatically generated from defined entities that initialize the database when the application is started. In a way, you don’t have to deal with SQL anymore.

Jpa.generateddl for JPA and Spring.jpa.hibernate.dcl-auto for Hibernate. Docs. Spring. IO/spring – the boot…

  • Spring.jpa. generate-DDl is a higher-level abstraction that has nothing to do with the concrete JPA implementation
  • Spring.jpa.hibernate.dcl-auto is a more specific setting related to Hibernate. The former will affect the latter, it is best not to mix the two, easy to confuse. In practice, just use the latter.

Spring.jpa.hibernate. dcl-auto: create, create-drop, None, update, validate, etc.

  • Create indicates that each time the application is started, all the previous tables are dropped and generated again based on the entity class.
  • Create-drop On the basis of create, the application is dropped once when it is closed.
  • Update is probably a common startup that looks at any changes to the entity class and then sees if the table structure needs to be changed.
  • Validate does not make changes to the table, but looks to see if it matches the entity class
  • None does nothing

Spring.jpa. generate-ddl when true, the default is update. org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter#buildJpaPropertyMap

if (isGenerateDdl()) {
   jpaProperties.put(AvailableSettings.HBM2DDL_AUTO, "update");
}
Copy the code

showSql

When this function is enabled, the EXECUTED SQL is output in logs, which is usually used in test scenarios

HibernateProperties

@ConfigurationProperties("spring.jpa.hibernate")
public class HibernateProperties {
   private final Naming naming = new Naming();
   private String ddlAuto;
   private Boolean useNewIdGeneratorMappings;
Copy the code

naming

Represents a naming strategy, and there are two modes available (Hibernate 5 is not available until later) :

  • ImplicitStrategy: Takes care of the model object level, processing the object model into logical names.
  • Physical-strategy: Is responsible for mapping to real data names, processing the above logical names into physical names

Implicit-strategy configuration items are used when @table and @column annotations are not used, and implicit-Strategy does not work when specified in the object model. Physical-strategy must be applied regardless of whether column names are explicitly specified or already implicitly determined in the object model.

For example, if the Entity has a field named phoneNumber and we expect its logical name to be phone_numer, the actual physical name in the database is p_num, then:

phoneNumber ----implicitStrategy---> phone_number
phone_number ----physicalStrategy---> p_num
Copy the code

useNewIdGeneratorMappings

Open means, can be specified as the primary key javax.mail. Persistence. The four types of GenerationType AUTO, TABLE, IDENTITY SEQUENCE generation strategy.

  • SEQUENCE

The sequence mechanism provided by the database is used to generate primary keys, and the database must support sequence. Such as ORACLE, DB, SAP DB, PostgerSQL, and McKoi sequence. A database such as MySQL, which does not support sequence, does not.

  • TABLE

Use a specific database table to hold the primary key. (Less used)

  • IDENTITY

The primary key is automatically generated by the database

  • AUTO

Depends on the configuration of the new_generator_mappings parameter

reference

  • Docs. Spring. IO/spring – the boot…
  • zhuanlan.zhihu.com/p/64844767
  • Blog.csdn.net/WZH577/arti…