Discussion on the Case of table field in Hibernate5

Source: Internet
Author: User

In the actual application, encountered in the database table in the case of the name of the field (such as TaskID, legacy problems, can not modify the database), when using Hibernate for ORM, encountered cannot find the field or database new build field problems. This paper takes PostgreSQL 9.6 database and hibernate 5.2.12.Final as an example to explore the sensitivity problem of table field in Orm in Hibernate5, and gives the solution.

Case database:

CREATE TABLE product
(
    pid character varying () not NULL,
    "taskId" character varying (?) not NULL,
    ... C4/>constraint product_pkey PRIMARY KEY (PID)
)

When using the Spring Boot (1.5.4.RELEASE), Hibernate5 for table ORM, the annotations to the TaskID field are as follows:

Private String taskId;
@Column (name= "taskId") public
String GetTaskID () {return
    taskId;
}
public void Settaskid (String taskId) {
    this.taskid = taskId;
}

In the naming policy spring.jpa.hibernate.naming.implicit-strategy and spring.jpa.hibernate.naming.physical-strategy do not make any configuration, Spring.jpa.hiberna In the case of Te.ddl-auto=update, starting the Springboot, the database table generates the field "task_id", which means that hibernate ORM cannot find the database table TaskID field. Springboot Business return taskid value is null.

When the @column (name= "TaskId") is changed to @column (name= "TaskId"), the system complains, the error is as follows:

2017-11-01 19:24:41 ERROR o.h.e.jdbc.spi.sqlexceptionhelper-error:column product0_.taskid does not exist
  Recommendation: Perhaps you meant to reference the column "Product0_.taskid" or the column "product0_.task_id".
  Location: 86

It is clearly not possible to change the @column (name= "TaskId") to @column (name= "TaskId") and Return to @column (name= "taskId") when executing SQL statements in Pgadmin "SELECT TaskId From product, error reported:

ERROR:  column "TaskId" does not exist line
1:select taskId from product
               ^
HINT:  Perhaps your meant to Reference the column "Product.taskid" or the column "product.task_id".
Error **********
What is the reason for the fact that table fields taskId exist, and we find that when you create a field name with uppercase letters, the field name is enclosed in double quotes, and the result returns correctly when the SQL statement is "SELECT ' TaskId ' from product". Then, according to this idea, improve @column (name= "TaskId") for @column (name= "\" taskid\ "), start the program, find the database table or new task_id fields, Springboot Business return taskid value is null.

Next springboot Add a named policy:

spring.jpa.hibernate.naming.implicit-strategy= Org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy= Org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl


Springboot correctly returns the TaskID value as a value in the database, while the database table does not add a "task_id" field.

In addition, Springnamingstrategy is removed after Hibernate5.1, and Hibernate.ejb.naming_strategy will no longer be supported, That is, Spring.jpa.hibernate.naming-strategy's two optional values org.hibernate.cfg.ImprovedNamingStrategy and Org.hibernate.cfg.DefaultNamingStrate Gy's not working, Instead, it was replaced with two attributes: Spring.jpa.hibernate.naming.implicit-strategy and Spring.jpa.hibernate.naming.physical-strategy.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.