Maven3 + Hibernate3.6 + MySQL5简单示例

 

Maven3 + Hibernate3.6 + MySQL5简单示例

参考:

http://www.xue-java.com/courses/hibernate/01.html

http://www.xue-java.com/courses/hibernate/02.html

 

1、在mysql5中创建数据库 :
  create database stud ;
2、创建表DBUser:
  use stud ;
 create table DBUser(
   USER_ID int NOT NULL PRIMARY KEY,
   USERNAME varchar(20) NOT NULL,
   CREATED_BY varchar(20) NOT NULL,
   CREATED_DATE date NOT NULL) type=innodb ;
3、使用Maven创建项目结构
使用Maven来创建一个标准的项目结构。
...>cd E:\maven3
E:\maven3>

mvn archetype:generate -DgroupId=com.studjava -DartifactId=HibernateExam -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

注:使用Maven创建Java项目的详细介绍,请参见本站Maven教程:使用Maven模板快速创建Java项目。

4、转为Eclipse项目
将生成的Maven原始项目转换为Eclipse项目。(有关将Maven项目转换为Eclipse IDE项目,请参考“将基于Maven的Java项目转换为Eclipse IDE支持项目”)

首先定位到HibernateExam目录下(在命令行中,使用cd命令),然后使用如下的命令,将生成的项目模板转换为Eclipse IDE项目。

maven转换命令    mvn eclipse:eclipse 
同时,所有的依赖库也会被下载。稍后,只需要将其导入到Eclipse IDE即可进一步地开发。方法如下:

在Eclipse IDE中,选择【File】|【Import】|【General folder,Existing Projects into Workspace】,然后选择项目所在的目录路径,按【完成】即可。

5、添加Hibernate和MySQL依赖
修改pom.xml,添加对Hibernate和MySQL的支持。Hibernate要求dom4j、commons-logging、commons-collections、slf4j-api-1.6.1、hibernate-jpa-2.0-api-1.0.0.Final.jar、javassist-3.12.0.GA.jar和cglib这些依赖库。象下面这样添加它们:

pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.studjava</groupId>
  <artifactId>HibernateExam</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>HibernateExam</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- MySQL database driver -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.0.8</version>
    </dependency>
    <!-- Hibernate Framework -->
    <dependency>
       <groupId>hibernate</groupId>
       <artifactId>hibernate3</artifactId>
       <version>3.6.0.Final</version>
    </dependency>
    <!-- Hibernate library dependency start -->
    <dependency>
      <groupId>dom4j</groupId>
      <artifactId>dom4j</artifactId>
      <version>1.6.1</version>
    </dependency>
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.1.1</version>
    </dependency>
    <dependency>
      <groupId>commons-collections</groupId>
      <artifactId>commons-collections</artifactId>
      <version>3.1</version>
    </dependency>
    <dependency>
      <groupId>cglib</groupId>
      <artifactId>cglib</artifactId>
      <version>2.2</version>
    </dependency>
    <!-- Hibernate lib dependency end -->
    <dependency>
     <groupId>slf4j-api</groupId>
     <artifactId>slf4j-api</artifactId>
     <version>1.6.1</version>
    </dependency>
    <!-- slf4j-simple.1.6.0.jar
 <dependency>
     <groupId>slf4j-simple</groupId>
     <artifactId>slf4j-simple</artifactId>
     <version>1.6.0</version>
    </dependency>
 -->
    <dependency>
     <groupId>org.hibernate.javax.persistence</groupId>
     <artifactId>hibernate-jpa-2.0-api </artifactId>
     <version>1.0.0.Final</version>
    </dependency>
    <dependency>
     <groupId>javassist</groupId>
     <artifactId>javassist</artifactId>
     <version>3.12.0.GA</version>
    </dependency>
    <dependency>
      <groupId>javax.transaction</groupId>
      <artifactId>jta</artifactId>
      <version>1.1</version>
    </dependency>
   
   
  </dependencies>
</project>


在命令行下(项目目录中),键入“mvn eclipse:eclipse”命令,Maven将自动地下载所有的Hibernate和MySQL库,并将这些库放入到Maven的本地资源中。同时,Maven将会把下载的库添加到Eclipse的“.classpath”中。(有Maven真好,不需要查找库并亲自拷贝它)

如果没有找到“hibernate3.jar”,可以手工从网上下载,然后安装到本地Maven资源库中。安装时使用如下的命令:

mvn install:install-file -DgroupId=hibernate -DartifactId=hibernate3 -Dversion=3.6.0.Final -Dpackaging=jar -Dfile=hibernate3.jar 

