yswzwh 2013-12-09 05:06 采纳率: 0%
浏览 1634

hibernate初学问题求助

用的是hibernate4.28,在进行配置和映射时,只能用hibernate.properties和Configuration的addResource方法进行配置映射,而hibernate.cfg.xml一点作用也没有起到。
下面是代码和配置文件,第一段测试成功,第二段报错。
public class Test {

public static void main(String[] args) {

Student s = new Student();
s.setId(5);
s.setName("s5");
s.setAge(22);
Configuration cfg = new Configuration().addResource("com/wyf/hibernate/model/Student.hbm.xml");
ServiceRegistry sr = new ServiceRegistryBuilder().buildServiceRegistry();
SessionFactory sf = cfg.buildSessionFactory(sr);
Session session = sf.openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
sf.close();

}

public class TestStudent {

public static void main(String[] args) {

Student s = new Student();
s.setId(6);
s.setName("s6");
s.setAge(22);
Configuration cfg = new Configuration();

cfg.configure();

ServiceRegistry sr=new ServiceRegistryBuilder().buildServiceRegistry();

SessionFactory sf =cfg.buildSessionFactory(sr);

Session session = sf.openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
sf.close();
}

}

这是配置文件

<!-- Database connection settings -->
com.mysql.jdbc.Driver
jdbc:mysql//localhost
root
root

<!-- 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>

<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/wyf/hibernate/model/Student.hbm.xml"/>
<mapping class="com.wyf.hibernate.model.Teacher"/>

  • 写回答

1条回答 默认 最新

  • xuning2516 2013-12-09 12:20
    关注

    报什么错误啊.....

    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配