我是Hibernate的新手,我有两个名为EmployeedetailsLeaverecord的表,在此Employeedetails是父级,而Leaverecord为子级。一名员工可以休多个假,我想获得特定员工的休假记录。


亩查询是,

select a.leaverecords.leaveFrom from Employeedetails as a,Leaverecord as b where b.employeedetails.refId='1017'


但它会引发错误,
illegal attempt to dereference a collection

我的Employeedetails.hbm.xml

<hibernate-mapping>
<class name="pojo.Employeedetails" table="employeedetails" catalog="hrm">
    <id name="refId" type="java.lang.Integer">
        <column name="RefId" />
        <generator class="identity" />
    </id>
    <set name="leaverecords" table="leaverecord" inverse="true" lazy="true" fetch="select">
        <key>
            <column name="RefId" not-null="true" />
        </key>
        <one-to-many class="pojo.Leaverecord" />
    </set>
 </class>




我的Leaverecord.hbm.xml

<class name="pojo.Leaverecord" table="leaverecord" catalog="hrm">
    <id name="sno" type="java.lang.Integer">
        <column name="Sno" />
        <generator class="identity" />
    </id>
    <many-to-one name="employeedetails" class="pojo.Employeedetails" fetch="select">
        <column name="RefId" not-null="true" />
    </many-to-one>
    <property name="leaveFrom" type="date">
        <column name="LeaveFrom" length="10" not-null="true" />
    </property>
    <property name="leaveTo" type="date">
        <column name="LeaveTo" length="10" />
    </property>
    <property name="empReason" type="string">
        <column name="EmpReason" length="100" not-null="true" />
    </property>
</class>

最佳答案

尝试:

   select b.`leave From` from Employeedetails as a
   inner join Leaverecord as b on a.RefId = b.RefId
   where a.refId='1017'


您有leave From列,而没有leaveFrom

关于java - 如何在Hibernate中从父表到子表检索值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22831625/

10-12 00:12