When we use hql for subqueries, such as select * from tree where PID in (select ID from
Tree. At this time, hiberante will report an error, such as the "*" number. However, if you change * to all the child segments in the tree class, the problem will not occur. The first line of data will be returned as usual
Return an object [], and then convert the values in the object [] Array Based on the fields in the tree class. This is troublesome. Today I found that if I use SQL to check a method
To 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 from tree)"). addentity (tree. Class); // return object
List list = query. List ();
When traversing the list, you can (tree) list. Get [I]; convert the content of each row into an object.
In addition, a map object can be returned, that is, multiple maps are contained in the List. The Code is as follows:
Query query =
Session. createsqlquery ("select ID, name from tree T where PID in (select
ID from tree)
"). Setresulttransformer (transformers. alias_to_entity_map );
// Returns a map with key: Used to traverse the list with the same name (case-insensitive) in the database.
Map map = (MAP) list. Get [I];
Map. Get ("ID"); map. Get ("name. Use the field name after your SQL statement select as the map key, but this key must be exactly the same as the field name in the database.
It can also be used as a function. For example
Query query = session. createsqlquery ("select sum (ID) sumid from tree T where PID in (select ID from tree)
. Addscalar ("sumid", Hibernate
. Integer) // conversion type, converted by type in dB
. Setresulttransformer (transformers. alias_to_entity_map); // returns a map with the key: consistent name (case-insensitive) in the DB)
Map. Get ("sumid") can be taken directly.
Another point is that this method can run properly on hibernate3.2.
======================================
Problem:
Check carefully and find the problem lies in the data type. Check it online and find hibernate
Execute list result = session. createsqlquery (SQL ). in list (), the preceding error occurs when fields of the decimal or long type are encountered in SQL statements. the error message shows dialect.
Solution:
After knowing the error, I will go online to find a solution. it seems that there are too many people who encounter such problems. There are people posting this problem everywhere on the Internet. after reading a few articles, I found that there is a solution: Custom hibernate.
Dialect. Although the databases used are different (the database I used is DB2), I think the same is true:
First, create a class that inherits org. hibernate
. Dialect. db2dialect. The content of this class is 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 ());
}
}
Step 2: Modify hibernate
Configuration File hibernate
. Cfg. xml:
Set:
<Property name = "hibernate
. Dialect ">
Org. hibernate
. Dialect. db2dialect
</Property>
Changed:
<Property name = "hibernate
. Dialect ">
Com. Yonder. PM. Common. pmdb2dialect
</Property>