In JPA or hibernate.

Source: Internet
Author: User

JPA performs native SQL truncation char type issues

Executes the native SQL:EntityManager.createNativeQuery (String sqlString) in the JPA API; The incoming parameter is a native SQL statement that returns a SQL object.

If a field in the Oracle database defines a char type, say char[2], when using entitymanager.createnativequery (String sql); When querying these values, it is found that the returned result set returns only one character, as long as the database is of type char.

Cause: Hibernate Converts a field of type char to character. Therefore, the query results are incorrect.
Workaround:

"1" only needs to be in the SQL statement in the database is a char type field, plus a trim function to handle it.

"2" with Entitymanager.createnativequery (String sqlString, Class resultclass) Method can be replaced by the following methods: Entitymanager.createnativequery (String sqlString); Where class ResultClass is the full name of the entity class, including the package name in which it resides.

The difference between CreateQuery and Createsqlquery in Hibernate is:
The former uses the HQL statement to query, the latter can use the SQL statement query
The former takes the bean generated by hibernate as the object to load the list back
The latter is stored as an array of objects
So it's not very convenient to use createsqlquery and sometimes want to load the list back with hibernate-generated beans.
Suddenly discovered that Createsqlquery had a way to convert objects directly
Query query = session.createsqlquery (SQL). addentity (Xxxxxxx.class);
XXXXXXX represents the object of the bean generated by hibernate, which is the bean that the data table maps out.
Hehe after more attention, or from time to time to see the use of various methods hibernate object.

Paging:

The paging statement in Hibernate can be written like this

Session = Hibernateutils.getsession ();

Session.begintransaction ();

Query query = Session.createquery ("from User");

Query.setfirstresult (0);//start with the first record

Query.setmaxresults (4);//Remove four records

List userlist = Query.list ();

In JPA or hibernate.

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.