a、E:\Maven3\HibernateExam>mvn install:install-file -DgroupId=hibernate -DartifactId=hibernate3 -Dversion=3.6.0.Final -Dpackaging=jar  -Dfile=E:\maven3\hibernate3.jar(需要指明路径)

b、E:\Maven3\HibernateExam>mvn install:install-file -DgroupId=slf4j-api -DartifactId=slf4j-api -Dversion=1.6.1 -Dpackaging=jar  -Dfile=E:\mvc.jar\slf4j-api-1.6.1.jar

c、
 
javassist-3.12.0.GA.jar

E:\Maven3\HibernateExam>mvn install:install-file -DgroupId=javassist -DartifactId=javassist -Dversion=3.12.0.GA -Dpackaging=jar  -Dfile=E:\mvc.jar\javassist-3.12.0.GA.jar


d:\Maven3\HibernateExam>mvn install:install-file -DgroupId=org.hibernate.javax.persistence -DartifactId=hibernate-jpa-2.0-api -Dversion=1.0.0.Final -Dpackaging=jar  -Dfile=E:\mvc.jar\hibernate-jpa-2.0-api-1.0.0.Final.jar
注意: -Dfile 所指定的文件的路径是相对于当前你所执行命令cmd 所显示的位置的。

注: 如 jar包为 javassist-3.12.0.GA.jar,则pom.xml中的依赖为 :
    <dependency>
     <groupId>javassist</groupId>
     <artifactId>javassist</artifactId>
     <version>3.12.0.GA</version>
    </dependency>


6、创建Hibernate映射文件(hbm)和Model类
在一开始,我们首先创建了一个名为"DBUser"的数据表,现在创建一个Hibernate XML映射文件,以及与表"DBUser"对应的model类。

首先创建如下的"DBUser.hbm.xml"文件并将它放到"src/main/resources/com/studjava"下面。(如果该目录不存在,则先创建它)

DBUser.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
 <class name="com.studjava.DBUser" table="DBUser">
  <id name="userId" type="java.lang.Integer">
   <column name="USER_ID" />
   <generator class="assigned"/>
  </id>
  <property name="username" type="string">
   <column name="USERNAME" length="20" not-null="true" unique="true" />
  </property>
  <property name="createdBy" type="string">
   <column name="CREATED_By" length="20" not-null="true" unique="true" />
  </property>
  <property name="createdDate" type="date">
   <column name="CREATED_DATE" length="7" not-null="true" />
  </property>
 </class>
</hibernate-mapping>


接下来,创建一个"DBUser.java"文件并将它放到"src/main/java/com/studjava"目录下。

DBUser.java


package com.studjava;

import java.util.Date;

public class DBUser {

 private Integer userId ;
 private String username ;
 private String createdBy ;
 private Date createdDate;
 
 public DBUser(){
  
 }
 
 public Integer getUserId(){
  return userId ;
 }
 public void setUserId(Integer userId){
  this.userId = userId ;
 }

 public String getUsername() {
  return username;
 }

 public void setUsername(String username) {
  this.username = username;
 }

 public String getCreatedBy() {
  return createdBy;
 }

 public void setCreatedBy(String createdBy) {
  this.createdBy = createdBy;
 }

 public Date getCreatedDate() {
  return createdDate;
 }

 public void setCreatedDate(Date createdDate) {
  this.createdDate = createdDate;
 }
 
 @Override
 public String toString(){
  return super.toString();
 } 
}


注:在一个大型的应用程序中,创建模型类和映射文件是非常麻烦的。以上代码可以使用Hibernate工具来自动生成。请参见本教程中的“Eclipse+Hibernate工具自动地生成Hibernate映射文件”一章。


7、创建Hibernate配置文件
创建一个Hibernate配置文件"hibernate.cfg.xml”,并将它放到资源目录的根目录下,"src/main/resources/hibernate.cfg.xml”,并填写相应的MySQL数据库信息。

<?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="hibernate.bytecode.use_reflection_optimizer">false</property>
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/stud</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.connection.password">root</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="show_sql">true</property>
  
  <mapping resource="com/studjava.DBUser.hbm.xml"/>
  
 </session-factory>
</hibernate-configuration>

设置“show_sql”属性为true,将会输出Hibernate SQL语句。Hibernate Dialect是用来告诉Hibernate应用程序它将生成哪一种SQL与数据库通信(数据库方言)。


