Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 仅当任何eniy数据已更改时,才将set updatedby设置为_Java_Hibernate_Jpa - Fatal编程技术网

Java 仅当任何eniy数据已更改时,才将set updatedby设置为

Java 仅当任何eniy数据已更改时,才将set updatedby设置为,java,hibernate,jpa,Java,Hibernate,Jpa,在我的客户实体中,我想保存一个changedByUserId字段。我只想在customer的其他字段之一确实发生更改时设置此字段。如果我一直设置它,我会强制EntityManager更新customer表,因为我设置了changedByUserId字段 @Entity @Table.... public class Customer { @Id private Long cusID; @Column private String cusNAME; @Col

在我的客户实体中,我想保存一个changedByUserId字段。我只想在customer的其他字段之一确实发生更改时设置此字段。如果我一直设置它,我会强制EntityManager更新customer表,因为我设置了changedByUserId字段

@Entity
@Table....
public class Customer {
    @Id
    private Long cusID;
    @Column
    private String cusNAME;
    @Column
    private Date changed;
    @Column
    private Long changedByUserId;

    @PreUpdate
    private preUpdate() {
        changed = new Date();
        // cannot set changedbyUserId here because no entitymanager 
        // available where i can query the id
    }
}

有人知道如何告诉EntityManager不要检查changedByUserID字段中的更改吗?

您可以创建一个拦截器类,该类扩展org.hibernate.EmptyInterceptor并重写其方法以实现您想要的功能。你可以从网站上了解更多。例如:

public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, 
            Object[] previousState, String[] propertyNames, Type[] types) throws CallbackException {
        if ( entity instanceof YourBaseEntityClass) {
            int updatedPropertyCount = 0;
            for ( int i=0; i < propertyNames.length; i++ ) {                
                if ("UPDATED_ON_PROPERTY".equals( propertyNames[i] ) ) {
                    currentState[i] = new Date();
                    updatedPropertyCount++;
                }
                if ("UPDATED_BY_PROPERTY".equals( propertyNames[i] ) ) {
                    currentState[i] = Context.getContextUserId();
                    updatedPropertyCount++;
                }
                //if the conditioon passes both the properties have been updated
                //break from the loop 
                if(updatedPropertyCount == UPDATE_TOTAL_COUNT){
                    return true;
                }
            }
            //even if either of them modified returned true after the end of the loop
            if(updatedPropertyCount >0){
                return true;
            }           
        }
        return false;
    }
public boolean onFlushDirty(对象实体,可序列化id,对象[]当前状态,
对象[]previousState、字符串[]propertyNames、类型[]类型)引发回调异常{
if(BaseEntityClass的实体实例){
int updatedPropertyCount=0;
对于(int i=0;i0){
返回true;
}           
}
返回false;
}

谢谢,我使用的是javax.persistence api,而不是hibernate。还有其他选择吗?