hibernate中关于hql的一个问题。。。。

在hibernate中的以下中hql语句出错:Exceptioninthread"main"java.lang.IllegalStateException:Nodatatypefornode:org.hibernate.hql.ast.tree.IdentNode\-[IDENT]IdentNode:'sno'{originalText=sno}

{

SessionFactorysf=newConfiguration().configure()

.buildSessionFactory();

Sessionsession=sf.openSession();

//Stringhql="fromStudentswheres.sdeptin(selects.sdeptfromswheres.sname='张三')";

Stringhql="fromStudentswheres.snoin(selectsnofromScwherecno='1')";

Queryquery=session.createQuery(hql);

Listlist=query.list();

Iteratorit=list.iterator();

while(it.hasNext())

{

Studentstu=(Student)it.next();

System.out.println(stu.getSname()+"\t"+stu.getSdept());

}

}

打开Sc.hbm.xml中发现没有sno属性,其内容为:<?xmlversion="1.0"encoding="utf-8"?>

<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!--

MappingfileautogeneratedbyMyEclipsePersistenceTools

-->

<hibernate-mapping>

<classname="com.yan.hibernate.Sc"table="sc"catalog="lrdatabase">

<idname="id"type="java.lang.Integer">

<columnname="id"/>

<generatorclass="identity"/>

</id>

<many-to-onename="course"class="com.yan.hibernate.Course"fetch="select">

<columnname="cno"/>

</many-to-one>

<many-to-onename="student"class="com.yan.hibernate.Student"fetch="select">

<columnname="sno"/>

</many-to-one>

<propertyname="grade"type="java.lang.Integer">

<columnname="grade"/>

</property>

</class>

</hibernate-mapping>

[color=darkred]解决方法是:

将hql语句改为:Stringhql="fromStudentswheres.snoin(selectstudent.snofromScwherecno='1')";[/color]

数据库中建表的情况如下:数据库中建立了三个表student、course、sc表,其关系是sc中的外键sno对应于student表中的sno,sc中的外键cno对应于course中cno,其建表过程为:

/*

MySQLDataTransfer

SourceHost:localhost

SourceDatabase:lrdatabase

TargetHost:localhost

TargetDatabase:lrdatabase

Date:2010-4-215:49:33

*/

SETFOREIGN_KEY_CHECKS=0;

------------------------------

--Tablestructureforcourse

------------------------------

CREATETABLE`course`(

`id`int(10)NOTNULLAUTO_INCREMENT,

`cno`int(10)DEFAULTNULL,

`cname`varchar(50)CHARACTERSETutf8DEFAULTNULL,

`ccredit`int(11)DEFAULTNULL,

PRIMARYKEY(`id`),

KEY`cno`(`cno`)

)ENGINE=InnoDBAUTO_INCREMENT=4DEFAULTCHARSET=gbk;

------------------------------

--Tablestructureforsc

------------------------------

CREATETABLE`sc`(

`id`int(10)NOTNULLAUTO_INCREMENT,

`sno`int(10)DEFAULTNULL,

`cno`int(10)DEFAULTNULL,

`grade`int(10)DEFAULTNULL,

PRIMARYKEY(`id`),

KEY`sno`(`sno`),

KEY`cno`(`cno`),

CONSTRAINT`sc_ibfk_1`FOREIGNKEY(`sno`)REFERENCES`student`(`sno`),

CONSTRAINT`sc_ibfk_2`FOREIGNKEY(`cno`)REFERENCES`course`(`cno`)

)ENGINE=InnoDBAUTO_INCREMENT=2DEFAULTCHARSET=gbk;

------------------------------

--Tablestructureforstudent

------------------------------

CREATETABLE`student`(

`id`int(10)NOTNULLAUTO_INCREMENT,

`sno`int(10)DEFAULTNULL,

`sname`varchar(20)CHARACTERSETutf8DEFAULTNULL,

`ssex`varchar(2)CHARACTERSETutf8DEFAULTNULL,

`sdept`varchar(10)CHARACTERSETutf8DEFAULTNULL,

`sage`int(10)DEFAULTNULL,

`saddress`varchar(50)CHARACTERSETutf8DEFAULTNULL,

PRIMARYKEY(`id`),

KEY`sno`(`sno`)

)ENGINE=InnoDBAUTO_INCREMENT=4DEFAULTCHARSET=gbk;

------------------------------

--Tablestructureforuser

------------------------------

CREATETABLE`user`(

`id`int(11)NOTNULLAUTO_INCREMENT,

`name`varchar(20)CHARACTERSETutf8DEFAULTNULL,

`password`varchar(20)CHARACTERSETutf8DEFAULTNULL,

`phonetype`tinyint(11)DEFAULTNULL,

PRIMARYKEY(`id`)

)ENGINE=InnoDBAUTO_INCREMENT=18DEFAULTCHARSET=gbk;

------------------------------

--Records

------------------------------

INSERTINTO`course`VALUES('1','1','数据库','2');

INSERTINTO`course`VALUES('2','2','操作系统','2');

INSERTINTO`course`VALUES('3','3','软件工程','3');

INSERTINTO`sc`VALUES('1','1','1','86');

INSERTINTO`student`VALUES('1','1','张三','男','计算机','20','北京市');

INSERTINTO`student`VALUES('2','2','李四','女','计算机','21','河北');

INSERTINTO`student`VALUES('3','3','王五','女','法学','20','北京市');

INSERTINTO`user`VALUES('9','李四','1','1');

INSERTINTO`user`VALUES('10','王五','1','1');

INSERTINTO`user`VALUES('11','你好。','1','2');

INSERTINTO`user`VALUES('12','你好啊。','aaa','1');

INSERTINTO`user`VALUES('13','hello','aaa','1');

INSERTINTO`user`VALUES('14','hello','a','1');

INSERTINTO`user`VALUES('15','hsdfgdsg','3242','1');

INSERTINTO`user`VALUES('16','safdasaswerewrw','3242','1');

INSERTINTO`user`VALUES('17','erewrw','3242','1');

相关推荐