hibernate异常: Could not execute query ??? createQuery("from UserInfoVO");为什么不是createQuery("from UserInfo");哪来的UserInfoVO类 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 是个javaBean/pojo 类名为UserInfoVO小弟初学Hibernate,对hql不清楚,在一遍文章上看到说from后面的是一个pojo类名不是数据库中的table名称 UserInfoVO有没有对应的UserInfoVO.hbm.xml文件? <hibernate-mapping><class name="UserInfoVO" table="user"> <id name="id" type="java.lang.Integer" column="id"> <!-- 主键产生方式 --> <generator class="increment" /> </id> <property name="userName" type="java.lang.String" length="10"/> <property name="password" type="java.lang.String" column="password" length="10"/> <property name="popedom"type="java.lang.String" column="popedom" length="10"/> </class></hibernate-mapping> public class UserInfoVO implements Serializable{ private Integer id; private String userName; private String password; private String popedom; public UserInfoVO(){} public Integer getId() { return id; } public void setId(Integer id) { this.id = id; }以下省略.... 最好 把Exception 全部贴上来,不然很难看出哪里错了 你先试用Session获得Connection con 对象做个简单的查询,目的是测试你的hibernate 数据库连接是否正确如果正确在研究 UserInfoVO.hbm.xml和 UserInfoVO 你的 UserInfoVO 要写出有参数的构造方法 你的<mapping resource="UserInfo.hbm.xml" />那么应该是from UserInfo而不是UserInfoVO. 控制台打印:Hibernate: select userinfovo0_.id as id, userinfovo0_.userName as userName, userinfovo0_.password as password, userinfovo0_.popedom as popedom from user userinfovo0_小异: Could not execute query from UserInfo 啊UserInfo这个类查 还是不知道哪错了,我把几个文件全贴不来把,大家帮忙在看看,3Q配置文件:<?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="show_sql">true</property> <property name="connection.url"> jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=fileUp </property> <property name="connection.username">sa</property> <property name="connection.password"></property> <property name="dialect"> org.hibernate.dialect.SQLServerDialect </property> <property name="connection.driver_class"> com.microsoft.jdbc.sqlserver.SQLServerDriver </property> <!--映射文件--> <mapping resource="UserInfo.hbm.xml" /></session-factory></hibernate-configuration>UserInfo.hbm.xml文件:<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" ><hibernate-mapping><class name="UserInfoVO" table="user"> <id name="id" type="java.lang.Integer" column="id"><!-- 主键产生方式 --> <generator class="increment" /> </id> <property name="userName" type="java.lang.String" length="10"/> <property name="password" type="java.lang.String" column="password" length="10"/> <property name="popedom" type="java.lang.String" column="popedom" length="10"/></class></hibernate-mapping> import java.util.List;import net.sf.hibernate.Query;import net.sf.hibernate.SessionFactory;import net.sf.hibernate.Session;import net.sf.hibernate.Transaction;import net.sf.hibernate.cfg.Configuration;public class HibernateDemo { /** * @param args */ Configuration cfg; SessionFactory sf; Session session; public static void main(String[] args) { // TODO Auto-generated method stub try{ HibernateDemo demo=new HibernateDemo(); System.out.println("size: "+demo.getUserInfo().size()); } catch(Exception e){ System.out.println("小异: "+e.getMessage()); } } public List getUserInfo() throws Exception{ List lt=null; cfg=new Configuration().configure(); sf=cfg.buildSessionFactory(); session=sf.openSession(); Transaction tx=session.beginTransaction(); Query query=session.createQuery("from UserInfoVO"); lt=query.list(); tx.commit(); session.close(); return lt; }}import java.io.Serializable;public class UserInfoVO implements Serializable{ private Integer id; private String userName; private String password; private String popedom; public UserInfoVO(){} public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getPopedom() { return popedom; } public void setPopedom(String popedom) { this.popedom = popedom; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; }} 我也遇过这个问题:把<!-- 主键产生方式 --> <generator class="increment" /></id>中class换一下, <generator class="native"></generator> 把你那个UserInfoVO改成UserInfo就哦了!试试! 为程序员正名贴!!!! 急! hibernate问题 电信和银行选哪个? 怎样用JAVA实现RSS功能?谢谢 用jsp做了快一年网站了,现在有些后怕。关于jsp中的线程安全 求jsp+servlet的问题~~~~~~~~~~~在线中 在Tomcat的webapps文件夹中的陌生项目 socket最应该注意的有哪些???? 急!!!!! 怎样从自定义标签中取出标签中的内容(body)? 能够打乱 java 的class, 防止被反编译的软件有什么?在线 更新SQL Server 2005 blob字段的奇怪问题 一个关于如何在jsp页面间传值的问题
小弟初学Hibernate,对hql不清楚,在一遍文章上看到说from后面的是一个pojo类名不是数据库中的table名称
<class name="UserInfoVO" table="user">
<id name="id" type="java.lang.Integer" column="id">
<!-- 主键产生方式 -->
<generator class="increment" />
</id>
<property name="userName" type="java.lang.String" length="10"/>
<property name="password" type="java.lang.String"
column="password"
length="10"/>
<property name="popedom"type="java.lang.String"
column="popedom"
length="10"/>
</class>
</hibernate-mapping>
public class UserInfoVO implements Serializable{
private Integer id;
private String userName;
private String password;
private String popedom;
public UserInfoVO(){}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
以下省略....
做个简单的查询,目的是测试你的hibernate 数据库连接是否正确
如果正确在研究 UserInfoVO.hbm.xml和 UserInfoVO 你的 UserInfoVO 要写出有参数的构造方法
那么应该是from UserInfo而不是UserInfoVO.
Hibernate: select userinfovo0_.id as id, userinfovo0_.userName as userName, userinfovo0_.password as password, userinfovo0_.popedom as popedom from user userinfovo0_
小异: Could not execute query
UserInfo这个类查
配置文件:
<?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="show_sql">true</property>
<property name="connection.url">
jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=fileUp
</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="connection.driver_class">
com.microsoft.jdbc.sqlserver.SQLServerDriver
</property>
<!--映射文件-->
<mapping resource="UserInfo.hbm.xml" />
</session-factory>
</hibernate-configuration>UserInfo.hbm.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" ><hibernate-mapping>
<class name="UserInfoVO" table="user">
<id name="id" type="java.lang.Integer"
column="id">
<!-- 主键产生方式 -->
<generator class="increment" />
</id>
<property name="userName"
type="java.lang.String"
length="10"/>
<property name="password" type="java.lang.String"
column="password" length="10"/>
<property name="popedom" type="java.lang.String"
column="popedom" length="10"/>
</class>
</hibernate-mapping> import java.util.List;
import net.sf.hibernate.Query;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;public class HibernateDemo {
/**
* @param args
*/
Configuration cfg;
SessionFactory sf;
Session session;
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
HibernateDemo demo=new HibernateDemo();
System.out.println("size: "+demo.getUserInfo().size());
}
catch(Exception e){
System.out.println("小异: "+e.getMessage());
}
}
public List getUserInfo() throws Exception{
List lt=null;
cfg=new Configuration().configure();
sf=cfg.buildSessionFactory();
session=sf.openSession();
Transaction tx=session.beginTransaction();
Query query=session.createQuery("from UserInfoVO");
lt=query.list();
tx.commit();
session.close();
return lt;
}
}
import java.io.Serializable;
public class UserInfoVO implements Serializable{
private Integer id;
private String userName;
private String password;
private String popedom;
public UserInfoVO(){}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPopedom() {
return popedom;
}
public void setPopedom(String popedom) {
this.popedom = popedom;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
把<!-- 主键产生方式 -->
<generator class="increment" />
</id>
中class换一下,
<generator class="native"></generator>