When using a hibernate query, the query data is not the same as the database problem

Source: Internet
Author: User

When using a hibernate query, the query data is not the same as the database problem because the data item type does not correspond. Need to convert.

The use of addscalar in Hibernate


When we use HQL for subqueries, such as SELECT * from tree where PID in (select id from tree), the hiberante will be able to make an error, saying "wrong".

Method One:
But if you change the * to all the sub sections in the tree class, there is no problem. It's going to be like usual. The first row of data returns a object[], and then you convert the values in the object[array according to the fields in the tree class. This is more troublesome. I found out today if I have SQL to look up a method that can return an object.
Configuration config = new Configuration (). Configure ();
Sessionfactory SF = Config.buildsessionfactory ();
Session session = Sf.opensession ();
Transaction ts = session.begintransaction ();
Query query = session.createsqlquery ("select * from tree t where PID in (select id)"). Addentity (Tree.class); Return object
List List = Query.list ();

At this point the list can be traversed (tree) list.get[i], and the contents of each row are transformed into one object.

Method Two:

You can also return a map object, which means that there are multiple maps in the list, with the following code
Query query = Session.createsqlquery ("Select Id,name from tree t where PID in (select id)"). Setresulttransforme R (Transformers.alias_to_entity_map); Returns a map,key: a consistent (case-consistent) name in DB is used to traverse the list

Map map = (map) list.get[i];

Map.get ("id"); Map.get ("name"); Use the field name of your SQL statement select as the key of the map, but the key must be exactly the same as the field name in the database.


It can also be used as a function aspect. Such as
Query query = session.createsqlquery ("Select sum (ID) sumid from" T where PID in (select ID from tree)
. Addscalar ("Sumid", Hibernate.integer)//conversion type, by type in db
. Setresulttransformer (Transformers.alias_to_entity_map); Returns a Map,key: consistent name (consistent case) for DB

Directly on the Map.get ("Sumid") can be evaluated


Another point is that this method works well on the Hibernate3.2 version.

=================================
Problem:

With a closer look, the problem was found to be the data type. Check on the Internet and find out hibernate in the execution of list result = Session.createsqlquery (SQL). List (), when the SQL statement encountered in the decimal, Long and other types of fields, the above error occurs. And from the error message can be found: the error is dialect.

Solution:

When the mistake is known, I'm going to find a solution on the Internet. It seems that there are too many people with this kind of problem. I read a few, found that there is a solution, is the custom hibernate dialect. Although the database used is different (my database is DB2), I feel the same, just do it:

First, you build a class that inherits Org.hibernate.dialect.DB2Dialect, and the class reads as follows:

Import Java.sql.Types;

Import Org.hibernate.Hibernate;
Import Org.hibernate.dialect.DB2Dialect;

public class Pmdb2dialect extends Db2dialect
{
Public Pmdb2dialect ()
{
Super ();
Registerhibernatetype (Types.decimal, Hibernate.BIG_DECIMAL.getName ());
}
}

The second step is to modify the Hibernate profile Hibernate.cfg.xml:

Will:

<property name= "Hibernate.dialect" >
Org.hibernate.dialect.DB2Dialect
</property>

To

<property name= "Hibernate.dialect" >
Com.yonder.pm.common.PmDb2Dialect
</property>


===============================
Yesterday encountered a problem, as follows:

In Oracle database, field type char (8), Value 12345678

Hibernate in the Createsqlquery method query, returned list with object[] receive, traversal value found Object[0] Output value is 1, only one, the other is gone. The other fields are correct.

---------------------------------------------

Look at the database and find that other fields, including varchar,date, have no problems, only a char type problem.

The char type is defined as the length, 8 represents 8 bytes, space-saving and efficiency is high, the disadvantage is inflexible, the length is dead, this is used to define the station number, fixed 8-bit length. So the database is a field type that solves the problem, but it's not the best way to do it, nor does it find the real reason.

-----------------------------------------------

Now, there are some things to look at, summary of the following:

The Char field of 1,oracle is mapped to the character type in Hibernate and is a subset of varchar.

2, complex SQL with Createsqlquery method query no problem, if query multiple fields, traversal with object[] shape, subscript from 0 start output value, do not need mapping file, if you want to write a mapping bean, easy to access.

3, if query SQL is only one field, it can not be received with object[] array, can only be received with the object class, direct output object.tostring (), that is, the value of this field.

4, you can use the Addscalar (String arg,type type) method to define the type of field you want to return, such as

S.createsqlquery (SHUIQINGHQL). Addscalar ("Stcd", hibernate.string). Addscalar ("stnm");

This solves the problem of only one character for the Char field type.

But the other fields need to be addscalar () in.

The arguments in 5,addscalar (String Arg) need to be capitalized.


Source Address: http://blog.163.com/wwwxiao_fei/blog/static/1391974172011113135538980/

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.