我可以在没有orm.xml文件的情况下使用Spring Data JPA Auditing(使用JavaConfig代替)吗? [英] Can I use Spring Data JPA Auditing without the orm.xml file (using JavaConfig instead)?

查看:274
本文介绍了我可以在没有orm.xml文件的情况下使用Spring Data JPA Auditing(使用JavaConfig代替)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让Spring Data Auditing在我的Spring 3.2.8 / Spring Data 1.5 / Hibernate 4项目中运行。

I'm trying to get Spring Data Auditing to work in my Spring 3.2.8 / Spring Data 1.5 / Hibernate 4 project.

根据 Spring Data Auditing docs ,我已经将 @CreatedBy 等注释添加到我的实体,由 AuditorAware 实现创建,并从内部实例化我的JavaConfig。然而,它似乎永远不会发生。

As per the Spring Data Auditing docs, I've added the @CreatedBy, etc annotations to my entities, created by AuditorAware implementation, and instantiated it from within my JavaConfig. However, it never seems to fire.

我发现文档有点令人困惑。似乎JavaConfig条目替换了xml条目,但我不确定。

I find the docs a little confusing. It appears that the JavaConfig entry replaces the xml entry, but I am not sure.

我目前没有任何 orm.xml 我的申请中的文件。说实话,我甚至不确定在哪里/如何配置它,或者为什么我需要它。我的所有实体都在使用注释。我已经尝试将@EntityListeners(AuditingEntityListener.class)添加到实体,但这没有帮助。

I don't currently have any orm.xml file in my application. To be entirely honest, I'm not even sure where/how to configure it, or why I need it. All my entities are using annotations. I have tried adding @EntityListeners(AuditingEntityListener.class) to the entity, but that has not helped.

我的当前实体管理器是在没有persistence.xml文件的情况下定义的:

My current entity manager is defined without a persistence.xml file:

    <!--  entity manager -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
        <property name="packagesToScan" value="com.ia.domain"/>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                <prop key="hibernate.query.substitutions">true '1', false '0'</prop>
                <prop key="hibernate.generate_statistics">true</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.format_sql">true</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>
            </props>
        </property>
    </bean>

JavaConfig:

JavaConfig:

@Configuration
@EnableJpaAuditing
public class AuditConfig {
    @Bean
    public AuditorAware<User> auditorProvider(){
        return new SpringSecurityAuditorAware();
    }
}

实体:

@EntityListeners({AuditingEntityListener.class})
@Entity
public class User
{

  @TableGenerator(name="UUIDGenerator", pkColumnValue="user_id", table="uuid_generator", allocationSize=1)
  @Id
  @GeneratedValue(strategy=GenerationType.TABLE, generator="UUIDGenerator")
  @Column(name="id")
  private Long id;

  @NotNull
  private String username;

  @CreatedDate
  @NotNull
  @Temporal(TemporalType.TIMESTAMP)
  @Column(name="created_date", nullable=false)
  private Date createdDate;

  @LastModifiedDate
  @NotNull
  @Temporal(TemporalType.TIMESTAMP)
  @Column(name="last_modified_date", nullable=false)
  private Date lastModifiedDate;

  @CreatedBy
  @ManyToOne(fetch=FetchType.LAZY)
  @JoinColumn(name="created_by")
  private User createdBy;

  @LastModifiedBy
  @ManyToOne(fetch=FetchType.LAZY)
  @JoinColumn(name="last_modified_by")
  private User lastModifiedBy;
  private String password;
  private Boolean enabled;


...
}

I我已经在我的 SpringSecurityAuditorAware 类中设置了一个断点但它永远不会被击中。

I've put a breakpoint in my SpringSecurityAuditorAware class but it is never being hit.

我还需要一个orm吗? .xml文件?如何/在哪里引用EntityManager?

Do I still need an orm.xml file? How/where is this referenced from the EntityManager?

推荐答案

使用Stephan的答案, https://stackoverflow.com/a/26240077/715640

Using Stephan's answer, https://stackoverflow.com/a/26240077/715640,

我使用自定义监听器工作了。

I got this working using a custom listener.

@Configurable
public class TimestampedEntityAuditListener {

    @PrePersist
    public void touchForCreate(AbstractTimestampedEntity target) {
        Date now = new Date();
        target.setCreated(now);
        target.setUpdated(now);
    }

    @PreUpdate
    public void touchForUpdate(AbstractTimestampedEntity target) {
        target.setUpdated(new Date());
    }
}

然后在我的基类中引用它:

And then referencing it in my base class:

@MappedSuperclass
@EntityListeners({TimestampedEntityAuditListener.class})
public abstract class AbstractTimestampedEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    private Long id;

    @Temporal(TemporalType.TIMESTAMP)
    private Date created;

    @Temporal(TemporalType.TIMESTAMP)
    private Date updated;

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public Date getUpdated() {
        return updated;
    }

    public void setUpdated(Date updated) {
        this.updated = updated;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}

FWIW,我在春天使用它 - 启动项目,没有orm.xml文件。

FWIW, I'm using this in a spring-boot project, without an orm.xml file.

这篇关于我可以在没有orm.xml文件的情况下使用Spring Data JPA Auditing(使用JavaConfig代替)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