0

嗨,我们有一台运行 Hibernate 的服务器,这些类使用 envers 进行审计。像这样 :

package db.shared;

import interfaces.GuiEditableTable;
import interfaces.ListableEntity;
import interfaces.ShortDescriptable;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.persistence.Version;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.envers.AuditTable;
import org.hibernate.envers.Audited;

import common.Identifiable;
import common.Preferences;
import common.entity.EntityConcurrency;

@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Audited
@AuditTable(schema=Preferences.SCHEMA_AUDIT, value=Lizenz.CLASS_NAME+Preferences.AUDIT_SUFFIX)
@Table(schema = Preferences.SCHEMA_SHARED, uniqueConstraints = { @UniqueConstraint(columnNames = { Lizenz.PROPERTY_LIZENZ_NAME }), @UniqueConstraint(columnNames = { Lizenz.PROPERTY_LIZENZ_NUMBER }) })
public class Lizenz extends common.entity.AbstractEntity
implements EntityConcurrency, ListableEntity, ShortDescriptable, GuiEditableTable
{

private static final long serialVersionUID = 589201271279697573L;
public static final String CLASS_NAME = "Lizenz";
public static final String PROPERTY_LIZENZ_NAME = "lizenzName";
public static final String PROPERTY_LIZENZ_NUMBER = "lizenzNummer";
public static final String MTM_PROPERTY_NAME = "lizenz";

public static final int LIZENZ_NUMBER_PADDING = 4;



@Id
@Column(name = Identifiable.PROPERTY_ID, columnDefinition = Preferences.DDL_TYPE_ID)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long ID;

@Version @Column(name = EntityConcurrency.PROPERTY_VERSION)
private long version;


private String lizenzName;
private String logo;

.
.
.
}

在 serverstart 有这样的警告:

15:14:49 WARN  [org.hibernate.dialect.lock.UpdateLockingStrategy] HHH000416: Write locks via update not supported for non-versioned entities [db.shared.Lizenz_AUD]
15:14:49 WARN  [org.hibernate.dialect.lock.UpdateLockingStrategy] HHH000416: Write locks via update not supported for non-versioned entities [db.shared.Lizenz_AUD]
15:14:49 WARN  [org.hibernate.dialect.lock.UpdateLockingStrategy] HHH000416: Write locks via update not supported for non-versioned entities [db.shared.Lizenz_AUD]
15:14:49 WARN  [org.hibernate.dialect.lock.PessimisticReadUpdateLockingStrategy] HHH000416: Write locks via update not supported for non-versioned entities [db.shared.Lizenz_AUD]
15:14:49 WARN  [org.hibernate.dialect.lock.PessimisticWriteUpdateLockingStrategy] HHH000416: Write locks via update not supported for non-versioned entities [db.shared.Lizenz_AUD]

问题是我们只为审计创建了数据库表,我们没有任何审计类。这些类将在启动时生成,我们还没有找到任何 @audit-Options 来抑制这些警告。警告对我们来说无关紧要,因为只写一次审计条目并且从不更新它,每个类有 5 个警告正在淹没我们的日志。

我们在 java 1.7 中使用 hibernate-envers-4.1.10.Final.jar 和 hibernate-core-4.1.10.Final.jar。

有没有人有同样的问题或知道如何解决它。我们感谢任何帮助。

4

1 回答 1

0

正如我们在 hibernate 的源代码中看到的:

public UpdateLockingStrategy(Lockable lockable, LockMode lockMode) {

    this.lockable = lockable;

    this.lockMode = lockMode;

    if (lockMode.lessThan(LockMode.UPGRADE)) {

        throw new HibernateException("[" + lockMode + "] not valid for update statement");

    }

    if (!lockable.isVersioned()) {

        log.warn("write locks via update not supported for non-versioned entities [" + lockable.getEntityName() + "]");

        this.sql = null;

    } else {
        this.sql = generateLockString();

    }
}

和 isVersioned() 的文档:

/**
 * Determine whether optimistic locking by column is enabled for this
 * entity.
 *
 * @return True if optimistic locking by column (i.e., <version/> or
 * <timestamp/>) is enabled; false otherwise.
 */
public boolean isVersioned();  

结果是如果你想使用审计实体并且不给出这个警告,你应该通过@Version 在你的实体列上启用乐观锁定:)

于 2013-10-15T11:06:09.773 回答