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;
    }



}

主类.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

持久性.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