浏览 2370 次
|
锁定老贴子 主题:[出错:]做好双向一对多关连后的查询问题
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
时间:2005-09-01
父表
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" > <hibernate-mapping> <!-- Created by the Middlegen Hibernate plugin 2.1 http://boss.bekk.no/boss/middlegen/ http://www.hibernate.org/ --> <class name="com.mycompany.myapp.hibernate.Father" table="father" > <meta attribute="class-description" inherit="false"> @hibernate.class table="father" </meta> <id name="fatherId" type="java.lang.String" column="fatherId" > <meta attribute="field-description"> @hibernate.id generator-class="uuid.hex" type="java.lang.String" column="fatherId" </meta> <generator class="uuid.hex" /> </id> <property name="name" type="java.lang.String" column="name" not-null="true" length="100" > <meta attribute="field-description"> @hibernate.property column="name" length="100" not-null="true" </meta> </property> 。。。。。。。。略 <!-- Associations --> <!-- bi-directional one-to-many association to Son --> <set name="son" lazy="false" inverse="true" cascade="none" > <meta attribute="field-description"> @hibernate.set lazy="false" inverse="true" cascade="none" @hibernate.collection-key column="fatherId" @hibernate.collection-one-to-many class="com.mycompany.myapp.hibernate.Son" </meta> <key> <column name="fatherId" /> </key> <one-to-many class="com.mycompany.myapp.hibernate.Son" /> </set> </class> </hibernate-mapping> 子表: <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" > <hibernate-mapping> <!-- Created by the Middlegen Hibernate plugin 2.1 http://boss.bekk.no/boss/middlegen/ http://www.hibernate.org/ --> <class name="com.mycompany.myapp.hibernate.Son" table="son" > <meta attribute="class-description" inherit="false"> @hibernate.class table="son" </meta> <id name="sonId" type="java.lang.String" column="sonId" > <meta attribute="field-description"> @hibernate.id generator-class="uuid.hex" type="java.lang.String" column="sonId" </meta> <generator class="uuid.hex" /> </id> <property name="sonName" type="java.lang.String" column="sonName" not-null="true" length="100" > <meta attribute="field-description"> @hibernate.property column="sonName" length="100" not-null="true" </meta> </property> 。。。。。。。。略 <!-- Associations --> <!-- bi-directional many-to-one association to Father --> <many-to-one name="father" class="com.mycompany.myapp.hibernate.Father" not-null="true" cascade="none" outer-join="auto" > <meta attribute="field-description"> @hibernate.many-to-one not-null="true" @hibernate.column name="fatherId" </meta> <column name="fatherId" /> </many-to-one> </class> </hibernate-mapping> 以上都是hib自带工具生成的,po中也是生成的,父表po中有子表对应的set集合,但是在执行查询: select count(*) FROM com.mycompany.myapp.hibernate.Son Son , com.mycompany.myapp.hibernate.Father Father where Father.fatherId=Son.fatherId 时报错:javax.servlet.ServletException: net.sf.hibernate.QueryException: could not resolve property: fatherId of: com.mycompany.myapp.hibernate.Son [select count(*) FROM com.mycompany.myapp.hibernate.Son Son , com.mycompany.myapp.hibernate.Father Father where Father.fatherId=Son.fatherId] org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:516) org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:423) org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226) org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164) org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:397) javax.servlet.http.HttpServlet.service(HttpServlet.java:743) javax.servlet.http.HttpServlet.service(HttpServlet.java:856) root cause java.lang.RuntimeException: net.sf.hibernate.QueryException: could not resolve property: fatherId of: com.mycompany.myapp.hibernate.Son [select count(*) FROM com.mycompany.myapp.hibernate.Son Son , com.mycompany.myapp.hibernate.Father Father where Father.fatherId=Son.fatherId] com.util.tools.PageTools.getEntityCount(PageTools.java:106) com.onetomany.actionBean.fatherAction.execute(fatherAction.java:109) org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421) org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226) org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164) org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:397) javax.servlet.http.HttpServlet.service(HttpServlet.java:743) javax.servlet.http.HttpServlet.service(HttpServlet.java:856) 我在做极联插入时没问题,证明我的配置没问题,但查询为何执行不了。 另外,使用 select count(*) FROM com.mycompany.myapp.hibernate.Son Son inner join com.mycompany.myapp.hibernate.Father Father on Father.fatherId=Son.fatherId 也不行。请兄弟们解答,偶刚用不久。。。谢谢 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
时间:2005-09-01
你的子表映射文件里应该没有fatherId这个Column吧.
但在你的Servelt里是否引用了SonClass.getFatherId()之类的?这样是不对的。 可否把你的Servlet,SonClass,以及FatherClass铁出来 |
|
| 返回顶楼 | |
|
时间:2005-09-01
引用,只能是SonClass.getFather().getFatherId()
|
|
| 返回顶楼 | |
|
时间:2005-09-01
叶枫 写道 引用,只能是SonClass.getFather().getFatherId()
子表隐射没有column,否则会与 <many-to-one的地方产生冲突; 下面是son的java文件: package com.mycompany.myapp.hibernate; import java.util.Set; import java.util.HashSet; import java.io.Serializable; import java.util.Date; public class Son implements Serializable { private SonPk sonPk; /** * This attribute maps to the column sonId in the son table. */ private String sonId; /** * This attribute maps to the column fatherId in the son table. */ private String fatherId; /** * This attribute maps to the column sonName in the son table. */ private String sonName; /** * This attribute maps to the column sonSex in the son table. */ private String sonSex; /** * This attribute maps to the column sonWeight in the son table. */ private float sonWeight; /** * This attribute represents whether the primitive attribute sonWeight is null. */ private boolean sonWeightNull = true; /** * This attribute maps to the column sonAddress in the son table. */ private String sonAddress; /** * This attribute maps to the column sonBirthday in the son table. */ private Date sonBirthday; /** * This attribute maps to the column sonOld in the son table. */ private int sonOld; /** * This attribute represents whether the primitive attribute sonOld is null. */ private boolean sonOldNull = true; /** * This attribute maps to the column sonMemo in the son table. */ private String sonMemo; private Father Father; /** * Method 'Son' * */ public Son() { } public void setFather(Father Father) { this.Father = Father; } public Father getFather() { return Father; } /** * Sets the value of sonPk */ public void setSonPk(SonPk sonPk) { this.sonPk = sonPk; } /** * Gets the value of sonPk */ public SonPk getSonPk() { return sonPk; } /** * Method 'getSonId' * * @return java.lang.String */ public java.lang.String getSonId() { return sonId; } /** * Method 'setSonId' * * @param sonId */ public void setSonId(java.lang.String sonId) { this.sonId = sonId; } /** * Method 'getFatherId' * * @return java.lang.String */ public java.lang.String getFatherId() { return fatherId; } /** * Method 'setFatherId' * * @param fatherId */ public void setFatherId(java.lang.String fatherId) { this.fatherId = fatherId; } /** * Method 'getSonName' * * @return java.lang.String */ public java.lang.String getSonName() { return sonName; } /** * Method 'setSonName' * * @param sonName */ public void setSonName(java.lang.String sonName) { this.sonName = sonName; } /** * Method 'getSonSex' * * @return java.lang.String */ public java.lang.String getSonSex() { return sonSex; } /** * Method 'setSonSex' * * @param sonSex */ public void setSonSex(java.lang.String sonSex) { this.sonSex = sonSex; } /** * Method 'getSonWeight' * * @return float */ public float getSonWeight() { return sonWeight; } /** * Method 'setSonWeight' * * @param sonWeight */ public void setSonWeight(float sonWeight) { this.sonWeight = sonWeight; setSonWeightNull(false); } /** * Sets the value of sonWeightNull */ public void setSonWeightNull(boolean sonWeightNull) { this.sonWeightNull = sonWeightNull; } /** * Gets the value of sonWeightNull */ public boolean isSonWeightNull() { return sonWeightNull; } /** * Method 'getSonAddress' * * @return java.lang.String */ public java.lang.String getSonAddress() { return sonAddress; } /** * Method 'setSonAddress' * * @param sonAddress */ public void setSonAddress(java.lang.String sonAddress) { this.sonAddress = sonAddress; } /** * Method 'getSonBirthday' * * @return java.util.Date */ public java.util.Date getSonBirthday() { return sonBirthday; } /** * Method 'setSonBirthday' * * @param sonBirthday */ public void setSonBirthday(java.util.Date sonBirthday) { this.sonBirthday = sonBirthday; } /** * Method 'getSonOld' * * @return int */ public int getSonOld() { return sonOld; } /** * Method 'setSonOld' * * @param sonOld */ public void setSonOld(int sonOld) { this.sonOld = sonOld; setSonOldNull(false); } /** * Sets the value of sonOldNull */ public void setSonOldNull(boolean sonOldNull) { this.sonOldNull = sonOldNull; } /** * Gets the value of sonOldNull */ public boolean isSonOldNull() { return sonOldNull; } /** * Method 'getSonMemo' * * @return java.lang.String */ public java.lang.String getSonMemo() { return sonMemo; } /** * Method 'setSonMemo' * * @param sonMemo */ public void setSonMemo(java.lang.String sonMemo) { this.sonMemo = sonMemo; } /** * Method 'equals' * * @param _other * @return boolean */ public boolean equals(Object _other) { if (_other == null) { return false; } if (_other == this) { return true; } if (! (_other instanceof Son)) { return false; } final Son _cast = (Son) _other; if (sonPk == null ? _cast.sonPk != sonPk : !sonPk.equals(_cast.sonPk)) { return false; } if (sonId == null ? _cast.sonId != sonId : !sonId.equals(_cast.sonId)) { return false; } if (sonName == null ? _cast.sonName != sonName : !sonName.equals(_cast.sonName)) { return false; } if (sonSex == null ? _cast.sonSex != sonSex : !sonSex.equals(_cast.sonSex)) { return false; } if (sonWeight != _cast.sonWeight) { return false; } if (sonWeightNull != _cast.sonWeightNull) { return false; } if (sonAddress == null ? _cast.sonAddress != sonAddress : !sonAddress.equals(_cast.sonAddress)) { return false; } if (sonBirthday == null ? _cast.sonBirthday != sonBirthday : !sonBirthday.equals(_cast.sonBirthday)) { return false; } if (sonOld != _cast.sonOld) { return false; } if (sonOldNull != _cast.sonOldNull) { return false; } if (sonMemo == null ? _cast.sonMemo != sonMemo : !sonMemo.equals(_cast.sonMemo)) { return false; } return true; } /** * Method 'hashCode' * * @return int */ public int hashCode() { int _hashCode = 0; if (sonPk != null) { _hashCode = 29 * _hashCode + sonPk.hashCode(); } if (sonId != null) { _hashCode = 29 * _hashCode + sonId.hashCode(); } if (sonName != null) { _hashCode = 29 * _hashCode + sonName.hashCode(); } if (sonSex != null) { _hashCode = 29 * _hashCode + sonSex.hashCode(); } _hashCode = 29 * _hashCode + Float.floatToIntBits(sonWeight); _hashCode = 29 * _hashCode + (sonWeightNull ? 1 : 0); if (sonAddress != null) { _hashCode = 29 * _hashCode + sonAddress.hashCode(); } if (sonBirthday != null) { _hashCode = 29 * _hashCode + sonBirthday.hashCode(); } _hashCode = 29 * _hashCode + sonOld; _hashCode = 29 * _hashCode + (sonOldNull ? 1 : 0); if (sonMemo != null) { _hashCode = 29 * _hashCode + sonMemo.hashCode(); } return _hashCode; } } 自动生成的;另外,在这个文件里面不论是否定义fatherId是没有区别的,照样报错。 |
|
| 返回顶楼 | |
|
时间:2005-09-01
叶枫 写道 你的子表映射文件里应该没有fatherId这个Column吧.
但在你的Servelt里是否引用了SonClass.getFatherId()之类的?这样是不对的。 可否把你的Servlet,SonClass,以及FatherClass铁出来 Servelt里是否引用了SonClass.getFatherId()之类的? 答:在servlet中并没有执行到这一步,就那个sql就执行不了,报那个错了;而且执行单表查询是不会报错的 |
|
| 返回顶楼 | |
|
时间:2005-09-02
我觉得是blade_rain用法有误。
1, 如果b成员在...hbm.xml 里没有映射声明的话 himbernate是不会知道对象有b这个属性的,所以 hql 里用 'Son.fatherId ' 本身就不对... 2, 如果用了一对多的父子关系(father-son),对数据库而言,就是在子表(son对映的表)里加一个外键,列名就是father里<set>部分声明的(blade_rain用的是'fatherId'), 估计正好和son的fatherId 属性映射的列名一样。所以原来会有冲突,数据库字段重复了! 3. 其实如果想查询 son , hql 直接用 from com.mycompany.myapp.hibernate.Son 就可以了, father 可以用 son.getFather() 得到。 第一次发贴,不知说的对不对... |
|
| 返回顶楼 | |
|
时间:2005-09-09
还是一对多的关系
双相关联 通过hql: son.father.id就可以取得father的id 如果是取有孩子的Father总数可以如下: select count(distinct son.father.id) from Son as son 执行的sql是: select count(distinct son0_.father_id) as x0_0_ from son son0_ |
|
| 返回顶楼 | |




