Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.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 冬眠环境不';不要使用SpringMVC配置在审计表中写入任何内容_Java_Spring_Hibernate_Spring Mvc_Hibernate Envers - Fatal编程技术网

Java 冬眠环境不';不要使用SpringMVC配置在审计表中写入任何内容

Java 冬眠环境不';不要使用SpringMVC配置在审计表中写入任何内容,java,spring,hibernate,spring-mvc,hibernate-envers,Java,Spring,Hibernate,Spring Mvc,Hibernate Envers,当我在数据库中保存、更新或删除一个条目时,我试图使用Enverse来审核这些表 Envers配置如下所示: pom.xml <!-- Spring --> <org.springframework-version>4.1.6.RELEASE</org.springframework-version> <!-- Hibernate --> <hibernate.version&

当我在数据库中保存、更新或删除一个条目时,我试图使用Enverse来审核这些表

Envers配置如下所示:

pom.xml

        <!-- Spring -->
        <org.springframework-version>4.1.6.RELEASE</org.springframework-version>

        <!-- Hibernate  -->
        <hibernate.version>4.3.5.Final</hibernate.version>
[...]

    <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-envers</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
    <property name="packagesToScan" value="my.domain"/>

    <property name="persistenceUnitName" value="persistenceUnit"/>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop> 
            <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
            <prop key="hibernate.connection.charSet">UTF-8</prop>

            <prop key="hibernate.max_fetch_depth">3</prop>
            <prop key="hibernate.jdbc.fetch_size">50</prop>
            <prop key="hibernate.jdbc.batch_size">20</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.format_sql">true</prop>

            <prop key="org.hibernate.envers.audit_table_suffix">_H</prop>
            <prop key="org.hibernate.envers.revision_field_name">AUDIT_REVISION</prop>
            <prop key="org.hibernate.envers.revision_type_field_name">ACTION_TYPE</prop>
            <prop key="org.hibernate.envers.audit_strategy">org.hibernate.envers.strategy.ValidityAuditStrategy</prop>
            <prop key="org.hibernate.envers.audit_strategy_validity_end_rev_field_name">AUDIT_REVISION_END</prop>
            <prop key="org.hibernate.envers.audit_strategy_validity_store_revend_timestamp">True</prop>
            <prop key="org.hibernate.envers.audit_strategy_validity_revend_timestamp_field_name">AUDIT_REVISION_END_TS</prop>               
            <prop key="jadira.usertype.databaseZone">jvm</prop>
        </props>
    </property>
</bean>

唯一被修改的字段是版本,其他字段被忽略。

据我所知,您实际上根本不需要
envers
。Envers更多的是存储数据的
修订版
(当然可以用于审计目的),但是如果您只想让这些
字段由
创建,
createdDate
lastModifiedBy
lastModifiedDate
字段创建,有一种更简单的方法

由于您已经在使用
spring数据jpa
,我建议您使用
@EnableJpaAuditing
注释启用
jpa审核

然后您可以从您的
抽象域
类中删除
@Audited
,并添加
@EntityListener(AuditingEntityListener.class)
,这将在每次实体被持久化时强制hibernate保存审核信息

最后,但并非最不重要的事情是定义
AuditorAware
bean。它会告诉每个时刻到底是谁在操纵给定的实体,这样审核侦听器就会知道应该将哪些数据设置为
createdBy
lastModifiedBy
字段。 这很简单,下面是一个例子:

@Bean
AuditorAware auditor() {
    // return () -> "system";  // Fixed principal
    // and for spring-security
    return () -> {
        Authentication authentication = SecurityContextHolder.getContext()
                .getAuthentication();
        if (authentication == null || !authentication.isAuthenticated()) {
            return null;
        }
        return authentication.getPrincipal().toString();
    }
}
就这样

以下是一个完整的示例:

@SpringBootApplication
@EnableJpaAuditing
public class So45347635Application {
    public static void main(String[] args) { SpringApplication.run(So45347635Application.class, args); }

    @MappedSuperclass
    @EntityListeners(AuditingEntityListener.class)
    public static abstract class AbstractDomain extends AbstractPersistable<Long> implements Auditable<String, Long> {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
        @Version
        private int version;

        @Column(updatable = false)
        private String createdBy;

        @Temporal(TemporalType.TIMESTAMP)
        @Column(updatable = false)
        private Date createdDate;

        private String lastModifiedBy;

        @Temporal(TemporalType.TIMESTAMP)
        @Column(insertable = false)
        private Date lastModifiedDate;

        @Override
        public String getCreatedBy() {
            return this.createdBy;
        }

        @Override
        public void setCreatedBy(String createdBy) {
            this.createdBy = createdBy;
        }

        @Override
        public DateTime getCreatedDate() {
            return null == this.createdDate ? null : new DateTime(this.createdDate);
        }

        @Override
        public void setCreatedDate(DateTime createdDate) {
            this.createdDate = createdDate.toDate();
        }

