(异常)Hibernate的动态模型

解决办法

不用自带的Hibernate包 自己下载Hibernate包导入

最近出现这样的错误

2015-5-10 12:24:16 org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
2015-5-10 12:24:16 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.4.Final}
2015-5-10 12:24:16 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
2015-5-10 12:24:16 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
2015-5-10 12:24:16 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
2015-5-10 12:24:16 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
2015-5-10 12:24:16 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: org/vo/UserTable.hbm.xml
2015-5-10 12:24:16 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
2015-5-10 12:24:16 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
2015-5-10 12:24:16 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
2015-5-10 12:24:16 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
2015-5-10 12:24:16 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] at URL [jdbc:sqlserver://localhost:1433;DatabaseName=Test9_4]
2015-5-10 12:24:16 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=sa, password=****}
2015-5-10 12:24:17 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.SQLServerDialect
2015-5-10 12:24:17 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
2015-5-10 12:24:17 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
%%%% Error Creating SessionFactory %%%%
java.lang.NullPointerException
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:247)
    at org.hibernate.annotations.common.util.ReflectHelper.classForName(ReflectHelper.java:143)
    at org.hibernate.annotations.common.reflection.java.JavaReflectionManager.classForName(JavaReflectionManager.java:117)
    at org.hibernate.envers.configuration.RevisionInfoConfiguration.configure(RevisionInfoConfiguration.java:262)
    at org.hibernate.envers.configuration.AuditConfiguration.<init>(AuditConfiguration.java:102)
    at org.hibernate.envers.configuration.AuditConfiguration.getFor(AuditConfiguration.java:165)
    at org.hibernate.envers.event.EnversIntegrator.integrate(EnversIntegrator.java:64)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:306)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1744)
    at org.util.HibernateSessionFactory.<clinit>(HibernateSessionFactory.java:34)
    at org.test.HibernateTest.getCurrentSession(HibernateTest.java:26)
    at org.test.HibernateTest.main(HibernateTest.java:13)
2015-5-10 12:24:17 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
2015-5-10 12:24:17 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
2015-5-10 12:24:17 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: org/vo/UserTable.hbm.xml
2015-5-10 12:24:17 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
2015-5-10 12:24:17 org.hibernate.cfg.Configuration$MappingsImpl addImport
INFO: HHH000071: Duplicate import: UserTable -> UserTable
%%%% Error Creating SessionFactory %%%%
org.hibernate.InvalidMappingException: Could not parse mapping document from resource org/vo/UserTable.hbm.xml
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3409)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXmlQueue(Configuration.java:3398)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3386)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1341)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1731)
    at org.util.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:73)
    at org.util.HibernateSessionFactory.getSession(HibernateSessionFactory.java:55)
    at org.test.HibernateTest.getCurrentSession(HibernateTest.java:26)
    at org.test.HibernateTest.main(HibernateTest.java:13)
Caused by: org.hibernate.DuplicateMappingException: Duplicate class/entity mapping UserTable
    at org.hibernate.cfg.Configuration$MappingsImpl.addClass(Configuration.java:2574)
    at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:172)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3406)
    ... 8 more
Exception in thread "main" java.lang.NullPointerException
    at org.test.HibernateTest.saveUser(HibernateTest.java:38)
    at org.test.HibernateTest.main(HibernateTest.java:14)


同学为类包的调用错误

原先我调用的为Hibernate 4.1 core Library和 Hibernate 4.1 Advanced Support librarys

这个是Myeclipse 10.7自带的

后来下载了hibernate-release-4.3.9.Final官网的这个

通过这样引入

复制压缩包下 C:\Users\Administrator\Desktop\hibernate-release-4.3.9.Final\lib\required的 所有必须jar包

技术分享

然后

技术分享

最后引入到下方类包统一起来

技术分享

这样就把Hibernate官方版引入进去了

然后运行结果可以运行

运行结果如下

