JPA-Hibernate与Hibernate拦截器

时间:2016-04-12 08:30:46

标签: hibernate jpa

我尝试在没有Spring的情况下使用此示例link执行AuditLog。我用了JPA。我试图注释EntityManager,但它没有工作。我试图调试re.sub中的em为空。你能帮助我参加那次会议吗?我看到很多人使用hibernate会话,但如果我想使用MyUtil我该怎么办? 我的示例代码:

EntityManager

1 个答案:

答案 0 :(得分:1)

如果您没有容器管理实体经理(您提及"没有Spring")那么它非常简单。

请注意,我认为您必须为审计日志拦截器使用不同的持久性单元来防止无限循环(在我的示例中为PU_2)。

所以你需要META-INF中的persistence.xml(用于保存数据库连接设置和JPA / Hibernate属性):

<persistence 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" version="2.0">
    <persistence-unit name="PU_1" transaction-type="RESOURCE_LOCAL">
        <description>Can be anything</description>
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>pack.entities.Test</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyTenSevenDialect" />
            <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/db01;create=true" />
            <property name="javax.persistence.jdbc.user" value="APP" />
            <property name="javax.persistence.jdbc.password" value="anything" />
            <property name="hibernate.hbm2ddl.auto" value="update" />

            <property name="hibernate.ejb.interceptor" value="pack.AuditInterceptor" />
        </properties>
    </persistence-unit>

    <persistence-unit name="PU_2" transaction-type="RESOURCE_LOCAL">
        <description>For DB audit log</description>
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>pack.entities.AuditLog</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyTenSevenDialect" />
            <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/db01;create=true" />
            <property name="javax.persistence.jdbc.user" value="APP" />
            <property name="javax.persistence.jdbc.password" value="anything" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>

</persistence>

你应该在主程序中以编程方式创建你的EntityManagerFactory和EntityManager:

package pack;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import pack.entities.Test;

public class Start {

    public static void main(String[] args) {

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("PU_1");
        EntityManager em = emf.createEntityManager();

        em.getTransaction().begin();
        Test t = new Test();
        t.setName("something for name");
        em.persist(t);
        em.getTransaction().commit();

        em.getTransaction().begin();
        t = new Test();
        t.setName("something for name 2");
        em.persist(t);
        em.getTransaction().commit();

        em.close();
        emf.close();
    }
}

在你的审计日志hibernate拦截器中就像这样:

package pack;

import java.io.Serializable;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.hibernate.EmptyInterceptor;
import org.hibernate.Transaction;
import org.hibernate.type.Type;

import pack.entities.AuditLog;

public class AuditInterceptor extends EmptyInterceptor {

    private EntityManager em;

    public AuditInterceptor() {
        System.out.println("AuditInterceptor constructed");
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("PU_2");
        em = emf.createEntityManager();
    }

    @Override
    public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
        System.out.println("onSave called");
        em.getTransaction().begin();
        AuditLog a = new AuditLog();
        a.setEntry("object with ID: " + id + " saved");
        em.persist(a);
        em.getTransaction().commit();
        return false;
    }

    @Override
    public void afterTransactionCompletion(Transaction tx) {
        System.out.println("afterTransactionCompletion called");
        em.getTransaction().begin();
        AuditLog a = new AuditLog();
        a.setEntry("transaction: " + tx + " completed");
        em.persist(a);
        em.getTransaction().commit();
    }

}

实体类:

package pack.entities;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Test {

    @Id
    @GeneratedValue
    private Long id;

    private String name;

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

package pack.entities;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class AuditLog {

    @Id
    @GeneratedValue
    private Long id;

    private String entry;

    public Long getId() {
        return id;
    }

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

    public String getEntry() {
        return entry;
    }

    public void setEntry(String entry) {
        this.entry = entry;
    }

}