1

JPA と Hibernate を使用してオブジェクトをデータベースに保存していますが、例外が発生しています:

スレッド「メイン」の例外 java.lang.IllegalStateException: com.Main.main(Main.java:46) の org.hibernate.ejb.TransactionImpl.rollback(TransactionImpl.java:82) でトランザクションがアクティブではありません

これらは私のcomパッケージのクラスです

ユーザー.java

package com;

import javax.persistence.*;

/**
 * this class show entity corresponding to 
 * 
 * user table in database
 * @author hemendra
 *
 */
@Entity
@Table(name="user")
public class User {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    @Column(name="name")
    private String name;

    @Column(name="password")
    private String password;


    public void User() {
        System.out.println("sjdflsjl");
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof User) {
            User user = (User) obj;
            return user.getId() == this.getId();
        }

        return false;
    }


    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 getPassword() {
        return password;
    }


    public void setPassword(String password) {
        this.password = password;
    }



}

MainClass.java

/*  this package contains classes
 *   for database connectivity
 *   and operation
 */ 

package com;

import javax.persistence.*;




/**
 * this class is for testing the 
 * 
 * connectivity to database
 * @author Hemendra
 *
 */
public class MainClass {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        /**
         * Get EntityManagerFactory from 
         * 
         * @link persistence.xml file
         */
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("Hello");

        //creation of entity manager for transaction
        EntityManager manager =  factory.createEntityManager();

        try {
            manager.getTransaction().begin();
            User user = new User();
            user.setName("anc");
            user.setPassword("sdfs");
            manager.persist(user);
            manager.getTransaction().commit();

        } catch (Exception e ) {
            manager.getTransaction().rollback();
            e.printStackTrace();
        } finally {
            factory.close();            
        }
         System.out.println("It is over");
    }

}

and i have persistence.xml file in src/META-INF folder

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="2.0"
    xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <persistence-unit name="Hello" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <properties>
            <property name="hibernate.show_sql" value="true" />
            <property name="javax.persistence.jdbc.driver"
                value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url"
                value="jdbc:mysql://localhost:3307/Test" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password"
                value="root" />
            <property name="hibernate.dialect"
                value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>
</persistence>

このタイプの例外が発生する理由がわかりません。前もって感謝します

4

0 に答える 0