8、创建Hibernate实用工具类
创建一个经典的"HibernateUtil.java"类来管理Hibernate session。将该类放到"src/main/java/com/studjava/persistence/HibernateUtil.java"目录下。

HibernateUtil.java

package com.studjava.persistence;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class HibernateUtil {

 private static final SessionFactory sessionFactory = buildSessionFactory() ;
 
 public static SessionFactory buildSessionFactory(){
  
  try{
   // Create the SessionFactory from hibernate.cfg.xml
   return new Configuration().configure().buildSessionFactory() ;
  }catch(Throwable e){
   // Make sure you log the exception, as it might be swallowed
   System.out.println("initial SessionFactory creation failed." + e);
   throw new ExceptionInInitializerError(e) ;
  }
 }
 
 public static SessionFactory getSessionFactory(){
  
  return sessionFactory ;
 }
 
 public static void shutdown(){
  // Close caches and connection pools
  getSessionFactory().close() ;
 }
}


9、Hibernate编码
修改“App.java”如下:

package com.studjava;

import java.util.Date;

import org.hibernate.Session;

import com.studjava.persistence.HibernateUtil;

/**
 * Hello world!
 *
 */
public class App
{
    public static void main( String[] args )
    {
//        System.out.println( "Hello World!" );
     System.out.println("Maven3 + Hibernate3.6 + MySQL ");
     Session session = HibernateUtil.getSessionFactory().openSession() ;
     
     session.beginTransaction() ;
     
     DBUser user = new DBUser() ;
     user.setUserId(1) ;
     user.setUsername("AA") ;
     user.setCreatedBy("system") ;
     user.setCreatedDate(new Date()) ;
     session.save(user) ;
     session.getTransaction().commit() ;
     
    }
}
 
10、运行程序
运行App.java,将会插入一条新的记录到“DBUser”表:

Maven3 + Hibernate3.6 + MySQL
468 [main] INFO org.hibernate.annotations.common.Version - Hibernate Commons Annotations 3.2.0.Final
500 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.6.0.Final
500 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
515 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
531 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
843 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
843 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
1078 [main] WARN org.hibernate.util.DTDEntityResolver - recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
1218 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : com/studjava/DBUser.hbm.xml
1328 [main] WARN org.hibernate.util.DTDEntityResolver - recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
1468 [main] INFO org.hibernate.cfg.Configuration - Configured SessionFactory: null
1703 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: com.studjava.DBUser -> DBUser
1781 [main] INFO org.hibernate.cfg.Configuration - Hibernate Validator not found: ignoring
1812 [main] INFO org.hibernate.cfg.search.HibernateSearchEventListenerRegister - Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled.
1828 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!)
1828 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Hibernate connection pool size: 20
1828 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - autocommit mode: false
1890 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/stud
1890 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - connection properties: {user=root, password=****}
2765 [main] INFO org.hibernate.cfg.SettingsFactory - Database ->
       name : MySQL
    version : 5.0.27-community-log
      major : 5
      minor : 0
2765 [main] INFO org.hibernate.cfg.SettingsFactory - Driver ->
       name : MySQL-AB JDBC Driver
    version : mysql-connector-java-5.0.8 ( Revision: ${svn.Revision} )
      major : 5
      minor : 0
