hibernate条件查询创建多个sql

时间:2017-01-18 17:41:55

标签: java mysql hibernate spring-orm

我在应用程序中遇到了一个非常奇怪的hibernate标准问题。以下是我的源代码片段中提到的。

实体类

public class ContactDetails
{
    public string Name { get; set; }
    public string Lastname { get; set; }
    public string Email { get; set; }
}

DAO课程

    import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
@Table(name = "AIRPORT")
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
public class Airport implements Serializable {

    private static final long serialVersionUID = -7120581694566566178L;
    private Long id;
    private String countryCode;
    private String countryName;
    private String cityCode;
    private String cityName;
    private String airportCode;
    private String airportName;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID", unique = true)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name = "COUNTRY_NAME")
    public String getCountryName() {
        return countryName;
    }

    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }

    @Column(name = "COUNTRY_CODE", length = 10)
    public String getCountryCode() {
        return countryCode;
    }

    public void setCountryCode(String countryCode) {
        this.countryCode = countryCode;
    }

    @Column(name = "CITY_CODE", length = 25)
    public String getCityCode() {
        return cityCode;
    }

    public void setCityCode(String cityCode) {
        this.cityCode = cityCode;
    }

    @Column(name = "CITY_NAME")
    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    @Column(name = "AIRPORT_CODE", unique = true, length = 10)
    public String getAirportCode() {
        return airportCode;
    }

    public void setAirportCode(String airportCode) {
        this.airportCode = airportCode;
    }

    @Column(name = "AIRPORT_NAME")
    public String getAirportName() {
        return airportName;
    }

    public void setAirportName(String airportName) {
        this.airportName = airportName;
    }
}

启动应用程序并查询结果时生成的SQL

    Criteria criteria = getSession().createCriteria(getTemplateClass());
    criteria.addOrder(Order.asc("countryCode"));
    criteria.addOrder(Order.asc("cityCode"));
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    criteria.setCacheable(true);
    return (List<Airport>) criteria.list();

如果我再次调用相同的代码并假设我有1000个机场列表,那么它会在查询下执行1000次。这种行为很奇怪。

Hibernate: select this_.ID as ID1_12_0_, this_.AIRPORT_CODE as AIRPORT_2_12_0_, this_.AIRPORT_NAME as AIRPORT_3_12_0_, this_.CITY_CODE as CITY_COD4_12_0_, this_.CITY_NAME as CITY_NAM5_12_0_, this_.COUNTRY_CODE as COUNTRY_6_12_0_, this_.COUNTRY_NAME as COUNTRY_7_12_0_ from AIRPORT this_ order by this_.COUNTRY_CODE asc, this_.CITY_CODE asc

即使我在我的标准中使用ehcache甚至以下行。

Hibernate: select airport0_.ID as ID1_12_0_, airport0_.AIRPORT_CODE as AIRPORT_2_12_0_, airport0_.AIRPORT_NAME as AIRPORT_3_12_0_, airport0_.CITY_CODE as CITY_COD4_12_0_, airport0_.CITY_NAME as CITY_NAM5_12_0_, airport0_.COUNTRY_CODE as COUNTRY_6_12_0_, airport0_.COUNTRY_NAME as COUNTRY_7_12_0_ from AIRPORT airport0_ where airport0_.ID=?
Hibernate: select airport0_.ID as ID1_12_0_, airport0_.AIRPORT_CODE as AIRPORT_2_12_0_, airport0_.AIRPORT_NAME as AIRPORT_3_12_0_, airport0_.CITY_CODE as CITY_COD4_12_0_, airport0_.CITY_NAME as CITY_NAM5_12_0_, airport0_.COUNTRY_CODE as COUNTRY_6_12_0_, airport0_.COUNTRY_NAME as COUNTRY_7_12_0_ from AIRPORT airport0_ where airport0_.ID=?
Hibernate: select airport0_.ID as ID1_12_0_, airport0_.AIRPORT_CODE as AIRPORT_2_12_0_, airport0_.AIRPORT_NAME as AIRPORT_3_12_0_, airport0_.CITY_CODE as CITY_COD4_12_0_, airport0_.CITY_NAME as CITY_NAM5_12_0_, airport0_.COUNTRY_CODE as COUNTRY_6_12_0_, airport0_.COUNTRY_NAME as COUNTRY_7_12_0_ from AIRPORT airport0_ where airport0_.ID=?
........
........

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我可以想到为什么会出现这种情况的几个不同原因:

  1. 您的实体在其中定义了一个关联,默认配置为急切加入,并且您还指定关联使用FetchMode.SELECT。 (这被称为N + 1问题)

  2. 当事务仍处于打开状态时,您正在与设置为延迟加载的每个Airport对象的关联进行交互。通过交互,我的意思是,您使用getter来访问关系,迫使Hibernate对相关实体进行deproxy。由于在事务仍然打开并且尚未加载关联实体的情况下发生了解压缩,因此Hibernate会自动为您提取关联。

  3. 您已经编写了机场实体的哈希码或等于方法,以使用非热切联接的关联属性并强制hibernate进行deproxy,从而在事务中获取已卸载的实体。