Configuration config = new Configuration().configure(); 一直報(bào)空指針異常
//1.創(chuàng)建一個(gè)配置對(duì)象
Configuration config = new Configuration().configure();
//2.創(chuàng)建服務(wù)注冊(cè)對(duì)象
// ServiceRegistry serviceRegistry =new?
// ServiceRegistryBuilder().applySettings(config.getProperties()).build();
StandardServiceRegistryBuilder ssrb = new?
StandardServiceRegistryBuilder().applySettings(config.getProperties());
StandardServiceRegistry ssr = ssrb.build();
//3.創(chuàng)建sessionFactory 會(huì)話工廠
SessionFactory sf = config.buildSessionFactory(ssr);
Session session =sf.getCurrentSession();
//創(chuàng)建SchemaExport 生成表結(jié)構(gòu)
SchemaExport export =new SchemaExport(config);
export.create(true, true);
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.current_session_context_class">thread</property>
<!--對(duì)應(yīng)的映射 -->
<mapping resource="entity/Students.hbm.xml" />
<mapping resource="entity/Users.hbm.xml" />
我實(shí)在在不到方法求問慕課的朋友們幫忙一下
2017-07-25
同樣的問題,請(qǐng)問怎么解決的?
2017-03-28
我也遇到了這樣的情況,不知怎么解決
2017-03-26
//創(chuàng)建配置對(duì)象
Configuration config=new Configuration().configure(); ? /*configure()會(huì)默認(rèn)在CLASSPATH下面尋找hibernate.cfg.xml文件
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 如果沒找到,拋出異常hibernateException;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? */
//創(chuàng)建服務(wù)注冊(cè)對(duì)象
ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//創(chuàng)建會(huì)話工廠對(duì)象
sessionFactory=config.buildSessionFactory(serviceRegistry);
//創(chuàng)建會(huì)話對(duì)象
session=sessionFactory.openSession();
//開啟事務(wù)
transaction=session.beginTransaction();
你改成這樣試一下