2859 [main] INFO org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.MySQLDialect
2906 [main] INFO org.hibernate.engine.jdbc.JdbcSupportLoader - Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
2906 [main] INFO org.hibernate.transaction.TransactionFactoryFactory - Using default transaction strategy (direct JDBC transactions)
2906 [main] INFO org.hibernate.transaction.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
2906 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled
2906 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic session close at end of transaction: disabled
2906 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC batch size: 15
2906 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC batch updates for versioned data: disabled
2922 [main] INFO org.hibernate.cfg.SettingsFactory - Scrollable result sets: enabled
2922 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): enabled
2922 [main] INFO org.hibernate.cfg.SettingsFactory - Connection release mode: auto
2922 [main] INFO org.hibernate.cfg.SettingsFactory - Maximum outer join fetch depth: 2
2922 [main] INFO org.hibernate.cfg.SettingsFactory - Default batch fetch size: 1
2922 [main] INFO org.hibernate.cfg.SettingsFactory - Generate SQL with comments: disabled
2922 [main] INFO org.hibernate.cfg.SettingsFactory - Order SQL updates by primary key: disabled
2922 [main] INFO org.hibernate.cfg.SettingsFactory - Order SQL inserts for batching: disabled
2922 [main] INFO org.hibernate.cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
2922 [main] INFO org.hibernate.hql.ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory
2937 [main] INFO org.hibernate.cfg.SettingsFactory - Query language substitutions: {}
2937 [main] INFO org.hibernate.cfg.SettingsFactory - JPA-QL strict compliance: disabled
2937 [main] INFO org.hibernate.cfg.SettingsFactory - Second-level cache: enabled
2937 [main] INFO org.hibernate.cfg.SettingsFactory - Query cache: disabled
2937 [main] INFO org.hibernate.cfg.SettingsFactory - Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory
2937 [main] INFO org.hibernate.cfg.SettingsFactory - Optimize cache for minimal puts: disabled
2937 [main] INFO org.hibernate.cfg.SettingsFactory - Structured second-level cache entries: disabled
2953 [main] INFO org.hibernate.cfg.SettingsFactory - Echoing all SQL to stdout
2968 [main] INFO org.hibernate.cfg.SettingsFactory - Statistics: disabled
2968 [main] INFO org.hibernate.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled
2968 [main] INFO org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo
2968 [main] INFO org.hibernate.cfg.SettingsFactory - Named query checking : enabled
2968 [main] INFO org.hibernate.cfg.SettingsFactory - Check Nullability in Core (should be disabled when Bean Validation is on): enabled
3047 [main] INFO org.hibernate.impl.SessionFactoryImpl - building session factory
4203 [main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
Hibernate: insert into DBUser (USERNAME, CREATED_By, CREATED_DATE, USER_ID) values (?, ?, ?, ?)

 

 

 

 

 

 

 

 

 

 

修改本地仓库路径

修改仓库路径

从eclipse中增加了maven2的插件之后,maven默认的本地库的路径是${user}/.m2/repository/下,一般windows用户的操作系统都安装在C盘,所以这个目录下的jar包比较危险。我尝试从myeclipse->preferences->java->build path->classpath variables中查找M2_REPO这个变量,发现其路径指向的是“C:\Documents and Settings\lvxda\.m2\repository\”,我想修改其路径为:d:\m2\repository,可是这个变量的编辑按钮是灰色的,不可编辑。从网上也没有找到相应的解决办法。打算自己尝试一下:

1、手动创建本地仓库的地址为:d:/m2/repository

2、从windows的环境变量中增加了:M2_REPO=d:\m2\repository,同时把这个变量增加到path变量中。

3、修改d:\maven\conf\setting.xml文件,在<localRepository>节点下新增一行:
复制内容到剪贴板
代码:
<localRepository>d:/m2/repository</localRepository>
表示本地仓库的地址为:d:/m2/repository。
4、从myeclipse->preferences->maven->installations下add一个已经安装到d盘的maven,如:d:\maven,并勾选此新加项。并点击下面的browse按钮打开maven的全局配置文件,如:d:\maven\conf\setting.xml。

5、从myeclipse->preferences->maven->installations下修改user setting 选项为:D:\m2\repository\settings.xml,并点击update settings。并点击下面的reindex按钮更新索引。

6、点击配置窗口的apply按钮,突出配置窗口。

7、配置修改后,myeclise会自动更新索引,当完成后重启myeclipse,会发现M2_REPO变量的值变成了d:/m2/repository。

自此,maven从远程库下载的jar包都会放到d:/m2/repository下。为了避免每次构建系统都从远程仓库下载jar,需要搭建私服。

 


此次例子过程中出现的错误说明:
1、
错误信息:
initial SessionFactory creation failed.org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
Exception in thread "main" java.lang.ExceptionInInitializerError
 at com.studjava.persistence.HibernateUtil.buildSessionFactory(HibernateUtil.java:19)
 at com.studjava.persistence.HibernateUtil.<clinit>(HibernateUtil.java:9)
 at com.studjava.App.main(App.java:19)
Caused by: org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
 at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:108)
 at org.hibernate.tuple.entity.EntityTuplizerFactory.constructDefaultTuplizer(EntityTuplizerFactory.java:133)
 at org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.<init>(EntityEntityModeToTuplizerMapping.java:80)
 at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:322)
 at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:473)
 at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:133)
 at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:84)
 at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:284)
 at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1842)
 at com.studjava.persistence.HibernateUtil.buildSessionFactory(HibernateUtil.java:15)
 ... 2 more
Caused by: java.lang.reflect.InvocationTargetException
 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
 at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
 at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:105)
 ... 11 more
