hibernate入门程序出错,不能执行语句? hibernate 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个是配置文件和主程序我编写了简单的只包含id和name两个属性的employee类,映射文件employee.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"><hibernate-mapping> <class name="com.hibernate.bean.Employee" table="employee"> <id column="eid" name="id"> <generator class="assigned"/> </id> <property column="name" name="name"/> </class></hibernate-mapping>总的映射文件hibernate.cfg.xml<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools. --><hibernate-configuration><session-factory> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="connection.url"> jdbc:mysql://localhost:3306/hibernatetest </property> <property name="connection.username">root</property> <property name="connection.password">admin</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="show_sql">true</property> <mapping resource="com/hibernate/bean/Employee.hbm.xml" /></session-factory></hibernate-configuration>测试用java程序,据说buildSessionFactory已经不推荐了但是还能用public class Hibernatetest { public static void main(String[] args) { Employee e=new Employee(); e.setId("111"); e.setName("AAA"); Configuration cfg = new Configuration(); SessionFactory sf = cfg.configure().buildSessionFactory(); Session session = sf.openSession(); session.beginTransaction(); session.save(e); session.getTransaction().commit(); session.close(); sf.close(); }} 你的mysql存储引擎是InnoDB 还是 MyISAM? 最好是MyISAM 尝试改了默认存储引擎为MYISAM,重新建数据库和表还是JDBC可以而hibernate就不行 用的什么驱动? 可能是驱动包版本问题, 如果是mysql5.1及以上的用的mysql驱动包版本最好用5.1的驱动包 驱动是5.1.26,不过mysql是最近才装的6.0以上版本,是否是这个原因呢 驱动是5.1.26,不过mysql是最近才装的6.0以上版本,是否是这个原因呢可能吧, 你去官网找下6.x的mysql是不是能用5.1的驱动 另外我手动在mysql中插入几条数据,想尝试HQL把测试程序改为如下 public static void main(String[] args) { Employee e=new Employee(); e.setId("222"); e.setName("CCC"); Configuration cfg = new Configuration(); SessionFactory sf = cfg.configure().buildSessionFactory(); Session session = sf.openSession(); Query query=session.createQuery("select Employee from employee as e"); List<Employee> el=query.list(); for(Employee tem:el) { System.out.println(tem.getId()); System.out.println(tem.getName()); } session.close(); sf.close(); }则会显示employee is not mapped,是否其实是配置问题?但我也没发现配置哪错了啊,求指点,新手,如果是低级错误别打我,真是查了好久都不知道哪错 驱动是5.1.26,不过mysql是最近才装的6.0以上版本,是否是这个原因呢可能吧, 你去官网找下6.x的mysql是不是能用5.1的驱动不知道怎么查OTZ不过如果驱动问题的话应该JDBC也失败才对啊 尝试改了默认存储引擎为MYISAM,重新建数据库和表还是JDBC可以而hibernate就不行根据提示的错误肯定是hibernate出的问题,你把employee.java文件贴出来,我觉得是数据类型或者映射的问题。 打个我发现了你的hibernate配置文件头版本写的是3.0,而你用的是hibernate4.0。 你是不是copy别人的代码啊。在hibernate4.0文件里有头版本配置。 <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">这个头版本是错的。 呃,我用了myeclipse自动生成的模板,而且我看的hibernate4教程和搜的网上hibernate4入门环境搭建中也都用的这个头文件,原来有新的!!??我晕我去查一下先 看了下下的4.3.6包里的开发者引导我吧hibernate.cfg.xml改为了<?xml version="1.0" encoding="UTF-8"?><hibernate-configuration xmlns="http://www.hibernate.org/xsd/hibernate-configuration" xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/hibernatetest</property> <property name="connection.username">root</property> <property name="connection.password">admin</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/> </session-factory></hibernate-configuration>但是提示Exception in thread "main" org.hibernate.MappingException: invalid configuration是我没改对么,还是其他的东西也要改,求指点,TAT 尝试改了默认存储引擎为MYISAM,重新建数据库和表还是JDBC可以而hibernate就不行根据提示的错误肯定是hibernate出的问题,你把employee.java文件贴出来,我觉得是数据类型或者映射的问题。这个是employee.java是配置的问题么?TAT求指点package com.hibernate.bean;public class Employee { private String id; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; }} 在hibernate.cfg.xml的<session-factory>中添加: <property name="hibernate.hbm2ddl.auto" >update</property>试一下,意思是加载hibernate自动更新数据库结构,因为你数据库表是自己手动先创建好了 我重新看了一遍,个人觉得你的sql写的和hibernate3差别好大啊,应该用对象执行sql吧,你直接查询的表,我觉得应该是Query query=session.createQuery("from Employee");就可以了,from后边应该+对象不是+表名称。 Query query=session.createQuery("select Employee from employee as e");中sql语句换成“from Employee”试试 检查下mysql版本,尽量保持mysql版本和驱动包版本相兼容。 在jsp里面 当某个元素触发onmouseover时,想要出旁边出现一个浮动div 如何提升Online Judge性能 在servlet 中能不能获得页面的pageContext 对象 ? 用那个方法 。 'MyTest' 未定义 求jsp日程管理程序 关于水晶报表的问题,求救 单独页面如何让session不超时 数据库数据显示不出来 请问如何取出SQL数据库里某表某字段的description内容 jsp编译报错 为什么initMethod没有执行 ? JavaWeb求教大神,为什么我这个会报错??
我编写了简单的只包含id和name两个属性的employee类,映射文件employee.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">
<hibernate-mapping>
<class name="com.hibernate.bean.Employee" table="employee">
<id column="eid" name="id">
<generator class="assigned"/>
</id>
<property column="name" name="name"/>
</class>
</hibernate-mapping>
总的映射文件
hibernate.cfg.xml<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/hibernatetest
</property>
<property name="connection.username">root</property>
<property name="connection.password">admin</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="show_sql">true</property>
<mapping resource="com/hibernate/bean/Employee.hbm.xml" />
</session-factory>
</hibernate-configuration>
测试用java程序,据说buildSessionFactory已经不推荐了但是还能用
public class Hibernatetest {
public static void main(String[] args) {
Employee e=new Employee();
e.setId("111");
e.setName("AAA");
Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(e);
session.getTransaction().commit();
session.close();
sf.close();
}
}
可能吧, 你去官网找下6.x的mysql是不是能用5.1的驱动
Employee e=new Employee();
e.setId("222");
e.setName("CCC");
Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
Query query=session.createQuery("select Employee from employee as e");
List<Employee> el=query.list();
for(Employee tem:el)
{
System.out.println(tem.getId());
System.out.println(tem.getName());
}
session.close();
sf.close();
}则会显示employee is not mapped,是否其实是配置问题?但我也没发现配置哪错了啊,求指点,新手,如果是低级错误别打我,真是查了好久都不知道哪错
可能吧, 你去官网找下6.x的mysql是不是能用5.1的驱动
不知道怎么查OTZ不过如果驱动问题的话应该JDBC也失败才对啊
根据提示的错误肯定是hibernate出的问题,你把employee.java文件贴出来,我觉得是数据类型或者映射的问题。
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">这个头版本是错的。
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration
xmlns="http://www.hibernate.org/xsd/hibernate-configuration"
xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernatetest</property>
<property name="connection.username">root</property>
<property name="connection.password">admin</property> <!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property> <!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property> <!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> <!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property> <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/>
</session-factory>
</hibernate-configuration>但是提示Exception in thread "main" org.hibernate.MappingException: invalid configuration
是我没改对么,还是其他的东西也要改,求指点,TAT
根据提示的错误肯定是hibernate出的问题,你把employee.java文件贴出来,我觉得是数据类型或者映射的问题。
这个是employee.java是配置的问题么?TAT求指点
package com.hibernate.bean;public class Employee {
private String id;
private String name; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
<property name="hibernate.hbm2ddl.auto" >update</property>
试一下,意思是加载hibernate自动更新数据库结构,因为你数据库表是自己手动先创建好了
我觉得应该是Query query=session.createQuery("from Employee");就可以了,from后边应该+对象不是+表名称。