Step by step upgrade configuration 14: Mysql database, hibernate. dialect using MySQL5_MySQL

Source: Internet
Author: User
Step by step upgrade configuration 14: Mysql database, hibernate. dialect uses MySQL5Dialect to replace MySQLDialect Hibernate

Step by step upgrade configuration 14: Mysql database, hibernate. dialect replaces org. hibernate. dialect. MySQL5Dialect with org. hibernate. dialect. MySQLDialect

I. background:

In the latest project, the hibernatejpa is used to generate the primary key and index of the database table.

However, some tables are found, and the index is clearly written in the @ Entity class.

/**

* Gets the product title, which cannot exceed 300 bytes.

*

* @ Returnthe product title, which cannot exceed 300 bytes

*/

@ Column (name = "TITLE", length = 300)

@ Index (name = "I _PRODUCT_ITEM_TITLE ")

PublicStringgetTitle (){

Returntitle;

}

However, the title field of the generated database table is not indexed.

II. troubleshooting

2.1 checkcolumn type

After checking the title field of the table, we found that the title field type is longtext.



2.2 check create index log

In addition, when hibernate. hbm2ddl. autoupdate is executed, the following two sentences are found in the log:

19:56:36. 352 [main] DEBUG o. h. tool. hbm2ddl. SchemaUpdate-create index I _PRODUCT_ITEM_TITLE on T_PRODUCT_ITEM (TITLE)

19:56:36. 389 [main] ERROR o. h. tool. hbm2ddl. SchemaUpdate-Unsuccessful: create index I _PRODUCT_ITEM_TITLE on T_PRODUCT_ITEM (TITLE)

Longtext fields cannot be indexed.

2.3 try fix (try 1 ):

I changed the length to 200 and successfully created the varchar (200) field.



The index is created successfully.

20:04:57. 481 [main] DEBUG o. h. tool. hbm2ddl. SchemaUpdate-alter table T_PRODUCT_ITEM add column TITLE varchar (200)

20:04:57. 768 [main] DEBUG o. h. tool. hbm2ddl. SchemaUpdate-create index I _PRODUCT_ITEM_TITLE on T_PRODUCT_ITEM (TITLE)

2.4 try fix (try 2 ):

I changed the length to 260, which is also of the longtext type.



2.5 try fix (try 3 ):

Multiple attempts found that the critical point was at length255

III. thinking:

3.1 if you want to deliver the difference, you can change the length of all fields> 255 to 255.

But this is not my character. my character is "an abnormal thing will have a demon"

3.2 The problem is that the table fields generated for fields with the gross length> 255 are of the longtext type?

I thought my hibernate/hibernate-annotations version (3.5.1-Final) was too low,

I tried upgrading to 3.6.10-Final/3.5.6-Final, and the result is still longtext.

Later I thought, it must be the hibernatemapping mysql database. if there is a problem with the table generation, I 'd like to see where the String matches the database field.

We used org. hibernate. dialect. MySQLDialect.

4. source code:

4.1 org. hibernate. dialect. MySQLDialect

Source code:



Originally, if the length is <= 255, the varchar ($1) type is created.

Otherwise, create the longtext type.

Here is an article: http://dev.mysql.com/doc/refman/5.0/en/char.html

Before mysql5.0.3, the maximum varchar length is 255, and after 5.0.3, the varchar length is 65535.



4.2 org. hibernate. dialect. MySQL5Dialect

In the source code, there is a MySQL5Dialect

If the length is <= 65535, the varchar ($1) type is created.

Otherwise, create the longtext type.



Replace hibernate. dialect in sessionFactoryhibernateProperties with org. hibernate. dialect. MySQL5Dialect

(Extended reading: out-of-the-box (configuration process), using spring to reduce the pain of configuring hibernate mapping (AnnotationSessionFactoryBean ))



Re-execute hibernate. hbm2ddl. autoupdate

20:24:31. 375 [main] DEBUG o. h. tool. hbm2ddl. SchemaUpdate-alter table T_PRODUCT_ITEM add column TITLE varchar (300)

20:24:31. 661 [main] DEBUG o. h. tool. hbm2ddl. SchemaUpdate-create index I _PRODUCT_ITEM_TITLE on T_PRODUCT_ITEM (TITLE)



Varchar (300) field created and index created



Refer:

Http://dev.mysql.com/doc/refman/5.0/en/char.html

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.