Hibernate Native SQL mapping to solve problems

2010-06-01  来源:本站原创  分类:Java  人气:367 

Where in the questionnaire and forums, often see people ask how to use the local SQL query results out of the value of the object mapped to the problem, here on the methods provided by Hibernate to be a conclusion. Premise, there are no delays in loading techniques using the property.

Suppose there is a value on the image, as follows:

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;
    }
}  

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 check all five records, then as long as

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

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

If you only need to check id, name, age three records show? Then they would have new construction method, and

public Person(Long id, String name, Long age) {
    this.id = id;
    this.name = name;
    this.age = age;
}  

        public Person(Long id, String name, Long age) {
                this.id = id;
                this.name = name;
                this.age = age;
        }

Then, you can come through the HQL query.

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

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

This method is usually to meet needs, and only if, only need to check id, name, phone records, then three columns, but also new construction method? To die, there will be conflict in the constructor. One way:

List list = getSession().createQuery("select id,name,phone from person").addScalar("id",Hibernate.Long).addScalar("name").addScalar("phone",Hibernate.Long).addEntity(Person.class);  

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 existing Person entity, and if no Person Hibernate mapping in it, the system is only one JavaBean.

List list = getSession().createSQLQuery("select id \"id\",name \"name\",phone \"phone\" from person")
                    .addScalar("id",Hibernate.Long).addScalar("name").addScalar("phone",Hibernate.Long)
                    .setResultTransformer(Transformers.aliasToBean(Person.class)));  

List list = getSession().createSQLQuery("select id \"id\",name \"name\",phone \"phone\" from person")
                                        .addScalar("id",Hibernate.Long).addScalar("name").addScalar("phone",Hibernate.Long)
                                        .setResultTransformer(Transformers.aliasToBean(Person.class)));

Then Hibernate will automatically check out the three elements to the VO object to assemble, but the amount of code a bit, but it also needs to redefine the name to lowercase, and check out the Oracle in the column default to uppercase (do not know how like other database)

This approach does not depend on the construction method, and only need to define private variables and set getter / setter methods on the line.

However, if more violent point, it is not how to do JavaBean objects can be filled, Hibernate can check out the column to the collection class to the assembly. As Map.

List list = getSession().createSQLQuery("select * from person")
                    .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);  

List list = getSession().createSQLQuery("select * from person")
                    .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);

In addition to the Transformers series can also be converted into List.

List list = getSession().createSQLQuery("select * from person")
                    .setResultTransformer(Transformers.T0_LIST);  

List list = getSession().createSQLQuery("select * from person")
                    .setResultTransformer(Transformers.T0_LIST);

This can also be the result of Transformers through inheritance mapping for other objects, not tired out, and basic skills can be used.