Caused by: java.lang.NoClassDefFoundError: javassist/util/proxy/MethodFilter
 at org.hibernate.bytecode.javassist.BytecodeProviderImpl.getProxyFactoryFactory(BytecodeProviderImpl.java:49)
 at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactoryInternal(PojoEntityTuplizer.java:205)
 at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactory(PojoEntityTuplizer.java:183)
 at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:167)
 at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:77)
 ... 16 more


修改方法:
   追踪到 Caused by: java.lang.NoClassDefFoundError: javassist/util/proxy/MethodFilter ,是缺少javassist.3.12.0.GA.jar;
   通过在POM.xml中加入
   <dependency>
     <groupId>javassist</groupId>
     <artifactId>javassist</artifactId>
     <version>3.12.0.GA</version>
    </dependency>
 
 然后 通过
 E:\Maven3\HibernateExam(项目目录)>mvn install:install-file -DgroupId=javassist -DartifactId=javassist -Dversion=3.12.0.GA -Dpackaging=jar  -Dfile=E:\mvc.jar\javassist-3.12.0.GA.jar
 E:\mvc.jar\javassist-3.12.0.GA.jar--为jar包的本地目录 ,如果通过 mvn eclipse:eclipse 不能安装 javassist-3.12.0.GA.jar ,则通过之上的脚本安装 ;
2、
错误信息:
initial SessionFactory creation failed.java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
Exception in thread "main" java.lang.ExceptionInInitializerError
 at com.studjava.persistence.HibernateUtil.buildSessionFactory(HibernateUtil.java:19)
 at com.studjava.persistence.HibernateUtil.<clinit>(HibernateUtil.java:9)
 at com.studjava.App.main(App.java:19)
Caused by: java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
 at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:196)
 at com.studjava.persistence.HibernateUtil.buildSessionFactory(HibernateUtil.java:15)
 ... 2 more
修改方法:
 追踪到Caused by: java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory,缺少slf4j-api.1.6.1.jar ;
 通过在POM.xml中加入
  <dependency>
     <groupId>slf4j-api</groupId>
     <artifactId>slf4j-api</artifactId>
     <version>1.6.1</version>
    </dependency>
 
 mvn install:install-file -DgroupId=slf4j-api -DartifactId=slf4j-api -Dversion=1.6.1 -Dpackaging=jar  -Dfile=E:\mvc.jar\slf4j-api-1.6.1.jar
3、
错误信息:
Maven3 + Hibernate3.6 + MySQL
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.ExceptionInInitializerError
 at com.studjava.persistence.HibernateUtil.buildSessionFactory(HibernateUtil.java:19)
 at com.studjava.persistence.HibernateUtil.<clinit>(HibernateUtil.java:9)
 at com.studjava.App.main(App.java:19)
Caused by: java.lang.NoClassDefFoundError: javax/persistence/EntityListeners
 at org.hibernate.cfg.annotations.reflection.JPAMetadataProvider.getDefaults(JPAMetadataProvider.java:96)
 at org.hibernate.annotations.common.reflection.java.JavaReflectionManager.getDefaults(JavaReflectionManager.java:226)
 at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1355)
 at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1826)
 at com.studjava.persistence.HibernateUtil.buildSessionFactory(HibernateUtil.java:15)
 ... 2 more
initial SessionFactory creation failed.java.lang.NoClassDefFoundError: javax/persistence/EntityListeners


修改方法:
 Caused by: java.lang.NoClassDefFoundError: javax/persistence/EntityListeners,缺少hibernate-jpa-2.0-api-1.0.0.Final.jar ;
 通过在POM.xml中加入
     <dependency>
     <groupId>hibernate-jpa</groupId>
     <artifactId>hibernate-jpa-2.0-api </artifactId>
     <version>1.0.0.Final</version>
    </dependency>
 
 mvn install:install-file -DgroupId=hibernate-jpa -DartifactId=hibernate-jpa-2.0-api -Dversion=1.0.0.Final -Dpackaging=jar  -Dfile=E:\mvc.jar\hibernate-jpa-2.0-api-1.0.0.Final.jar
 
使用jar包
mysql-connector-java-5.0.8-bin.jar
commons-logging-1.1.1.jar
commons-collections-3.1.jar
cglib-2.2.jar
slf4j-api-1.6.1.jar
jta-1.1.jar
hibernate-jpa-2.0-api-1.0.0.Final.jar
javassist-3.12.0.GA.jar
hibernate3.jar --hibernate-distribution-3.6.0.Final
asm-3.1.jar
javassist-3.12.0.GA.jar


 

你可能感兴趣的:(eclipse,maven,Hibernate,mysql,jdbc,jar)