Java 如何让Hibernate拦截器获取某些超类字段?

Java 如何让Hibernate拦截器获取某些超类字段?,java,hibernate,Java,Hibernate,我有一些实体。比如说 @Entity @IdClass(SecurityUserPublisherId.class) @Table(name="SECUSERPUB") public class SecurityUserPublisher extends Auditable { private static final long serialVersionUID = 1L; @Id @Column(name="USERPUB_CONTACTID", updatable=f

我有一些实体。比如说

@Entity
@IdClass(SecurityUserPublisherId.class)
@Table(name="SECUSERPUB")
public class SecurityUserPublisher extends Auditable {
    private static final long serialVersionUID = 1L;
    @Id
    @Column(name="USERPUB_CONTACTID", updatable=false, nullable=false)
    protected Integer contactId = null;
    @Id
    @Column(name="USERPUB_PUBID", updatable=false, nullable=false)
    protected Integer publisherId = null;

    public SecurityUserPublisher() {
        super();
        setAudited(true);
    }

    public SecurityUserPublisher(SecurityUserPublisher securityUserPublisher) {
        setContactId(securityUserPublisher.getContactId());
        setPublisherId(securityUserPublisher.getPublisherId());
    }

    public Integer getContactId() {return contactId;}

    public void setContactId(Integer contactId) {this.contactId = contactId;}

    public Integer getPublisherId() {return publisherId;}

    public void setPublisherId(Integer publisherId) {this.publisherId = publisherId;}

    @Transient
    @Override
    public String getPrimaryKeyDisplay() {
        StringBuilder sb = new StringBuilder();
        if (contactId == null) {
            sb.append(" contactId: null");
        } else {
            sb.append(" contactId: " + contactId.toString());
        }
        if (publisherId == null) {
            sb.append(" publisherId: null");
        } else {
            sb.append(" publisherId: " + publisherId.toString());
        }
        return sb.toString();
    }

    @Transient
    @Override
    public String getAuditDetail() {
        return new ToStringBuilder(this).append("contactId", contactId).append("publisherId", publisherId).toString();
    }
}
它们扩展了可审核类

public abstract class Auditable implements Serializable {
    private static final long serialVersionUID = 1L;
    protected Integer auditContactId = null;
    protected Boolean audited = false;

    public void setAuditContactId(Integer auditContactId) {this.auditContactId = auditContactId;}
    public Integer getAuditContactId() {return auditContactId;}
    public Boolean isAudited() {return audited;}
    protected void setAudited(Boolean audited) {this.audited = audited;}
    public abstract String getPrimaryKeyDisplay();
    public abstract String getAuditDetail();
}
我有一个拦截器

public class AuditInterceptor extends EmptyInterceptor {
    private static final long serialVersionUID = 1L;

    private Set<Auditable> inserts = new HashSet<>();
    private Set<Auditable> updates = new HashSet<>();
    private Set<Auditable> deletes = new HashSet<>();

    @Override
    public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types)
        throws CallbackException {

        if (entity != null && entity instanceof Auditable && ((Auditable)entity).isAudited()){
            System.out.println(entity.toString());
            System.out.println(id.toString());
            System.out.println(Arrays.toString(state));
            System.out.println(Arrays.toString(propertyNames));
            System.out.println(Arrays.toString(types));
            inserts.add((Auditable)entity);
        }
        return super.onSave(entity, id, state, propertyNames, types);

    }

    @Override
    public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types)
        throws CallbackException {

        if (entity instanceof Auditable && ((Auditable)entity).isAudited()){
            updates.add((Auditable)entity);
        }
        return super.onFlushDirty(entity, id, currentState, previousState, propertyNames, types);

    }

    @Override
    public void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {

        if (entity instanceof Auditable && ((Auditable)entity).isAudited()){
            System.out.println(entity.toString());
            System.out.println(id.toString());
            System.out.println(Arrays.toString(state));
            System.out.println(Arrays.toString(propertyNames));
            System.out.println(Arrays.toString(types));
            deletes.add((Auditable)entity);
        }
    }

    //called before commit into database
    @Override
    public void preFlush(@SuppressWarnings("rawtypes") Iterator iterator) {
    }

    //called after committed into database
    @Override
    public void postFlush(@SuppressWarnings("rawtypes") Iterator iterator) {
        try {
            for (Iterator<Auditable> iter = inserts.iterator(); iter.hasNext();) {
                Auditable entity = iter.next();
                iter.remove();
                logIt("Saved", entity);
            }

            for (Iterator<Auditable> iter = updates.iterator(); iter.hasNext();) {
                Auditable entity = iter.next();
                iter.remove();
                logIt("Updated", entity);
            }

            for (Iterator<Auditable> iter = deletes.iterator(); iter.hasNext();) {
                Auditable entity = iter.next();
                iter.remove();
                logIt("Deleted", entity);
            }

        } finally {
            inserts.clear();
            updates.clear();
            deletes.clear();
        }
    }

    public void logIt(String action, Auditable entity) {
        if (!entity.isAudited()) {
            return;
        }
        Session tempSession = SessionHelper.getSession().getSession().getSessionFactory().openSession();
        Audit auditRecord = new Audit();
        auditRecord.setAction(action);
        auditRecord.setContactId(entity.getAuditContactId());
        auditRecord.setDetail(entity.getAuditDetail()); 
        auditRecord.setCreatedTimestamp(new Timestamp(System.currentTimeMillis())); 
        auditRecord.setEntityPK(entity.getPrimaryKeyDisplay()); 
        auditRecord.setEntityName(entity.getClass().toString());
        tempSession.save(auditRecord);
        tempSession.flush();
    }

}
审计线索看起来不错 审计识别号120385 已保存审核\u操作
审计详情SecurityUserPublisher@678541a[contactId=8721,publisherId=360]审计创建数据传输2019-04-04 13:52:57 审计实体联系人ID:8721:360 AUDIT\u ENTITYNAME SecurityUserPublisher 审计联系人ID 7341

