请教一个Hibernate left join fetch问题

09-05-28 fqhznh
由于项目中使用了OpenSessionView,故在查找的时候使用了left join fetch。hql语句为:

select distinct w.taxMenu from TaxWork w left join fetch w.taxMenu where w.taxMenu.menu=?
<p>

错误如下:

 org.springframework.jdbc.BadSqlGrammarException: Hibernate operation: could
 not execute query; bad SQL grammar [select distinct taxmenu1_.id as id50_0_, ta
xmenu1_.id as id50_1_, taxmenu1_.name as name50_0_, taxmenu1_.metric as metric50
_0_, taxmenu1_.leaf as leaf50_0_, taxmenu1_.check_icCard as check5_50_0_, taxmen
u1_.parent_id as parent6_50_0_, taxmenu1_.account_book_id as account7_50_0_, tax
menu1_.menu_id as menu8_50_0_, taxmenu1_.name as name50_1_, taxmenu1_.metric as
metric50_1_, taxmenu1_.leaf as leaf50_1_, taxmenu1_.check_icCard as check5_50_1_
, taxmenu1_.parent_id as parent6_50_1_, taxmenu1_.account_book_id as account7_50
_1_, taxmenu1_.menu_id as menu8_50_1_ from tax_work taxwork0_ left outer join ta
x_menu taxmenu1_ on taxwork0_.tax_menu_id=taxmenu1_.id where taxmenu1_.menu_id=?
]; nested exception is java.sql.SQLException: Invalid column name id50_.
<p>

查了许多的资料,不明白错误在哪里,望多多指教。谢谢

Hbm文件如下:

TaxMenu.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.nssb.bean.TaxMenu" table="tax_menu">
		<id name="id" type="long">
			<column name="id" length="19" not-null="true"/>
			<generator class="org.hibernate.id.TimeIDGenerator" />
		</id>
		
		<property name="name" type="string">
			<column name="name" length="30" not-null="true"/>
		</property>
		<property name="metric" type="int">
			<column name="metric" length="4" not-null="true"/>
		</property>
		<property name="leaf" type="true_false">
			<column name="leaf"/>
		</property>
		<property name="check4ICCard" type="true_false">
			<column name="check_icCard"/>
		</property>
		<property name="parentId" type="long">
			<column name="parent_id" length="19" />
		</property>
		<many-to-one name="accountBook" class="com.zwcl.bean.AccountBook" outer-join="true" update="false">
			<column name="account_book_id" length="19" not-null="true" />
		</many-to-one>
		<many-to-one name="menu" class="com.sys.bean.Menu" outer-join="true" update="false">
			<column name="menu_id" length="19" not-null="true" />
		</many-to-one>
	</class>
</hibernate-mapping>
<p>

TaxWork.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.nssb.bean.TaxWork" table="tax_work">
		<id name="id" type="long">
			<column name="id" length="19" not-null="true"/>
			<generator class="org.hibernate.id.TimeIDGenerator" />
		</id>
		<property name="metric" type="int">
			<column name="metric" length="4" not-null="true"/>
		</property>
		<property name="createData" type="date">
			<column name="create_date"/>
		</property>
		<many-to-one name="accountBook" class="com.zwcl.bean.AccountBook" outer-join="true" update="false">
			<column name="account_book_id" length="19" not-null="true" />
		</many-to-one>
		<many-to-one name="taxMenu" class="com.nssb.bean.TaxMenu" outer-join="true" update="false">
			<column name="tax_menu_id" length="19" not-null="true" />
		</many-to-one>
		<many-to-one name="voucher" class="com.zwcl.bean.Voucher" outer-join="true" update="true">
			<column name="voucher_id" length="19" not-null="true" />
		</many-to-one>
	</class>
</hibernate-mapping>

1
banq
2009-05-29 15:02
帮不了你,不过发个感慨,不是针对你,不要介意。

这其实就是基于数据库复杂SQL编程思路导致的,你又使用了面向对象的持久框架Hibernate,更加难以调试和使用,既然数据库思路,不如直接用JDBC。

yellowcat
2009-06-03 19:58
left join fetch中的fetch去掉试一试

yellowcat
2009-06-03 20:22
因为在where字句中已经有了对w.taxMenu的访问,所以已经自动加载了w.taxMenu了,fetch没有必要使用

猜你喜欢