我想使用Envers来审计与嵌入式组件的多对多关系,但是我遇到了一个MappingException,说不支持ComponentType。这是堆栈跟踪的相关部分:
Caused by: org.hibernate.MappingException: Type not supported: org.hibernate.type.ComponentType
at org.hibernate.envers.configuration.metadata.IdMetadataGenerator.addIdProperties(IdMetadataGenerator.java:74)
at org.hibernate.envers.configuration.metadata.IdMetadataGenerator.addId(IdMetadataGenerator.java:105)
at org.hibernate.envers.configuration.metadata.AuditMetadataGenerator.generateFirstPass(AuditMetadataGenerator.java:413)
at org.hibernate.envers.configuration.EntitiesConfigurator.configure(EntitiesConfigurator.java:101)
at org.hibernate.envers.configuration.AuditConfiguration.<init>(AuditConfiguration.java:103)
at org.hibernate.envers.configuration.AuditConfiguration.getFor(AuditConfiguration.java:135)
at org.hibernate.envers.event.EnversIntegrator.integrate(EnversIntegrator.java:63)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:295)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737)
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:76)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:905)
阅读Envers参考,似乎Envers可以处理 我正在努力做什么。根据参考文献:
如果您要覆盖从@Mappedsuperclass或嵌入式组件继承的某些字段/属性的审核行为,则可以在组件的子类型或用法站点上应用@AuditOverride(s)批注。
这是我的关联实体。您可以在类级别查看我尝试使用@AuditOverride的位置,以防止审核嵌入式组件。我也尝试在字段本身上使用注释。两者都没有区别。
@Audited
// @AuditOverride(name = "pk", isAudited = false) <===== Didn't help
@Table(name = "user_role")
@javax.persistence.Entity
@AssociationOverrides
(
{
@AssociationOverride
(name = "pk.user", joinColumns = @JoinColumn(name = "id")),
@AssociationOverride
(name = "pk.role", joinColumns = @JoinColumn(name = "id"))
}
)
public class UserRole extends Entity<UserRole>
{
private static final long serialVersionUID = 1L;
private Date expirationDate;
private UserRolePk pk = new UserRolePk();
public UserRole() {}
// @AuditOverride(name = "pk", isAudited = false) <== Didn't help
@EmbeddedId
public UserRolePk getPk() { return pk; }
@Transient
public User getUser() { return getPk().getUser(); }
@Transient
public Role getRole() { return getPk().getRole(); }
...
}
这是用户实体:
@Audited
@Table(name = "applicationuser")
@javax.persistence.Entity
public class User extends Entity<User>
{
private static final long serialVersionUID = 1L;
private String firstName;
private String lastName;
private String email;
private Set<UserRole> userRoles = new HashSet<UserRole>(0);
@OneToMany(fetch = FetchType.LAZY, cascade=CascadeType.ALL,
mappedBy = "pk.user", orphanRemoval = true)
public Set<UserRole> getUserRoles() { return userRoles; }
...
}
这是角色实体:
@Audited
@Table(name = "role")
@javax.persistence.Entity
public class Role extends Entity<Role>
{
private static final long serialVersionUID = 1L;
private String name;
private String label;
private Set<UserRole> userRoles = new HashSet<UserRole>(0);
@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.role",
cascade=CascadeType.ALL, orphanRemoval = true)
public Set<UserRole> getUserRoles() { return userRoles; }
...
}
这是嵌入式组件:
@Embeddable
public class UserRolePk implements Serializable
{
private static final long serialVersionUID = 1L;
private User user;
private Role role;
@ManyToOne
public User getUser() { return user; }
@ManyToOne
public Role getRole() { return role; }
...
}
最后,这是我的基本实体,为了完整性:
@MappedSuperclass()
public abstract class Entity<X extends Entity<X>>
implements Comparable<X>, Serializable
{
private static final long serialVersionUID = 1L;
private Long id;
private Timestamp timestamp;
...
}
我已经阅读了Envers参考资料并仔细阅读了论坛,但信息 看起来很稀疏。关于这个的任何想法或指示?
答案 0 :(得分:1)
我通过丢弃可嵌入组件UserRolePk解决了这个问题,并且只使用了@JoinColumn,例如。
public class UserRole extends Entity<UserRole>
{
private User user;
private Role role;
public UserRole() {}
@ManyToOne(fetch=FetchType.EAGER, optional=false)
@JoinColumn(name="userid", referencedColumnName = "id", insertable=false, updatable=false)
// XmlTransient used to prevent the following exception when JAXB marshals this into XML:
// com.sun.istack.SAXException2: A cycle is detected in the object graph. This will cause
// infinitely deep XML:
@XmlTransient
public User getUser() { return this.user; }
@ManyToOne(fetch=FetchType.EAGER, optional=false)
@JoinColumn(name="roleid", referencedColumnName = "id", insertable=false, updatable=false)
public Role getRole() { return this.role; }
...
}
答案 1 :(得分:1)
我正在使用Envers 4.1.3.Final,它肯定还有这个错误:对于与@Embedded组件的关系,它在数据库中正确记录审计记录,但查询API({ {1}},在我的情况下)根本看不到它。它将返回已更改对象的修订版本(MOD),但是当您查看包含对嵌入对象的引用的实际实体属性时,对于所有修订版本它始终为null。
我最后编写了一个解决方法,用本机SQL(对于受影响的属性)查询更改的值,因为我不想改变我的映射只是为了安抚一个错误的插件。