遇到一个奇怪的问题希望达人能解释一下.在bean中配置数据加载模式为延迟加载,在去port时却执行两条select语句:
Hibernate: select port0_.portID as portID44_0_, port0_.airFlag as airFlag44_0_, port0_.createDate as createDate44_0_, port0_.createOpr as createOpr44_0_, port0_.disable as disable44_0_, port0_.englishName as englishN6_44_0_, port0_.modifyDate as modifyDate44_0_, port0_.modifyOpr as modifyOpr44_0_, port0_.nationID as nationID44_0_, port0_.notes as notes44_0_, port0_.othersFlag as othersFlag44_0_, port0_.portCode as portCode44_0_, port0_.portName as portName44_0_, port0_.seaFlag as seaFlag44_0_, port0_.version as version44_0_ from global_config_port port0_ where port0_.portID=?Hibernate: select nation0_.nationID as nationID41_0_, nation0_.airLaneID as airLaneID41_0_, nation0_.createDate as createDate41_0_, nation0_.createOpr as createOpr41_0_, nation0_.disable as disable41_0_, nation0_.englishName as englishN5_41_0_, nation0_.iataArea as iataArea41_0_, nation0_.modifyDate as modifyDate41_0_, nation0_.modifyOpr as modifyOpr41_0_, nation0_.nationCode as nationCode41_0_, nation0_.nationName as nationName41_0_, nation0_.nationShort as nationS11_41_0_, nation0_.noWood as noWood41_0_, nation0_.notes as notes41_0_, nation0_.seaLaneID as seaLaneID41_0_, nation0_.version as version41_0_ from global_config_nation nation0_ where nation0_.nationID=?
如果使用Hibernate默认设置(Hibernate3默认设置是延迟加载),结果只执行一条select语句:
Hibernate: select port0_.portID as portID44_0_, port0_.airFlag as airFlag44_0_, port0_.createDate as createDate44_0_, port0_.createOpr as createOpr44_0_, port0_.disable as disable44_0_, port0_.englishName as englishN6_44_0_, port0_.modifyDate as modifyDate44_0_, port0_.modifyOpr as modifyOpr44_0_, port0_.nationID as nationID44_0_, port0_.notes as notes44_0_, port0_.othersFlag as othersFlag44_0_, port0_.portCode as portCode44_0_, port0_.portName as portName44_0_, port0_.seaFlag as seaFlag44_0_, port0_.version as version44_0_ from global_config_port port0_ where port0_.portID=?注解包使用的是:
import javax.persistence.ManyToOne;
难道在Hibernate配置还有负负得正的关系嘛~
下面是bean代码:
@Entity
@Table(name="global_config_port" )
public class Port extends BaseEntity { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer portID;
private String portCode;
private String portName;
private String englishName;
@ManyToOne(fetch=FetchType.LAZY)//这里显示声明为延迟加载
@NotFound(action=NotFoundAction.IGNORE)
@JoinColumn(name="nationID")
private Nation nation;
private Boolean airFlag;
private Boolean seaFlag;
private Boolean othersFlag;
private String notes;
private Boolean disable;
private String createOpr;
private Date createDate;
private String modifyOpr;
private Date modifyDate;
@Version
private Long version;

public Port(Integer portID,Long version) {
super();
this.portID = portID;
this.version = version;
}

public Port() {
super();
}

@Override
public Map<String, Object> describe() {
Map<String,Object> map = ListOrderedMap.decorate(new CaseInsensitiveMap());
map.put("portID", getPortID());
map.put("portCode", getPortCode());
map.put("portName", getPortName());
map.put("englishName", getEnglishName());
// map.put("nationID", getNation().getNationID());//这里已经把取关联数据注释掉了
map.put("airFlag", getAirFlag());
map.put("seaFlag", getSeaFlag());
map.put("othersFlag", getOthersFlag());
map.put("notes", getNotes());
map.put("disable", getDisable());
map.put("createOpr", getCreateOpr());
map.put("createDate", getCreateDate());
map.put("modifyOpr", getModifyOpr());
map.put("modifyDate", getModifyDate());
map.put("version", getVersion());
return map;
}}

解决方案 »

  1.   

    第一.你的hql进行fetch没有
    第二.你在什么地方是否调用了getNation方法
      

  2.   

    感觉跟你这个nationID有很大的关系  上面那2句为什么 第一句没有去查询nationID 第2句却查了呢?
      

  3.   

    1.HQL进行fetch?在配置文件里面吗?没有.
    2.并没有调用getNation方法.在此后只是调用describe().
      

  4.   

    第一句中已经查出nationID了,port0_.nationID as nationID44_0_, 第二句只是根据nationID查关联的nation数据