News.javapackage mc.bean;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;@Entity@Table(name="news_inf")public class News { // 消息类的标识属性 @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Integer id; // 消息标题 private String title; // 消息内容 private String content; // id的setter和getter方法 public void setId(Integer id) { this.id = id; } public Integer getId() { return this.id; } // title的setter和getter方法 public void setTitle(String title) { this.title = title; } public String getTitle() { return this.title; } // content的setter和getter方法 public void setContent(String content) { this.content = content; } public String getContent() { return this.content; }}Main方法package mc.bean;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;public class MainTest { public static void main(String[] args) { // TODO Auto-generated method stub // 实例化Configuration,不加载任何配置文件 Configuration conf = new Configuration() // 通过addAnnotatedClass()方法添加持久化类 .addAnnotatedClass(mc.bean.News.class) // 通过setProperty设置Hibernate的连接属性。 .setProperty("hibernate.connection.driver_class" , "com.mysql.jdbc.Driver") .setProperty("hibernate.connection.url" , "jdbc:mysql://localhost/mctest") .setProperty("hibernate.connection.username" , "root") .setProperty("hibernate.connection.password" , "123457") .setProperty("hibernate.c3p0.max_size" , "20") .setProperty("hibernate.c3p0.min_size" , "1") .setProperty("hibernate.c3p0.timeout" , "5000") .setProperty("hibernate.c3p0.max_statements" , "100") .setProperty("hibernate.c3p0.idle_test_period" , "3000") .setProperty("hibernate.c3p0.acquire_increment" , "2") .setProperty("hibernate.c3p0.validate" , "true") .setProperty("hibernate.dialect" , "org.hibernate.dialect.MySQLDialect") .setProperty("hibernate.hbm2ddl.auto" , "update"); // 以Configuration实例创建SessionFactory实例 ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(conf.getProperties()).build(); SessionFactory sf = conf.buildSessionFactory(serviceRegistry); // 实例化Session Session sess = sf.openSession(); // 开始事务 Transaction tx = sess.beginTransaction(); // 创建消息实例 News n = new News(); // 设置消息标题和消息内容 n.setTitle("00000"); n.setContent("0000000"); // 保存消息 sess.save(n); // 提交事务 tx.commit(); System.out.println("操作成功!"); // 关闭Session sess.close(); }}异常十一月 12, 2016 3:11:45 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}十一月 12, 2016 3:11:45 下午 org.hibernate.Version logVersionINFO: HHH000412: Hibernate Core {4.3.5.Final}Exception in thread "main" java.lang.ExceptionInInitializerError at org.hibernate.cfg.Configuration.reset(Configuration.java:324) at org.hibernate.cfg.Configuration.<init>(Configuration.java:289) at org.hibernate.cfg.Configuration.<init>(Configuration.java:293) at mc.bean.MainTest.main(MainTest.java:15)Caused by: java.lang.NullPointerException at org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:170) at org.hibernate.cfg.Environment.<clinit>(Environment.java:221) ... 4 more
- 1 回答
- 1 关注
- 3168 浏览
添加回答
举报
0/150
提交
取消