我可以在没有 orm.xml 文件的情况下使用 Spring Data JPA 审计(改用 JavaConfig)吗?

2024-08-24Java开发问题
3

vpn下载 本文介绍了我可以在没有 orm.xml 文件的情况下使用 Spring Data 免费vpn JPA 审计(改用 JavaConfig)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧! 免费vpn下载 vpn下载

问题描述

我正在尝试让 Spring Data Auditing 在我的 Spring 3.2.8/Spring Data 1.5/Hibernate 4 vpn free 项目中工作.

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

根据 Spring Data Auditing docs,我已将 免费vpn下载 @CreatedBy 等注释添加到我的实体中,由 AuditorAware vpn下载 实现创建,并在我的 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 vpn下载 免费vpn vpn free entry, but I am not sure.

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

I don't currently have any vpn free orm.xml file 免费vpn下载 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. 免费vpn I have tried adding @EntityListeners(AuditingEntityListener.class) to the entity, but that has not helped.

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

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

    <!--  entity manager -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
   免费vpn      <property name="dataSource" ref="dataSource"/>
       free vpn  <property 免费vpn name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
        vpn vpn下载 free <property name="packagesToScan" 免费vpn value="com.ia.domain"/>
        <property name="jpaProperties">
            <props>
     vpn free      vpn free       <prop 免费vpn下载 key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
     vpn free        free vpn     <prop key="hibernate.query.substitutions">true '1', false '0'</prop>
    vpn下载             <prop key="hibernate.generate_statistics">true</prop>
  free vpn vpn free               <prop key="hibernate.show_sql">false</prop>
    vpn free             <prop key="hibernate.format_sql">true</prop>
          免费vpn     免费vpn   <prop key="hibernate.hbm2ddl.auto">update</prop>
         vpn free    vpn free     <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
  免费vpn        免费vpn  vpn下载   免费vpn下载     vpn free <prop key="hibernate.connection.charSet">UTF-8</prop>
            </props>
       free vpn  </property>
    </bean>

Java配置:

@Configuration
@EnableJpaAuditing
public class AuditConfig {
  free vpn   @Bean
    public vpn下载 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
 vpn下载  @NotNull
  @Temporal(TemporalType.TIMESTAMP)
  @Column(name="last_modified_date", nullable=false)
  private Date lastModifiedDate;

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

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


...
}

我在 SpringSecurityAuditorAware free 免费vpn vpn 类中设置了一个断点,但它从未被击中.

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?

推荐答案

短版:无

从 JPA 2.0 开始,无法在没有 XML 文件 (orm.xml) 的情况下定义此类实体侦听器.

Short version: No

As of JPA 2.0, it is not possible vpn下载 to define such entity listener vpn下载 without an XML file free vpn 免费vpn (orm.xml).

默认实体侦听器——适用于持久单元中所有实体的实体侦听器——可以通过 vpn 免费vpn free XML 描述符指定.(第 93 vpn free 页)

Default entity listeners—entity listeners vpn free that apply to all entities vpn下载 in the persistence unit—can be specified vpn下载 by means of the XML vpn下载 descriptor. (p.93)

长版:解决方法...

如果您项目中的所有实体都扩展了 AbstractAuditable 超类,那么您可以将 @EntityListeners({AuditingEntityListener.class}) 放在 AbstractAuditable 上.附加到实体类的侦听器由其子类继承.

Long version: The workaround...

If all entities in your project extends vpn free an AbstractAuditable superclass then you can put @EntityListeners({AuditingEntityListener.class}) on AbstractAuditable. Listeners attached to 免费vpn an entity class are inherited by its subclasses.

继承层次结构中的多个实体类和映射的超类可以定义侦听器类和/或直接在类上的生命周期回调方法.(第 93 页)

Multiple entity classes and 免费vpn mapped superclasses in vpn下载 an inheritance hierarchy vpn free may define listener classes and/or lifecycle callback methods directly on the class. (p.93)

请注意,子类可以使用 @ExcludeSuperclassListeners 注释显式排除继承的侦听器.

Note that a subclass can exclude explicitly an vpn下载 inherited listener using the @ExcludeSuperclassListeners annotation.

我想引用规范中最后一个有趣的脚注:

There is one last interesting free vpn footnote from the spec vpn下载 I'd like to quote:

排除的侦听器可以通过列出来重新引入实体类它们在 EntityListeners 注释或 free vpn XML 中显式实体侦听器元素.(脚注 [45] 第 97 页)

Excluded listeners may be vpn下载 reintroduced on an 免费vpn下载 entity class by listing them explicitly in the EntityListeners annotation or XML entity-listeners element. (Footnote [45] p.97)

<小时>

以下是一些用于说明解决方法的代码:


Here is some code for illustrating the workaround:

AbstractAuditableEntity.java

import java.util.Date;

import javax.persistence.EntityListeners;
import 免费vpn下载 vpn下载 javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import 免费vpn javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

@MappedSuperclass
@EntityListeners({AuditingEntityListener.class}) // AuditingEntityListener will also audit any subclasses of AbstractAuditable...
public abstract class AbstractAuditableEntity 免费vpn下载 vpn free {
    @Id
    @GeneratedValue
    private Long id;

   免费vpn下载  @CreatedDate
 免费vpn下载    @Temporal(TemporalType.TIMESTAMP)
    private Date createdDate;

    @LastModifiedDate
   免费vpn下载  @Temporal(TemporalType.TIMESTAMP)
  free vpn   private Date lastModifiedDate;
}

MyEntity.java

@Entity
public abstract class MyEntity vpn下载 extends AbstractAuditableEntity {

}

我认为可以使用接口 Auditable (@EntityListeners 可以出现在接口上)而不是 AbstractAuditable 类,但我没有试试看……

I think an interface Auditable 免费vpn may be used (@EntityListeners can appear on an interface) instead of an AbstractAuditable class but I 免费vpn didn't try...

参考:JSR-000317 Java Persistence 2.0 - 最终版本

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

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 vpn free JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse vpn下载 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 免费vpn RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
免费vpn 2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 免费vpn下载 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13