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.