相关文章
  • Hibernate Native SQL mapping to solve problems 2010-06-01

    Where in the questionnaire and forums, often see people ask how to use the local SQL query results out of the value of the object mapped to the problem, here on the methods provided by Hibernate to be a conclusion. Premise, there are no delays in loa

  • How to use hibernate native SQL query the data 2010-08-02

    Sometimes we may need to use sql statement to query some of the content, rather than the physical object out, this time you can use native SQL to query and return List, which elements of the Object [] array, check all the fields are among the array L

  • Hibernate HQL Example 5: Native SQL statement query 2010-05-05

    Use: session.createSQLQuery Note: Here is the query table name corresponding to the .. package com.bjsxt.hibernate; import java.util.Iterator; import java.util.List; import org.hibernate.SQLQuery; import org.hibernate.Session; import junit.framework.

  • hibernate no dialect mapping for jdbc type 2010-03-20

    The project applied to hibernate, sometimes unavoidable to use native SQL for certain queries. Hql query with the words, because the mapping of physical configuration files, database fields are mapped on the different data types java native sql query

  • Use native SQL query JPQL and JPA entities (rpm) 2010-12-30

    Of: Yuli Vasiliev Learn how to use the Java Persistence query language and native SQL query JPA entities. Published September 2008 In the Java EE, Java Persistence API (JPA) is a standard for accessing relational database API, which provides a simple

  • On SQL EXPRESS 2005 connection problems 2010-12-01

    The other one On SQL EXPRESS 2005 connection problems April 2, 2007 Monday 13:45 By default, SQL Server Express as a named "SQLEXPRESS" instance installation. Specified in the connection string name with the server instance name, you can connect

  • Post a persistence framework, can be achieved instead of hibernate and dynamic mapping, run-time to change the table structure, structure of the table with more than database storage, and multiple database support, please scream if I felt that I row 2010-08-28

    Post a persistence framework, can be achieved instead of hibernate and dynamic mapping, run-time to change the table structure, structure of the table with more than database storage, and multiple database support, please scream if I felt that line.

  • Learn how to use bit operations to solve problems 2010-10-04

    Learn how to use bit operations to solve problems, common solutions are given below: Detection of an unsigned number is not 2 ^ n-1 (^ is power): x & (x +1) The far right of the 0 to 1: x | (x +1) Complement operation formula: -X = ~ x + 1 = ~ (x-1)

  • sql character set, solve the problem of garbled mysql (rpm) 2011-09-06

    sql character set, solve the problem of garbled blog category mysql: mysql MySQLWindowsDOSHPLinux MySQL is the MySQL root garbled character set properly, the paper summarizes the MySQL character set view command. MySQL database server, including view

  • 让Hibernate输出SQL语句参数配置 2013-06-04

    在J2ee应用中,如果采用Hibernate框架,可以自动建立数据库实体与Java实体类的映射关系,大大方便Java程序进行持久化对象访问和操作,从而明显提高企业应用开发效率,减轻Java程序与后台数据库的依赖性. 但 是,有时候如果操作不慎,可能造成映射关系不完整(特别是存在外键链接的时候),造成访问数据库对象时程序有Bug.这时,可以通过参数配置,让 Hibernate在每次需要从数据库查询记录或者每次插入.更新数据库记录时,将相应的SQL语句输出到控制台,从而了解具体的数据库操作,更好地进

  • Small talk about Hibernate under SQL Grammer Exception, Could not execute JDBC batch update exception solution 2010-12-22

    Hibernate-based application development process, the database through Hibernate operation, the program execution occurred during the SQL Grammer Exception, Could not execute JDBC batch update of the message. Encounter such problems do people feel puz

  • hibernate one-to-many inverse mapping of the intelligent property confusion 2009-04-30

    We found that the number of sql statements are the same, functions similar, but found at the above two update statements inside, then set up a name like, but the sql statement below shows the more "intelligent" Some do not know why, urge you to

  • Hibernate Object Relational Mapping Configuration 2010-03-29

    1, class innuendo <class name = "com.gdlinkway.hibernate. students" / / class name table = "student" / / database table name schema = "SCOTT" / / database user dynamic-update = "true" / / dynamically generate the

  • "switch" to use P6SPY see HIBERNATE generated SQL 2009-01-21

    HIBERNATE-generated SQL to obtain SHOW_SQL in, update / insert statement of the value has been replaced by "?", Yesterday may receive adequate guidance in this forum to solve this problem with p6spy, special feedback will be solution to this for

  • Hibernate's SQL query 2010-08-18

    Hibernate also supports the use of SQL queries, SQL queries can take advantage of some of the characteristics of the database, or application used to migrate existing Hibernate JDBC applications. Use named SQL query SQL statements can also be configu

  • Hibernate O / R Mapping 2010-12-13

    O / R mapping is the most critical ORM part of the framework; Hibernate basic data types, entity attributes in the Hibernate mapping definition, the definition of these types, you can complete the Java data types to the database specific data type ma

  • sql server locks solve the deadlock project query 2011-03-30

    Locking a database table SELECT * FROM table WITH (HOLDLOCK) Note: Locking a table in the database the difference between SELECT * FROM table WITH (HOLDLOCK) Other transactions can read the table, but can not be updated to delete SELECT * FROM table

  • Hibernate通过SQL查询常量时只能返回第一个字符的解决方法 2013-08-12

    在Hibernate中如果通过 session.createSQLQuery("select '张三' as name from dual").list(); 查询,返回值中只会有一个"张"字. 经查,常量在数据库中被认为是CHAR类型的,Hibernate取值时将其保存为Character类型,而Character只能保存一个字符,所以造成返回值中只存在一个字符.Hibernate官方已存在此bug,却从未修复. 该bug中给出以下3种解决方法. 1.继承一个Di

  • Hibernate Annotation many-to-many mapping implementation 2009-05-20

    Since users want to achieve - the role - rights management, so the need to achieve many-to-many mapping. After the whole day learning and debugging, and finally the implementation of easy features. The following is a type of written test for everyone

  • View hibernate actual sql statement issued by the 2010-03-18

    The usual show_sql = true arguments can only see the last one? log4j settings log4j.category.org.hibernate.SQL = DEBUG, CA log4j.category.org.hibernate.type = DEBUG, CA log DEBUG 10-03-18 17:26:17,203 main [AbstractBatcher.log:346] select topdsobayi0