我是hibernate的新手并且正在尝试学习基本的东西。我有一个表目的地,其目的地为唯一键我也有UUID作为目标表中的唯一键。 我想提供一种方法,用户可以根据destinationid获取Destination,所以我尝试使用HQL,如下所示
Session session=getSessionFactory().getCurrentSession();
Destination destination=null;
String queryString="from Destination destination "+
"where destination.destinationID = :destinationid";
Query query= session.createQuery(queryString).setParameter("destinationid", destinationId, Hibernate.STRING);
@SuppressWarnings("unchecked")
List<Destination> list=qu
ery.list();
我的Destination类引用了许多其他子类作为集合,所有子集都被定义为延迟加载
<set name="roadTransport" table="ROADTRANSPORT" inverse="true" lazy="true" cascade="save-update">
在Desttination类中有大约9-10个类正在被延迟加载。
我正在导入目标xml,而这个xml是由某个第三方系统提供的,所以我无法关联UUID,我需要决定是在更新模式还是新目的地进行导入。所以destinationid是只有这样我才能找到目的地是否已经在数据库中。
我的问题是新目的地上面的HQL查询工作正常,新目的地正在导入到数据库但是现有的似乎它没有按预期工作而且查询似乎处于挂起状态
Hibernate:
/*
from
Destination destination
where
destination.destinationID = :destinationid */ select
destinatio0_.UUID as UUID0_,
destinatio0_.DESTINATIONID as DESTINAT2_0_,
destinatio0_.NAME as NAME0_,
destinatio0_.SHORTDESCRIPTION as SHORTDES4_0_,
destinatio0_.LONGDESCRIPTION as LONGDESC5_0_,
destinatio0_.IMAGE as IMAGE0_,
destinatio0_.GALLERYPATH as GALLERYP7_0_,
destinatio0_.RATING as RATING0_,
destinatio0_.LASTMODIFIED as LASTMODI9_0_,
destinatio0_.CREATIONDATE as CREATIO10_0_,
destinatio0_.STATEID as STATEID0_,
destinatio0_.TYPECODE as TYPECODE0_,
destinatio0_.COUNTRYCODE as COUNTRY13_0_,
destinatio0_.POPULARITY as POPULARITY0_
from
travellingrants.DESTINATION destinatio0_
where
destinatio0_.DESTINATIONID=?
Hibernate:
/* load com.raisonne.tr.model.destination.destinationattributes.Culture */ select
culture0_.UUID as UUID4_2_,
culture0_.SHORTDESCRIPTION as SHORTDES2_4_2_,
culture0_.LONGDESCRIPTION as LONGDESC3_4_2_,
culture0_.CREATIONDATE as CREATION4_4_2_,
culture0_.LASTMODIFIED as LASTMODI5_4_2_,
destinatio1_.UUID as UUID0_0_,
destinatio1_.DESTINATIONID as DESTINAT2_0_0_,
destinatio1_.NAME as NAME0_0_,
destinatio1_.SHORTDESCRIPTION as SHORTDES4_0_0_,
destinatio1_.LONGDESCRIPTION as LONGDESC5_0_0_,
destinatio1_.IMAGE as IMAGE0_0_,
destinatio1_.GALLERYPATH as GALLERYP7_0_0_,
destinatio1_.RATING as RATING0_0_,
destinatio1_.LASTMODIFIED as LASTMODI9_0_0_,
destinatio1_.CREATIONDATE as CREATIO10_0_0_,
destinatio1_.STATEID as STATEID0_0_,
destinatio1_.TYPECODE as TYPECODE0_0_,
destinatio1_.COUNTRYCODE as COUNTRY13_0_0_,
destinatio1_.POPULARITY as POPULARITY0_0_,
culture2_.UUID as UUID4_1_,
culture2_.SHORTDESCRIPTION as SHORTDES2_4_1_,
culture2_.LONGDESCRIPTION as LONGDESC3_4_1_,
culture2_.CREATIONDATE as CREATION4_4_1_,
culture2_.LASTMODIFIED as LASTMODI5_4_1_
from
travellingrants.CULTURE culture0_
left outer join
travellingrants.DESTINATION destinatio1_
on culture0_.UUID=destinatio1_.UUID
left outer join
travellingrants.CULTURE culture2_
on destinatio1_.UUID=culture2_.UUID
where
culture0_.UUID=?
我是hibernate的新手,但在看到hibernate为已经存在的目的地生成的查询后,它似乎面临某种循环引用,但我不知道如何避免这种情况或有没有更好的方法来检查基于唯一键(不是主键)的已存在实体
提前致谢
答案 0 :(得分:0)
使用此选项inverse =“false”lazy =“false”fetch =“join”。问题在于lazy = true。