[Hibernate开发之路](2)Hibernate问题
最后更新于:2022-04-01 09:59:09
###一.数据库驱动问题
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.HibernateException: JDBC Driver class not found: com.microsoft.sqlserver.jdbc.SQLServerDriver
at org.hibernate.connection.DriverManagerConnectionProvider.configure(DriverManagerConnectionProvider.java:89)
at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:137)
at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:79)
at org.hibernate.cfg.SettingsFactory.createConnectionProvider(SettingsFactory.java:425)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:89)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2119)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2115)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1339)
at com.test.Test.main(Test.java:17)
Caused by: java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:192)
at org.hibernate.connection.DriverManagerConnectionProvider.configure(DriverManagerConnectionProvider.java:84)
... 8 more
解决:
(1)找不到这个jar包,你确定导入了吗?web项目的话放到WEB-INF下的lib目录,java项目的话需要add to build path。
(2)你找找看你这个jar包里面有没有com.microsoft.sqlserver.jdbc.SQLServerDriver这个类,没有的话就是jar包不对。
(3)看清楚 有的jar包是com.microsoft.sqlserver.jdbc.SQLServerDriver有的jar包是com.microsoft.jdbc.sqlserver.SQLServerDriver
下载地址:[点击打开链接](http://download.csdn.net/detail/sunnyyoona/7791983)
### 二 .AnnotationConfiguration问题
问题:AnAnnotationConfiguration instance is required to use <mappingclass="com.model.TeacherInfo"/>
最近学习Hibernate,尝试使用XML和Annotation两种方式进行POJO类的映射,有两个类:Student和Teacher,前者使用XML文件来映射类和属性,后者使用Annotation映射类和属性,并放在一个工程下。Hibernate的配置文件——hibernate.cfg.xml位于src目录下。在单元测试时,执行下面代码时,会产生异常。
~~~
Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
~~~
异常信息为:org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="com.model.TeacherInfo"/>
原因分析:
Hibernate配置文件中,若带有<mapping class="com.model.TeacherInfo"/>,则说明映射类时,采用了Annotation方式。在初始化Configuation时,应使用AnnoationConfiguration,代码如下:
~~~
Configuration cfg = new AnnoationConfiguration();
SessionFactory sf = cfg.configure().buildSessionFactory();
~~~
如果,你在映射时,未使用过Annotation,则使用Configuration。