报错如下~
五月 03, 2016 8:11:56 下午 org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {5.1.0.Final} 五月 03, 2016 8:11:56 下午 org.hibernate.cfg.Environment <clinit> INFO: HHH000206: hibernate.properties not found 五月 03, 2016 8:11:56 下午 org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode provider name : javassist 五月 03, 2016 8:11:56 下午 org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead. Support for obsolete DTD/XSD namespaces may be removed at any time. 五月 03, 2016 8:11:56 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit> INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final} 五月 03, 2016 8:11:56 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!) 五月 03, 2016 8:11:56 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001005: using driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] at URL [jdbc:sqlserver://localhost:1433;database=java_web] 五月 03, 2016 8:11:56 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001001: Connection properties: {user=sa, password=****} 五月 03, 2016 8:11:56 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001003: Autocommit mode: false 五月 03, 2016 8:11:56 下午 org.hibernate.engine.jdbc.connections.internal.PooledConnections <init> INFO: HHH000115: Hibernate connection pool size: 20 (min=1) 五月 03, 2016 8:11:57 下午 org.hibernate.dialect.Dialect <init> INFO: HHH000400: Using dialect: org.hibernate.dialect.SQLServerDialect 五月 03, 2016 8:11:57 下午 org.hibernate.envers.boot.internal.EnversServiceImpl configure INFO: Envers integration enabled? : true org.hibernate.MappingException: Unknown entity: com.wangliang.model.NXSCmodel at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:776) at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1533) at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:104) 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:682) at org.hibernate.internal.SessionImpl.save(SessionImpl.java:674) at org.hibernate.internal.SessionImpl.save(SessionImpl.java:669) at com.wangliang.test.test.insert(test.java:31) at com.wangliang.test.test.main(test.java:52)
失败
model类
package com.wangliang.model; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="javaweb") public class NXSCmodel { private int id; private String name; private String sex; private int age; @Id public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!--配置数据库的驱动程序 --> <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property> <!--设置数据库的连接,其中localhost表示SQLserver服务器名称,此处为本机,1433是SQL的端口号, db_Contact是数据库名 --> <property name="connection.url">jdbc:sqlserver://localhost:1433;database=java_web</property> <!-- 连接数据库的账号 --> <property name="connection.username">sa</property> <!-- 连接数据库的密码 --> <property name="connection.password">123</property> <!--dialect,只是Hibernate使用的数据库方言,就是要用Hibernate连接那种类型的数据库服务器。 --> <property name="dialect">org.hibernate.dialect.SQLServerDialect</property> <!-- sessionFactory.getCurrentSession()可以完成一系列的工作,当调用时, hibernate将session绑定到当前线程,事务结束后,hibernate 将session从当前线程中释放,并且关闭session。当再次调用getCurrentSession ()时,将得到一个新的session,并重新开始这一系列工作。 --> <property name="current_session_context_class">thread</property> <!-- 配置二级缓存 --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- 运行的时候在控制台输出SQL语句 --> <property name="show_sql">true</property> <!-- 输出格式化后的sql,更方便查看 --> <property name="format_sql">true</property> <mapping class="com.wangliang.model.NXSCmodel"/> </session-factory> </hibernate-configuration>
test.java
package com.wangliang.test; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import com.wangliang.model.NXSCmodel; public class test { public static void insert() { SessionFactory factory = null; Session session = null; try { NXSCmodel user = new NXSCmodel(); user.setName("aaa"); user.setAge(123456); user.setSex("男"); Configuration cfg = new Configuration().configure(); ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build(); // 读取hibernate.cfg.xml中的配置 // 获取SessionFactory SessionFactory sf = cfg.buildSessionFactory(sr); // 获取Session session = sf.openSession(); // 开启事务 session.beginTransaction(); // 保存 session.save(user); System.out.print("添加成功"); // 提交事务 session.getTransaction().commit(); // 关闭连接 session.close(); sf.close(); } catch (HibernateException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("失败"); } finally { if (session != null) { if (session.isOpen()) { session.close(); } } } } public static void main(String[] args) { // TODO Auto-generated method stub insert(); } }
没人唔~~~~~你这是缺少配置文件吧不是用了注解就可以不用配置文件吗?
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。