hibernate的配置出了错误。hibernate.cfg.xml放在src下。
只有一个包com.wdc,在包里有News.Java 和 NewsManager.java
代码中News.java利用了注释
看了以前人们的帖子,将News.hbm.xml配置到了hibernate.cfg.xml中,还是不对。
错误代码:
只有一个包com.wdc,在包里有News.Java 和 NewsManager.java
代码中News.java利用了注释
看了以前人们的帖子,将News.hbm.xml配置到了hibernate.cfg.xml中,还是不对。
错误代码:
INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException 十一月 30, 2015 4:08:23 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: HHH000228: Running hbm2ddl schema update Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.wdc.News at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:776) at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1462) at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:100) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192) at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177) at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73) at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:678) at org.hibernate.internal.SessionImpl.save(SessionImpl.java:670) at org.hibernate.internal.SessionImpl.save(SessionImpl.java:665) at com.wdc.NewsManager.main(NewsManager.java:33)
hibernate.cfg.xml
<?xml version="1.0" encoding="GBK"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory name=""> <!-- 指定连接数据库所用的驱动 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 指定连接数据库的url,其中hibernate是本应用连接的数据库名 --> <property name="connection.url">jdbc:mysql://localhost/javaee</property> <!-- 指定连接数据库的用户名 --> <property name="connection.username">root</property> <!-- 指定连接数据库的密码 --> <property name="connection.password">root</property> <!-- 指定连接池里最大连接数 --> <property name="hibernate.c3p0.max_size">20</property> <!-- 指定连接池里最小连接数 --> <property name="hibernate.c3p0.min_size">1</property> <!-- 指定连接池里连接的超时时长 --> <property name="hibernate.c3p0.timeout">5000</property> <!-- 指定连接池里最大缓存多少个Statement对象 --> <property name="hibernate.c3p0.max_statements">100</property> <property name="hibernate.c3p0.idle_test_period">3000</property> <property name="hibernate.c3p0.acquire_increment">2</property> <property name="hibernate.c3p0.validate">true</property> <!-- 指定数据库方言 --> <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <!-- 根据需要自动创建数据表 --> <property name="hbm2ddl.auto">update</property> <!--① 显示Hibernate持久化操作所生成的SQL --> <property name="show_sql">true</property> <!-- 将SQL脚本进行格式化后再输出 --> <property name="hibernate.format_sql">true</property> <!-- 罗列全部持久化类的类名 --> <mapping class="com.wdc.News"/> </session-factory> </hibernate-configuration>
News.java
package com.wdc; import javax.persistence.*; @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; } }
NewsManager.java
package com.wdc; import org.hibernate.*; import org.hibernate.cfg.*; import org.hibernate.service.*; import org.hibernate.boot.registry.*; public class NewsManager { public static void main(String[] args) throws Exception { // 实例化Configuration, Configuration conf = new Configuration().configure(); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(conf.getProperties()).build(); // 以Configuration实例创建SessionFactory实例 SessionFactory sf = conf.buildSessionFactory(serviceRegistry); // 创建Session Session sess = sf.openSession(); // 开始事务 Transaction tx = sess.beginTransaction(); // 创建消息对象 News n = new News(); // 设置消息标题和消息内容 n.setTitle("123"); n.setContent("abc"); // 保存消息 sess.save(n); // 提交事务 tx.commit(); // 关闭Session sess.close(); sf.close(); } }
解决方案:40分
正常用注解+扫描器,就不需要XML了,本人觉得你还是看看注解怎么用吧。