我下午把代码靠到网吧来了!你们帮我看看!hibernate.cfg.xml:<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="myeclipse.connection.profile">zxr</property> <property name="connection.url"> jdbc:mysql://localhost:3306/test </property> <property name="connection.username">root</property> <property name="connection.password">0555243000</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <mapping resource="test/Cat.hbm.xml" /> </session-factory> </hibernate-configuration> Cat.hbm.xml:<?xml version="1.0" encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" ><!-- DO NOT EDIT: This is a generated file that is synchronized --> <!-- by MyEclipse Hibernate tool integration. --> <!-- Created Mon Sep 19 15:52:42 CST 2005 --> <hibernate-mapping package="test"> <class name="Cat" table="cat"> <id name="username" column="username" type="java.lang.String"> <generator class="increment"/> </id> <property name="userpassword" column="userpassword" type="java.lang.String" /> </class> </hibernate-mapping> 测试类: import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.SQLQuery; import org.hibernate.Query; import org.hibernate.HibernateException; import java.util.Iterator; import java.util.List; import test.*; import org.apache.log4j.BasicConfigurator; import org.apache.log4j.Logger; import org.hibernate.Transaction;public class test { public static void main(String args[]){ Iterator ite=null; String str="failure"; List l=null; Query q=null; SessionFactory sf; Session s=null; Transaction t=null; Configuration conf=null; BasicConfigurator.configure(); try{ conf = new Configuration(); conf.configure(); sf=conf.buildSessionFactory(); s=sf.openSession(); t=s.beginTransaction(); q=s.createQuery("from cat"); ite=q.iterate(); t.commit(); System.out.print("\n\nok\n\n"); }catch(HibernateException he){} System.out.print(ite); } } 错误信息:...... 7968 [main] DEBUG org.hibernate.hql.ast.ErrorCounter - throwQueryException() : no errors 8047 [main] DEBUG org.hibernate.hql.antlr.HqlSqlBaseWalker - query() << begin, level = 1 8109 [main] ERROR org.hibernate.hql.PARSER - *** ERROR: cat is not mapped. 8109 [main] DEBUG org.hibernate.hql.ast.ErrorCounter - cat is not mapped. cat is not mapped. at org.hibernate.hql.ast.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:85) at org.hibernate.hql.ast.FromElementFactory.addFromElement(FromElementFactory.java:77) at org.hibernate.hql.ast.FromClause.addFromElement(FromClause.java:67) at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:217) at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:2830) at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2719) at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:513) at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:371) at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:201) at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:151) at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:189) at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:130) at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83) at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:427) at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:884) at org.hibernate.impl.SessionImpl.iterate(SessionImpl.java:920) at org.hibernate.impl.QueryImpl.iterate(QueryImpl.java:41) at test.main(test.java:35) 8125 [main] DEBUG org.hibernate.hql.antlr.HqlSqlBaseWalker - query() : finishing up , level = 1 8125 [main] DEBUG org.hibernate.hql.ast.HqlSqlWalker - processQuery() : ( SELECT FromClause{level=1} ) 8140 [main] DEBUG org.hibernate.hql.ast.HqlSqlWalker - Derived SELECT clause created. 8187 [main] DEBUG org.hibernate.hql.antlr.HqlSqlBaseWalker - query() >> end, level = 1 8187 [main] DEBUG org.hibernate.hql.ast.AST - --- SQL AST --- \-[SELECT] QueryNode: 'SELECT' +-[SELECT_CLAUSE] SelectClause: '{derived select clause}' \-[FROM] FromClause: 'from' FromClause{level=1, fromElementCounter=0, fromElements=0, fromElementByClassAlias=[], fromElementByTableAlias=[], fromElementsByPath=[], collectionJoinFromElementsByPath=[], impliedElements=[]}null
首先的一地方的错是:q=s.createQuery("from cat"); cat应该是那个类的名字要大写Cat你写成这样看看("from Cat as hello") hello可随意 其它地方你就慢慢找啊
snruge123(狂热&冷静) 说的对,我试了成功了。但只限于 Query sq=s.createQuery("from Cat"); Iterator i=sq.list().iterator(); 如果是 SQLQuery sq=s.createSQLQuery("select c.username as name,c.userpassword as password from Cat c").addEntity("c",Cat.class); Iterator i=sq.list().iterator(); 就会出现column uername0_ not found 错误。
不过配置多不会错的,因为我是用Myeclipse自动生成的Cat.hbm.xml和hibernate.cfg.xml!
当:
SQLQuery sq=s.createSQLQuery("from test.cat");
Iterator i=sq.list().iterator();
i怎么搞多是NULL。数据库是Mysql。且里面有数据。
不知道为什么!
改为
Configuration conf=new Configuration().configure();试试
原来就是Configuration conf=new Configuration().configure();
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="myeclipse.connection.profile">zxr</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/test
</property>
<property name="connection.username">root</property>
<property name="connection.password">0555243000</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<mapping resource="test/Cat.hbm.xml" />
</session-factory>
</hibernate-configuration>
Cat.hbm.xml:<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" ><!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Mon Sep 19 15:52:42 CST 2005 -->
<hibernate-mapping package="test"> <class name="Cat" table="cat">
<id name="username" column="username" type="java.lang.String">
<generator class="increment"/>
</id>
<property name="userpassword" column="userpassword" type="java.lang.String" />
</class>
</hibernate-mapping>
测试类:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.SQLQuery;
import org.hibernate.Query;
import org.hibernate.HibernateException;
import java.util.Iterator;
import java.util.List;
import test.*;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.hibernate.Transaction;public class test {
public static void main(String args[]){
Iterator ite=null;
String str="failure";
List l=null;
Query q=null;
SessionFactory sf;
Session s=null;
Transaction t=null;
Configuration conf=null;
BasicConfigurator.configure();
try{
conf = new Configuration();
conf.configure();
sf=conf.buildSessionFactory();
s=sf.openSession();
t=s.beginTransaction();
q=s.createQuery("from cat");
ite=q.iterate();
t.commit();
System.out.print("\n\nok\n\n");
}catch(HibernateException he){}
System.out.print(ite);
}
}
错误信息:......
7968 [main] DEBUG org.hibernate.hql.ast.ErrorCounter - throwQueryException() : no errors
8047 [main] DEBUG org.hibernate.hql.antlr.HqlSqlBaseWalker - query() << begin, level = 1
8109 [main] ERROR org.hibernate.hql.PARSER - *** ERROR: cat is not mapped.
8109 [main] DEBUG org.hibernate.hql.ast.ErrorCounter - cat is not mapped.
cat is not mapped.
at org.hibernate.hql.ast.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:85)
at org.hibernate.hql.ast.FromElementFactory.addFromElement(FromElementFactory.java:77)
at org.hibernate.hql.ast.FromClause.addFromElement(FromClause.java:67)
at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:217)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:2830)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2719)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:513)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:371)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:201)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:151)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:189)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:130)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83)
at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:427)
at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:884)
at org.hibernate.impl.SessionImpl.iterate(SessionImpl.java:920)
at org.hibernate.impl.QueryImpl.iterate(QueryImpl.java:41)
at test.main(test.java:35)
8125 [main] DEBUG org.hibernate.hql.antlr.HqlSqlBaseWalker - query() : finishing up , level = 1
8125 [main] DEBUG org.hibernate.hql.ast.HqlSqlWalker - processQuery() : ( SELECT FromClause{level=1} )
8140 [main] DEBUG org.hibernate.hql.ast.HqlSqlWalker - Derived SELECT clause created.
8187 [main] DEBUG org.hibernate.hql.antlr.HqlSqlBaseWalker - query() >> end, level = 1
8187 [main] DEBUG org.hibernate.hql.ast.AST - --- SQL AST ---
\-[SELECT] QueryNode: 'SELECT'
+-[SELECT_CLAUSE] SelectClause: '{derived select clause}'
\-[FROM] FromClause: 'from' FromClause{level=1, fromElementCounter=0, fromElements=0, fromElementByClassAlias=[], fromElementByTableAlias=[], fromElementsByPath=[], collectionJoinFromElementsByPath=[], impliedElements=[]}null
cat应该是那个类的名字要大写Cat你写成这样看看("from Cat as hello")
hello可随意 其它地方你就慢慢找啊
Query sq=s.createQuery("from Cat");
Iterator i=sq.list().iterator();
如果是
SQLQuery sq=s.createSQLQuery("select c.username as name,c.userpassword as password from Cat c").addEntity("c",Cat.class);
Iterator i=sq.list().iterator();
就会出现column uername0_ not found 错误。