        @Override
        public String getLastModifiedBy() {
            return this.lastModifiedBy;
        }

        @Override
        public void setLastModifiedBy(String lastModifiedBy) {
            this.lastModifiedBy = lastModifiedBy;
        }

        @Override
        public DateTime getLastModifiedDate() {
            return null == this.lastModifiedDate ? null : new DateTime(this.lastModifiedDate);
        }

        @Override
        public void setLastModifiedDate(DateTime lastModifiedDate) {
            this.lastModifiedDate = lastModifiedDate.toDate();
        }
    }

    @Entity
    @Table(name = "users")
    public static class User extends AbstractDomain {
        private String username = "anonymous";

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        @Override
        public String toString() {
            return String.format("User{id=%d, createdBy='%s', createdDate=%s, lastModifiedBy='%s', lastModifiedDate=%s, username='%s'}",
                    getId(), getCreatedBy(), getCreatedDate(), getLastModifiedBy(), getLastModifiedDate(), username);
        }
    }

    @Bean
    AuditorAware auditor() { return () -> "system"; }

    @Bean
    CommandLineRunner start(UserRepository userRepository, TransactionTemplate tx) {
        return args -> tx.execute(ctx -> {
            final User user = userRepository.save(new User());
            user.setUsername("admin");
            System.out.println(">>>> " + userRepository.save(user));
            return null;
        });
    }
}

interface UserRepository extends CrudRepository<So45347635Application.User, Long> {
}

>但是在DB中,这个字段永远是空的。您指的是哪些字段?是否已创建审核表?您是否在日志中看到任何错误,表明审核操作未发生的原因?也许可以启用
hibernate.show_sql
并将
org.hibernate.sql
设置为
trace
,以便您可以查看日志中正在执行的sql语句。您是否看到与审计表相关的内容?根据你所展示的信息,如果没有更多的细节,很难提供更多的帮助。@Naros我看不出有什么奇怪的。
@SpringBootApplication
@EnableJpaAuditing
public class So45347635Application {
    public static void main(String[] args) { SpringApplication.run(So45347635Application.class, args); }

    @MappedSuperclass
    @EntityListeners(AuditingEntityListener.class)
    public static abstract class AbstractDomain extends AbstractPersistable<Long> implements Auditable<String, Long> {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
        @Version
        private int version;

        @Column(updatable = false)
        private String createdBy;

        @Temporal(TemporalType.TIMESTAMP)
        @Column(updatable = false)
        private Date createdDate;

        private String lastModifiedBy;

        @Temporal(TemporalType.TIMESTAMP)
        @Column(insertable = false)
        private Date lastModifiedDate;

        @Override
        public String getCreatedBy() {
            return this.createdBy;
        }

        @Override
        public void setCreatedBy(String createdBy) {
            this.createdBy = createdBy;
        }

        @Override
        public DateTime getCreatedDate() {
            return null == this.createdDate ? null : new DateTime(this.createdDate);
        }

        @Override
        public void setCreatedDate(DateTime createdDate) {
            this.createdDate = createdDate.toDate();
        }

        @Override
        public String getLastModifiedBy() {
            return this.lastModifiedBy;
        }

        @Override
        public void setLastModifiedBy(String lastModifiedBy) {
            this.lastModifiedBy = lastModifiedBy;
        }

        @Override
        public DateTime getLastModifiedDate() {
            return null == this.lastModifiedDate ? null : new DateTime(this.lastModifiedDate);
        }

        @Override
        public void setLastModifiedDate(DateTime lastModifiedDate) {
            this.lastModifiedDate = lastModifiedDate.toDate();
        }
    }

    @Entity
    @Table(name = "users")
    public static class User extends AbstractDomain {
        private String username = "anonymous";

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        @Override
        public String toString() {
            return String.format("User{id=%d, createdBy='%s', createdDate=%s, lastModifiedBy='%s', lastModifiedDate=%s, username='%s'}",
                    getId(), getCreatedBy(), getCreatedDate(), getLastModifiedBy(), getLastModifiedDate(), username);
        }
    }

    @Bean
    AuditorAware auditor() { return () -> "system"; }

    @Bean
    CommandLineRunner start(UserRepository userRepository, TransactionTemplate tx) {
        return args -> tx.execute(ctx -> {
            final User user = userRepository.save(new User());
            user.setUsername("admin");
            System.out.println(">>>> " + userRepository.save(user));
            return null;
        });
    }
}

interface UserRepository extends CrudRepository<So45347635Application.User, Long> {
}
create table users (id bigint generated by default as identity, created_by varchar(255), created_date timestamp, last_modified_by varchar(255), last_modified_date timestamp, version integer not null, username varchar(255), primary key (id));
insert into users (id, created_by, created_date, last_modified_by, version, username) values (null, 'system', '28-Jul-17', 'system', 0, 'anonymous');
update users set last_modified_by='system', last_modified_date='28-Jul-17', version=1, username='admin' where id=1 and version=0;