Hibernate3 returns Map

Source: Internet
Author: User

After learning that Hibernate3 can return the Map function from the query results, we thought it could save a processing process:

Public HashMap getPermissionMap (){
Try {

HashMap hashMap = (HashMap) getSession ()
. CreateSQLQuery ("select role_Id, role_Desc from Sys_Roles"). setResultTransformer (Transformers. ALIAS_TO_ENTITY_MAP );

Return hashMap;
} Catch (RuntimeException re ){

Throw re;
}
}

Only two lines of code can solve this problem. It seems like an ideal choice. Please try it.

After running the program, a line of Exception Code on the blank page of the jsp worker is eye-catching: org. hibernate. impl. SQLQueryImpl cannot be cast to java. util. HashMap.

It seems that setResultTransformer cannot directly return Map. It returns a List that encapsulates all maps.

No

It seems that things are not that simple, and we still need to explore. Now that we have started, simply explore and understand it! After several revisions to the code and query method, the Code is as follows:

The "Transformers. ALIAS_TO_ENTITY_MAP" in the preceding statement is lost.

It seems that things are not that simple, and we still need to explore. Now that we have started, simply explore and understand it! After several revisions to the code and query method, the Code is as follows:

Public HashMap getPermissionMap (){
Try {

List list = getHibernateTemplate ()
. Find ("select new Map (sysRoles. roleId as roleId, sysRoles. roleDesc as roleDesc) from SysRoles sysRoles ");

For (Object map: list ){
HashMap. putAll (Map) map );

}

Return (HashMap) hashMap;
} Catch (RuntimeException re ){

Throw re;
}
}

After the modification is complete, the so-called "concise and clear code" principle has been removed. On the other hand, there seems to be some truth in the Code. At the same time, I have a better understanding of java's use, and I am not so sorry for the wasted time.

The following code print the returned hashMap cyclically and finds that only one record can be printed, and the content is always the same:

For (Map. Entry map: hashMap. entrySet ()){
System. out. println ("#### map1.getKey () =" + map1.getKey () + ", map1.getValue () =" + map1.getValue ());
}

Alas, let's look for the related principle of "returning Map from Hibernate's query results". It seems that we have to pay a little price for our rashness.

The following is the relevant information I obtained from the Hibernate tutorial. Cai Huajiang of javaEye (now itEye) is clear. Please forgive me for using your article:


//////////////////////////////////////// //// // Start the excerpt ////////////////////////// /////////////////

In the Q & A and forum, I often see people asking how to map the results obtained from local SQL queries to value objects. Here I will draw a conclusion on the methods provided in Hibernate.
Note: The attribute delay loading technology is not used here.

Assume that there is a value pair, as shown below:


Package test;

Public class Person {
Private Long id;
Private String name;
Private Long age;
Private Long phone;
Private String address;

Public Person (Long id, String name, Long age, Long phone, String address ){
This. id = id;
This. name = name;
This. age = age;
This. phone = phone;
This. address = address;
}

Public Long getId (){
Return id;
}

Public void setId (Long id ){
This. id = id;
}

Public String getName (){
Return name;
}

Public void setName (String name ){
This. name = name;
}

Public Long getAge (){
Return age;
}

Public void setAge (Long age ){
This. age = age;
}

Public Long getPhone (){
Return phone;
}

Public void setPhone (Long phone ){
This. phone = phone;
}

Public String getAddress (){
Return address;
}

Public void setAddress (String address ){
This. address = address;
}
} If you query all the five columns of records

List list = getHibernateTemplate (). loadAll (Person. class );

What if I only need to query the records in the id, name, and age columns? Then we need to add a new constructor,

Public Person (Long id, String name, Long age ){
This. id = id;
This. name = name;
This. age = age;
}

Then, you can use HQL to query.

List list = getHibernateTemplate (). find ("select new test. Person (id, name, age) from Person ");

This method can usually meet the requirements, but if you only need to query the records in the id, name, and phone columns, will the constructor be added? No, there will be constructor conflicts. There is a way:

List list = getSession (). createQuery ("select id, name, phone from person ")
. AddScalar ("id", Hibernate. Long). addScalar ("name"). addScalar ("phone", Hibernate. Long)
. AddEntity (Person. class );
However, this only applies to the existence of Person entities. If the Person ing is not performed in Hibernate, only one JavaBean exists in the system.

List list=GetSession (). createSQLQuery ("Select id"Id", Name"Name", Phone"Phone"From person")
. AddScalar (
"Id", Hibernate. Long). addScalar ("Name"). AddScalar (

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.