我有2个相关的Entity类,并且想要定义另一个非Entity类,只包含2个Entity类,这样我就可以使用HQL来加载带有Join的2个Entity Classes。以下是代码:
@Entity
@Table(name = "Conversation", schema = "aaa", catalog = "xxx")
public class Conversation implements Serializable {
private String conversationGuid;
...
}
@Entity
@Table(name = "Ticket", schema = "dbo", catalog = "MyTime")
public class Ticket implements Serializable {
private String ticketGuid;
private String conversationGuid;
...
}
非实体POJO:
public class Conversation_Ticket {
private Conversation convo;
private Ticket ticket;
public Conversation_Ticket(Conversation convo, Ticket ticket) {
super();
this.convo = convo;
this.ticket = ticket;
}
}
DAO进行HQL查询:
Configuration cfg = new Configuration().addFile("/Users/csi/git/mytime-acr/src/main/resources/Conversation_Ticket.hbm.xml");
Query query = getSession().createQuery(
"select new Conversation_Ticket(convo, tic)"
+ "from Conversation as convo left outer join fetch Ticket as tic on convo.Conversation_GUID = tic.Conversation_GUID where agentGuidCurrent = :agentGuid and workState = :workState");
query.setParameter("agentGuidCurrent", agentGuid);
query.setParameter("workState", workState);
List list = query.list();
当调用createQuery()时,它基本上抛出以下异常: org.hibernate.hql.internal.ast.QuerySyntaxException:加入的路径! [从com.cs.mytime.acr.model.Conversation中选择新的Conversation_Ticket(convo,tic)作为convo left outer join fetch Ticket as tic on convo.Conversation_GUID = tic.Conversation_GUID where agentGuidCurrent =:agentGuid and workState =:workState] < / p>
似乎Hibernate只是不识别Conversation_Ticket POJO并期望Conversation&amp;之间的某些对象级关系。票。
Conversation_Ticket.hbm.xml定义如下:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<import class="com.cs.mytime.acr.model.aggregate.Conversation_Ticket" />
</hibernate-mapping>
我确实尝试将Conversation_Ticket.hbm.xml作为映射资源放在hibernate.cfg.xml中,如下所示:
<mapping resource="/Users/csi/git/mytime-acr/src/main/resources/Conversation_Ticket.hbm.xml"/>
但它抱怨找不到Conversation_Ticket.hbm.xml,即使我在我的环境的CLASSPATH中定义了/ Users / csi / git / mytime-acr / src / main / resources。
感谢所有能给我一些指导如何解决这个问题的人。感谢。
答案 0 :(得分:0)
您有一个特殊的目录布局。假设src
是Eclipse中的源文件夹,它会将所有非Java文件复制到classes或bin目录(或者您为编译类选择的任何目录名),并且Conversation_Ticket.hbm.xml
应该是直接在src
下,因为您告诉Hibernate从classpath
的根目录加载它:
cfg.addResource("Conversation_Ticket.hbm.xml");
如果将它放在main / resources中,加载它的代码应为
cfg.addResource("main/resources/Conversation_Ticket.hbm.xml");
为什么不使用自己的包层次结构,因此使用以下目录树:
src
com
rcook
myapp