我在Play Framework中使用Hibernate编写了一个Application。我想用Envers库添加版本。
我有一个用户类。
@Entity
@Audited(withModifiedFlag=true)
public class User
控制器,用于更改用户的某些内容。
@Transactional
public static Result updateAccountStates(Long userId) {
User user = JPA.em().find(User.class, userId);
Integer prevStatus = user.status;
Form<AccountStateForm> form = Form.form(AccountStateForm.class).bindFromRequest();
form.get().applyTo(user);
return redirect(routes.Customers.account(userId));
}
我想向元数据添加有关谁更改了用户数据的信息。
@Entity
@RevisionEntity(UserRevisionListener.class)
public class UserRevisionEntity extends DefaultRevisionEntity {
@ManyToOne
public Staff staff;
}
public class UserRevisionListener implements RevisionListener {
@Override
public void newRevision(Object revisionEntity) {
UserRevisionEntity userEntity = (UserRevisionEntity) revisionEntity;
userEntity = SecurityUtils.getStaff(ctx());
}
}
每次用户更改我都会记录改变它的女巫会员。
我已将UserRevisionEntity模型添加到persistence.xml:
<class>models.UserRevisionEntity</class>
当我更改用户状态时,我得到:
[error] o.h.e.j.s.SqlExceptionHelper - Cannot add or update a child row: a foreign key constraint fails (`creditgate24`.`user_aud`, CONSTRAINT `FK_97pph94d4cb7qah5aygmmll2y` FOREIGN KEY (`REV`) REFERENCES `REVINFO` (`REV`))
[error] play - Cannot invoke the action, eventually got an error: java.lang.RuntimeException: javax.persistence.RollbackException: Error while committing the transaction
[error] application -
! @6mjac6klf - Internal server error, for (POST) [/customers/9/account?csrfToken=fe225b185b9c5ee9f48d58180d0885ca7118d7fd-1435656024963-c42071a398c0e4c84c7682d0] ->
play.api.Application$$anon$1: Execution exception[[RuntimeException: javax.persistence.RollbackException: Error while committing the transaction]]
at play.api.Application$class.handleError(Application.scala:296) ~[play_2.11-2.3.7.jar:2.3.7]
at play.api.DefaultApplication.handleError(Application.scala:402) [play_2.11-2.3.7.jar:2.3.7]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.7.jar:2.3.7]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.7.jar:2.3.7]
at scala.Option.map(Option.scala:145) [scala-library-2.11.4.jar:na]
Caused by: java.lang.RuntimeException: javax.persistence.RollbackException: Error while committing the transaction
at play.db.jpa.JPA.withTransaction(JPA.java:111) ~[play-java-jpa_2.11-2.3.7.jar:2.3.7]
at controllers.admin.Customers.updateAccountStates(Customers.java:86) ~[classes/:na]
at admin.Routes$$anonfun$routes$1$$anonfun$applyOrElse$21$$anonfun$apply$21.apply(routes_routing.scala:952) ~[classes/:na]
at admin.Routes$$anonfun$routes$1$$anonfun$applyOrElse$21$$anonfun$apply$21.apply(routes_routing.scala:952) ~[classes/:na]
at play.core.Router$HandlerInvokerFactory$$anon$4.resultCall(Router.scala:264) ~[play_2.11-2.3.7.jar:2.3.7]
Caused by: javax.persistence.RollbackException: Error while committing the transaction
at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:94) ~[hibernate-entitymanager-4.3.10.Final.jar:4.3.10.Final]
at play.db.jpa.JPA.withTransaction(JPA.java:141) ~[play-java-jpa_2.11-2.3.7.jar:2.3.7]
at play.db.jpa.JPA.withTransaction(JPA.java:104) ~[play-java-jpa_2.11-2.3.7.jar:2.3.7]
at controllers.admin.Customers.updateAccountStates(Customers.java:86) ~[classes/:na]
at admin.Routes$$anonfun$routes$1$$anonfun$applyOrElse$21$$anonfun$apply$21.apply(routes_routing.scala:952) ~[classes/:na]
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1763) ~[hibernate-entitymanager-4.3.10.Final.jar:4.3.10.Final]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677) ~[hibernate-entitymanager-4.3.10.Final.jar:4.3.10.Final]
at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:82) ~[hibernate-entitymanager-4.3.10.Final.jar:4.3.10.Final]
at play.db.jpa.JPA.withTransaction(JPA.java:141) ~[play-java-jpa_2.11-2.3.7.jar:2.3.7]
at play.db.jpa.JPA.withTransaction(JPA.java:104) ~[play-java-jpa_2.11-2.3.7.jar:2.3.7]
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:72) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`cg`.`user_aud`, CONSTRAINT `FK_97pph94d4cb7qah5aygmmll2y` FOREIGN KEY (`REV`) REFERENCES `REVINFO` (`REV`))
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.7.0_71]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) ~[na:1.7.0_71]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.7.0_71]
at java.lang.reflect.Constructor.newInstance(Constructor.java:526) ~[na:1.7.0_71]
at com.mysql.jdbc.Util.handleNewInstance(Util.java:377) ~[mysql-connector-java-5.1.34.jar:5.1.34]
由于未正确创建REV条目,因此无法创建用户修订版。
我不明白为什么会这样。当'newRevision'抛出异常时会发生这种情况,不应该这样做。
感谢您的帮助!