2015-5-10 12:34:00 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
2015-5-10 12:34:00 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.9.Final}
2015-5-10 12:34:00 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
2015-5-10 12:34:00 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
2015-5-10 12:34:00 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
2015-5-10 12:34:00 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
2015-5-10 12:34:00 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: org/vo/UserTable.hbm.xml
2015-5-10 12:34:00 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
2015-5-10 12:34:00 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
2015-5-10 12:34:00 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] at URL [jdbc:sqlserver://localhost:1433;databaseName=Test9_4]
2015-5-10 12:34:00 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=sa, password=****}
2015-5-10 12:34:00 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
2015-5-10 12:34:00 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
2015-5-10 12:34:01 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.SQLServerDialect
2015-5-10 12:34:01 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
2015-5-10 12:34:01 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
2015-5-10 12:34:02 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
2015-5-10 12:34:02 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
2015-5-10 12:34:02 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000396: Updating schema
2015-5-10 12:34:02 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: Test9_4.dbo.userTable
2015-5-10 12:34:02 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [id, username, password]
2015-5-10 12:34:02 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: []
2015-5-10 12:34:02 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [pk_usertable]
2015-5-10 12:34:02 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete

数据库类

技术分享

这样数据就插入进去了

Test应当这么写

package org.test;
import java.util.*;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.util.HibernateSessionFactory;
//import org.vo.UserTale;
//import org.vo.UserTable;
public class HibernateTest {
	public Session session;
	public static void main(String[] args) {
		//HibernateTest ht = new HibernateTest();		//创建类对象
		//ht.getCurrentSession();	
		//获得session对象
		Configuration cfg=new Configuration();
		SessionFactory sessionFactory=cfg.configure().buildSessionFactory();
		Session session=sessionFactory.openSession();
		Transaction ts = session.beginTransaction();
		Map user = new HashMap();
		user.put("username", "yabber");
		user.put("password", "123456");
		session.save("org.vo.UserTable", user);
		ts.commit();
		
		//ht.saveUser();
		//ht.saveUser();							//插入一条记录
		//ht.updateUser();              			//修改该条记录
		//ht.queryUser();								//查看数据库结果
		//ht.deleteUser();              				//删除该条记录
		//System.out.println("删除该条记录后结果======");
		//ht.queryUser();								//查看数据库结果
		//ht.closeSession();             				//关闭session
	}
	
	//获得session方法
	public void getCurrentSession(){
		//调用 HibernateSessionFactory 的 getSession 方法创建 Session 对象
		session = HibernateSessionFactory.getSession();
	}
	//关闭session方法
	public void closeSession(){
		if(session!=null){
			HibernateSessionFactory.closeSession();	// 关闭Session
		}
	}
	
	//插入一条记录方法
	public void saveUser(){
		//保存一个UserTable对象
		Transaction ts = session.beginTransaction();
		Map user = new HashMap();
		user.put("username", "yabber");
		user.put("password", "123456");
		session.save("org.vo.UserTable", user);
		ts.commit();
	}
	
	//修改这条记录方法
	public void updateUser(){
		//更新一个UserTable对象
		Transaction ts = session.beginTransaction();
		Map user = new HashMap();
		user = (Map)session.get("org.vo.UserTable", 18);						// 获取id为18的记录
		user.put("password", "654321");
		session.update("org.vo.UserTable", user);
		ts.commit();
	}
	
	//查询数据库结果方法
	public void queryUser(){
		Query query = session.createQuery("from UserTable");    				//查询记录
		List list = query.list();
		for(int i=0;i<list.size();i++){
			Map user = (Map)list.get(i);
			System.out.println(user.get("username"));
			System.out.println(user.get("password"));
		}
	}
	
	//删除该条记录方法
	public void deleteUser(){
		Transaction ts = session.beginTransaction();
		Map user = new HashMap();
		user = (Map)session.get("org.vo.UserTable", 18);
		session.delete("org.vo.UserTable", user);
		ts.commit();
	}
}
UserTable.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class entity-name="org.vo.UserTable" table="userTable">
            <id name="id" column="id" type="integer">  
                <generator class="identity"/>  
            </id>  
            <property name="username" type="string" column="username"/>  
            <property name="password" type="string" column="password"/>  
    </class>
</hibernate-mapping>

Hibernate.hbm.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
        <property name="hbm2ddl.auto">update</property>
        <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
        <property name="connection.url">jdbc:sqlserver://localhost:1433;databaseName=Test9_4</property>
        <property name="connection.username">sa</property>
        <property name="connection.password">123</property>
        <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <mapping resource="org/vo/UserTable.hbm.xml" />
    </session-factory>

</hibernate-configuration>

动态类是MAP映射 而非POJO映射

关于介绍MAP映射的文章

1 http://blog.csdn.net/meng2602956882/article/details/22914493

2 http://www.360doc.com/content/14/0329/19/281812_364737677.shtml

3 http://blog.csdn.net/opnmzxcvb/article/details/4785569

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。