相关疑难解决方法(0)

将Spring依赖项注入JPA EntityListener

我试图将Spring依赖注入到JPA EntityListener中.这是我的听众课程:

@Configurable(autowire = Autowire.BY_TYPE, dependencyCheck = true)
public class PliListener {

    @Autowired
    private EvenementPliRepository evenementPliRepository;

    @PostPersist
    void onPostPersist(Pli pli) {
        EvenementPli ev = new EvenementPli();
        ev.setPli(pli);
        ev.setDateCreation(new Date());
        ev.setType(TypeEvenement.creation);
        ev.setMessage("Création d'un pli");
        System.out.println("evenementPliRepository: " + evenementPliRepository);
        evenementPliRepository.save(ev);
    }


}
Run Code Online (Sandbox Code Playgroud)

这是我的Entity类:

@RooJavaBean
@RooToString
@RooJpaActiveRecord
@EntityListeners(PliListener.class)
public class Pli implements Serializable{
...
Run Code Online (Sandbox Code Playgroud)

但是,我的依赖(即evenementPliRepository)始终为null.

有人可以帮忙吗?

spring dependency-injection jpa spring-roo entitylisteners

43
推荐指数
12
解决办法
4万
查看次数

如何在Spring Boot中使用Spring管理的Hibernate拦截器?

是否有可能在Spring Boot中集成Spring管理的Hibernate拦截器(http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch14.html)?

我正在使用Spring Data JPA和Spring Data REST,并且需要一个Hibernate拦截器来对实体上的特定字段进行更新.

使用标准JPA事件,不可能获得旧值,因此我认为我需要使用Hibernate拦截器.

hibernate spring-data spring-data-jpa spring-data-rest spring-boot

32
推荐指数
6
解决办法
4万
查看次数

如何自动生成创建或修改的时间戳字段?

我的实体类:

@Entity
@Table(name = "user")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name = "USER_ID_GENERATOR", sequenceName = "USER_SEQ")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "USER_ID_GENERATOR")
    @Column(name = "user_id")
    private long userId;


    @Temporal(TemporalType.DATE)
    private Date created;

    @Temporal(TemporalType.DATE)
    private Date modified;

    //setters and getters...
}
Run Code Online (Sandbox Code Playgroud)

我想在创建或修改对象时自动将CREATED和MODIFIED字段相互补充.CREATED和MODIFIED字段应为TIMESTAMP类型.

我如何实现这一目标?

orm hibernate jpa

16
推荐指数
5
解决办法
3万
查看次数

Tomcat - 不会加载我的META-INF\services\javax.servlet.ServletContainerInitializer文件?

我有一个Web项目,其\META-INF\services\javax.servlet.ServletContainerInitializer文件的内容指向实现ServletContainerInitializer接口的类的完全限定名称.我基本上遵循这里给出的例子:http://nullhaus.com/2011/03/using-servlets-3-0-servletcontainerinitializer/

我把调试行放在我的类中,它实现了ServletContainerInitializer接口,它永远不会在那里.甚至不是默认的构造函数......

我的应用程序文件夹结构如下:

\MyApp
      \META-INF\services\javax.servlet.ServletContainerInitializer
      \WEB-INF\classes\
                 ... [list of classes and packages go here]
Run Code Online (Sandbox Code Playgroud)

我需要检查的任何想法?

注1:我的Tomcat从包含我的应用程序的爆炸外部文件夹发布

注2:我从Eclipse开始使用Tomcat - 如果这有所不同的话!

java eclipse tomcat web-applications

10
推荐指数
2
解决办法
8354
查看次数

JSR-303依赖注入和Hibernate

Spring 3.0.2,Hibernate 3.5.0,Hibernate-Validator 4.0.2.GA

我试图使用以下方法将Spring依赖项注入ConstraintValidator:

@PersistenceContext
private EntityManager entityManager;
Run Code Online (Sandbox Code Playgroud)

我已经配置了应用程序上下文:

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
Run Code Online (Sandbox Code Playgroud)

根据Spring文档,应该允许"自定义ConstraintValidators像其他任何Spring bean一样受益于依赖注入"

在调试器中,我可以看到Spring调用getBean来创建ConstraintValidator.稍后当flush触发preInsert时,会创建并调用另一个ConstraintValidator.问题是EntityManager在这个新的ConstraintValidator中是null.我尝试在ConstraintValidator中注入其他依赖项,这些依赖项始终为null.

有谁知道是否有可能将依赖注入ConstraintValidator?

validation spring hibernate dependency-injection bean-validation

9
推荐指数
3
解决办法
1万
查看次数

如何为Spring 3/Hibernate 4 LocalSessionFactoryBean设置事件监听器?

我正在使用Spring 3.1.0.RELEASE和Hibernate 4.0.1.Final.如何为SessionFactory设置事件监听器?最后,我试图在这个线程之后将一个bean注入到Hibernate实体中 - 通过Spring将字段注入由Hibernate加载的实体.但是,由于以下异常,这是失败的.我的配置文件看起来像

<bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" id="sessionFactory">
    <!-- <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> 
        </property> -->
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.myco.myproject.domain.Registration</value>
            <value>com.myco.myproject.domain.Role</value>
            <value>com.myco.myproject.domain.EventFeed</value>
            <value>com.myco.myproject.domain.Event</value>
            <value>com.myco.myproject.domain.Address</value>
            <value>com.myco.myproject.domain.Category</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="show_sql">true</prop>
            <prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
        </props>
    </property>
    <property name="eventListeners">
        <map>
            <entry key="post-load">
                <bean class="com.myco.myproject.listeners.myprojectLoadListener"></bean>
            </entry>
        </map>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

这是令人讨厌的例外......

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:290)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) …
Run Code Online (Sandbox Code Playgroud)

java spring hibernate dependency-injection

6
推荐指数
1
解决办法
2万
查看次数

在JPA实体监听器中注入spring bean

我试图让JPA Entity Listener通过将其标记为@Configurable来了解spring上下文.但是注入的spring bean是null.能够使用相同的技术使JPA实体了解Spring上下文.我使用Spring(core和data-jpa)作为基础设施.有关如何使用JPA Entity Listeners或spring data-jpa实现此目的的任何想法?

@Configurable
@Scope("singleton")
public class AggregateRootListener {
    private static Logger log = LoggerFactory.getLogger(AggregateRootListener.class);

    @Autowired
    private EventHandlerHelper eventHandlerHelper;

    @PostPersist
    @PostUpdate
    public void publishEvents(BaseAggregateRoot aggregateRoot){
        log.info(aggregateRoot.getEvents().toString());
        aggregateRoot.getEvents().stream()
            .forEach(event -> {
                eventHandlerHelper.notify(event, aggregateRoot);
                log.info("Publishing " + event + " " + aggregateRoot.toString());
            });
    }
}
Run Code Online (Sandbox Code Playgroud)

和BaseAggregateRoot代码

@Configurable
@Scope("prototype")
@MappedSuperclass
@EntityListeners(AggregateRootListener.class)
public abstract class  BaseAggregateRoot extends BaseDomain{
    public static enum AggregateStatus {
        ACTIVE, ARCHIVE
    }

    @EmbeddedId
    @AttributeOverrides({
          @AttributeOverride(name = "aggregateId", column = @Column(name = "ID", nullable = false))}) …
Run Code Online (Sandbox Code Playgroud)

spring jpa aspectj spring-data-jpa

3
推荐指数
1
解决办法
5471
查看次数