使用Hibernate Envers玩Framework 2.2.2

时间:2014-04-24 13:02:54

标签: java hibernate playframework playframework-2.2 hibernate-envers

我正在尝试在Play Framework 2.2.2中实现Envers。我阅读了很多方法,但它不起作用。当我对使用@Audited注释的实体进行一些更改(保存或更新)时,数据库架构中没有任何更改。

我的更改:

build.sbt

name := "labor"

version := "1.0-SNAPSHOT"

libraryDependencies ++= Seq(
  javaJdbc,
  javaEbean,
  javaJpa, // <-- added
  "mysql" % "mysql-connector-java" % "5.1.30",
  "org.hibernate" % "hibernate-envers" % "4.3.5.Final",
  "org.hibernate" % "hibernate-entitymanager" % "4.3.5.Final", // <-- added
  cache
)     

play.Project.playJavaSettings

application.conf

db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://localhost/****?characterEncoding=UTF-8"
db.default.user=****
db.default.pass=****
db.default.jndiName=DefaultDS

的persistence.xml

<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="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <non-jta-data-source>DefaultDS</non-jta-data-source>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
        </properties>
    </persistence-unit>

</persistence>

Supplier.java

package models;

import javax.persistence.*;
import org.hibernate.envers.Audited;
import play.api.libs.iteratee.Cont;
import play.data.validation.Constraints;
import play.db.ebean.Model;

@Entity
@Audited
public class Supplier extends Model{

    @Id
    public Long id;

    @Constraints.Required
    @Audited
    public String companyName;

    @OneToOne(cascade = CascadeType.ALL)
    public Contact contact;

    @OneToOne(cascade = CascadeType.ALL)
    public Address address;

    public Supplier(String companyName){
        this.companyName = companyName;
    }

    // -- Queries

    public static Model.Finder<Long,Supplier> find = new Model.Finder<Long,Supplier>(Long.class, Supplier.class);
}

Suppliers.java

package controllers;

import models.Country;
import models.Supplier;
import play.mvc.*;
import play.data.*;
import static play.data.Form.*;
import views.html.suppliers.*;

public class Suppliers extends Controller {

    public static Result index() {
        return ok(index.render(Supplier.find.all()));
    }

    @Transactional
    public static Result addSupplier() {
        Form<Supplier> formSupplier = form(Supplier.class).bindFromRequest();
        System.out.print(formSupplier.data());
        if(formSupplier.hasErrors()){
            return badRequest(add.render(formSupplier));
        }
        Supplier supplier = formSupplier.get();
        supplier.address.save();
        supplier.contact.save();
        supplier.save();
        return redirect(routes.Suppliers.index());
    }

    @Transactional
    public static Result editSupplier(Long supplierId) {...}
}

如果有人能帮助我,我将非常感激。

祝你好运, couda

//编辑(1):

  1. 我从application.conf
  2. 中删除了侦听器
  3. 将javaJpa和hibernate-entitymanager添加到build.sbt
  4. 将“db.default.jndiName = DefaultDS”添加到application.conf
  5. 添加了JavaJPA
  6. 中描述的persistence.xml
  7. 将@Transactional添加到控制器供应商的方法(addSupplier / changeSupplier)。
  8. 但它仍然无效。首先,我收到了关于@Audited的错误,该错误在被引用的类(地址/联系人)中丢失了。在确定应用程序正常工作之后,除了在编辑前端中的实体后没有添加更多表。

    该配置有什么问题?

0 个答案:

没有答案