修改了一下HibernateSessionFactory 类,操作数据库就出错了

yifawu100 2015-11-03 02:51:10
使用的是MyEclipose 10 ,下载的Hibernate 5.0库,让其自动生成的HibernateSessionFactory .java 文件和其他配置文件。
数据表为Usertable,生成实体文件 Usertable.java,Usertable.hbm.xml映射文件,还有配置文件
hibernate.cfg.xml,然后测试保存数据,成功,记录保存到了数据库中。

因为HibernateSessionFactory .java 文件中的一些语句有些过时。于是将其他改为新的格式如下:

cfg.configure(configFile);
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
sessionFactory = cfg.buildSessionFactory(serviceRegistry);
这样测试程序就出错了。
提示信息:org.hibernate.MappingException: Unknown entity: entity.Usertable

于是再改回这样的
cfg.configure(configFile);
// ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
// sessionFactory = cfg.buildSessionFactory(serviceRegistry);
sessionFactory=cfg.buildSessionFactory();

程序又OK了,现在是下载的最新的5.0.2,难道只能用过时的写法?

...全文
153 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yifawu100 2015-11-03
  • 打赏
  • 举报
回复
少拷贝了几个库文件。要将C3p0下的3个包全部COPY到LIB下面就OK了
  • 打赏
  • 举报
回复
引用 3 楼 yifawu100 的回复:
自己解决了。只是hibernate 提示的错误信息完全不起作用。找了半天才发现原因
原因是什么?
yifawu100 2015-11-03
  • 打赏
  • 举报
回复
自己解决了。只是hibernate 提示的错误信息完全不起作用。找了半天才发现原因
yifawu100 2015-11-03
  • 打赏
  • 举报
回复
系统自动生成的文件: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.SQLServerDialect </property> <property name="connection.url"> jdbc:sqlserver://localhost:1433;databaseName=testdatabase </property> <property name="connection.username">sa</property> <property name="connection.password">12345</property> <property name="connection.driver_class"> com.microsoft.sqlserver.jdbc.SQLServerDriver </property> <property name="myeclipse.connection.profile">myconn</property> <mapping resource="entity/Usertable.hbm.xml" /> </session-factory> </hibernate-configuration> Usertable.hbm.xml 映射文件 <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd "> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="entity.Usertable" table="usertable" schema="dbo" catalog="testdatabase"> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator class="identity" /> </id> <property name="username" type="java.lang.String"> <column name="username" length="10" /> </property> <property name="password" type="java.lang.String"> <column name="password" length="20" /> </property> <property name="birthday" type="java.sql.Timestamp"> <column name="birthday" length="23" /> </property> <property name="age" type="java.lang.Integer"> <column name="age" /> </property> </class> </hibernate-mapping> Usertable.java 实体文件 package entity; import java.sql.Timestamp; /** * Usertable entity. @author MyEclipse Persistence Tools */ public class Usertable implements java.io.Serializable { // Fields private Integer id; private String username; private String password; private Timestamp birthday; private Integer age; // Constructors /** default constructor */ public Usertable() { } /** full constructor */ public Usertable(String username, String password, Timestamp birthday, Integer age) { this.username = username; this.password = password; this.birthday = birthday; this.age = age; } // Property accessors public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return this.username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return this.password; } public void setPassword(String password) { this.password = password; } public Timestamp getBirthday() { return this.birthday; } public void setBirthday(Timestamp birthday) { this.birthday = birthday; } public Integer getAge() { return this.age; } public void setAge(Integer age) { this.age =age; } } 测试代码 Session session=HibernateSessionFactory.getSession(); // ����������� Transaction ts=session.beginTransaction(); Usertable usertable=new Usertable(); usertable.setAge(40); usertable.setUsername("zhangxuyou"); usertable.setPassword("xianggang2"); session.save(usertable); ts.commit(); // �ύ���� Query query=session.createQuery("from Usertable where password='222'"); java.util.List list=query.list(); Usertable user1=(Usertable) list.get(0); System.out.println(user1.getUsername()); System.out.println("aaaa"); HibernateSessionFactory.closeSession();

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