但是当我试图删除

final SecurityUserPublisher secUserPub = SessionHelper.getSession().getSecurityUserPublisher(currentUser.getContactId(), publisher.getId());
secUserPub.setAuditContactId(parentFrame.getUser().getContactId());
try {
    SessionHelper.getRemove().removeSecurityUserPublisher(secUserPub);
审计跟踪联系人id为空,即使我在调用remove之前明确设置了它。 审计识别号120386 已删除审核操作
审计详情SecurityUserPublisher@1d184bc8[contactId=8721,publisherId=360] 审核时间:2019-04-04 13:53:35 审计实体联系人ID:8721:360 AUDIT\u ENTITYNAME SecurityUserPublisher
审核联系人ID为空


那么,如何让onDelete在Audit表中显示contactId呢?

在Auditable类上使用注释。否则Hibernate会忽略超类字段,并且可审核类的字段不会保存到数据库中。

如果我这样做,则数据库会抛出一个
,原因是:java.sql.SQLException:无效列名
,因为可审核字段没有注释,并且它们与表中的任何列名都不匹配。这些字段存在于另一个审核表中。如果我用@Transient对它们进行注释,我不会再得到异常,但它们也不会进入拦截器。要么字段应该在数据库中(在这种情况下,您应该对它们进行注释),要么它们是暂时的,并且不会持久化,在这种情况下,hibernate不会将它们视为数据库字段。您所描述的是,有时希望它们是持久字段,有时希望它们被忽略。但在我看来,您并没有提供hibernate进行区分所需的信息。我不确定你能不能。“修复”类层次结构以反映您的需要,这不是更好的解决方案吗?@NathanKummer我想做的就是让Hibernate拦截器记录对审核表进行特定更改的人。但是它没有访问用户名的权限,因为除非字段被持久化,否则用户名不会传输到服务器。我最终使用了它,尽管它让我们在每个要审核的表上都放置了一个字段,但这对该表来说真的没有任何意义(除了最后一个修改它的人)。
final SecurityUserPublisher secUserPub = SessionHelper.getSession().getSecurityUserPublisher(currentUser.getContactId(), publisher.getId());
secUserPub.setAuditContactId(parentFrame.getUser().getContactId());
try {
    SessionHelper.getRemove().removeSecurityUserPublisher(secUserPub);