程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> java-Java Hibernate運行問題

java-Java Hibernate運行問題

編輯:編程綜合問答
Java Hibernate運行問題

錯誤信息

 java.lang.NoClassDefFoundError: javax/transaction/SystemException
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at org.jboss.logging.Logger$1.run(Logger.java:2554)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.jboss.logging.Logger.getMessageLogger(Logger.java:2529)
    at org.jboss.logging.Logger.getMessageLogger(Logger.java:2516)
    at org.hibernate.internal.CoreLogging.messageLogger(CoreLogging.java:28)
    at org.hibernate.internal.CoreLogging.messageLogger(CoreLogging.java:24)
    at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:86)
    at com.lee.hibernate.HibernateTest.test(HibernateTest.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.ClassNotFoundException: javax.transaction.SystemException
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 33 more

hibernate.cfg.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">
<hibernate-configuration>
    <session-factory>

        <!-- 數據庫的基本信息 -->
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>

        <!-- Hibernate基本信息 -->
        <!-- Hibernate設置數據庫方言 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>

        <!-- 是否在控制台顯示SQL語句 -->
        <property name="show_sql">true</property>
        <!-- 格式化SQL語句 -->
        <property name="format_sql">true</property>
        <!-- 指定自動生成數據表策略 -->
        <property name="hibernate.hbm2ddl.auto">create</property>

        <!-- 關聯.hbm.xml對象 -->
        <mapping resource="/com/lee/hibernate/News.hbm.xml"/>

    </session-factory>
</hibernate-configuration>

News.hbm.xml

 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2015-11-27 21:27:39 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="com.lee.hibernate.News" table="NEWS">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="title" type="java.lang.String">
            <column name="TITLE" />
        </property>
        <property name="author" type="java.lang.String">
            <column name="AUTHOR" />
        </property>
        <property name="date" type="java.sql.Date">
            <column name="DATE" />
        </property>
    </class>
</hibernate-mapping>

News.java

 package com.lee.hibernate;

import java.sql.Date;

public class News {
    private Integer id;
    private String title;
    private String author;
    private Date date;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public News() {
        super();
    }

    public News(String title, String author, Date date) {
        super();
        this.title = title;
        this.author = author;
        this.date = date;
    }

    @Override
    public String toString() {
        return "News [id=" + id + ", title=" + title + ", author=" + author + ", date=" + date + "]";
    }

}

HibernateTest.java

 package com.lee.hibernate;

import java.sql.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
//import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
//import org.hibernate.service.ServiceRegistry;
import org.junit.Test;

public class HibernateTest {

    @Test
    public void test() {

        //1 創建一個SessionFactory對象
        SessionFactory sessionFactory = null;
        //1) 創建Configuration對象: 對應hibernate的基本配置信息和對象映射信息
        Configuration configuration = new Configuration().configure();

        sessionFactory = configuration.buildSessionFactory();

        //2)創建sessionRegistry對象: hibernate 4.x新添加的對象
        //hibernate的任何配置和服務都需要在該對象注冊後才能有效.
//      ServiceRegistry serviceRegistry = 
//              new StandardServiceRegistryBuilder().
//                  applySettings(configuration.getProperties())
//                      .build();


        //3)
//      sessionFactory = configuration.buildSessionFactory(sessionRegistry);

        //2 創建一個Session對象
        Session session = sessionFactory.openSession();

        //3 開啟事務
        Transaction transaction = session.beginTransaction();

        //4 執行保存操作
        News news = new News("Java", "Lee", new Date(new java.util.Date().getTime()));
        session.save(news);

        //5 提交事務
        transaction.commit();

        //6 關閉Session
        session.close();

        //7 關閉SessionFactory對象
        sessionFactory.close();
    }

}

hibernate框架是5.0.0.Final版本
mysql-jdbc驅動是5.1.37-bin版本
JUnit是4.10版本
Java8
Eclipse Version: Mars.1 Release (4.5.1)
Windows10

Configuration錯誤, 但不了解, 麻煩解答

最佳回答:


少了jta.jar包列 Hibernate會調用jta.jar的內容 加進去就OK了

Ro_bot
zjj7188
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved