0
私はプロジェクトに取り掛かりました。エンティティが作成または更新されたときに通知を受ける必要があります。しかし、残念ながら、@RepositoryEventHandler
は機能しませんでした。だから、私は簡単なバージョンを用意していますが、私も運がありませんでした。以下、問題を再現するために必要と思ったすべてのコンポーネントを書いています。RepositoryEventHandlerが呼び出されていません
マイコンフィギュ
@Configuration
@ComponentScan(basePackages = "db")
@EnableJpaRepositories(basePackages = {"db"})
//@EnableSpringDataWebSupport
public class Config {
@Bean
@Autowired
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) throws Exception {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setShowSql(Boolean.TRUE);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("db");
factory.setDataSource(dataSource);
factory.setJpaProperties(getHibernateProperties());
return factory;
}
@Bean
public DataSource getDataSource() throws Exception {
Properties p = new Properties();
Class.forName("org.h2.Driver");
p.put("driverClassName", "org.h2.Driver");
p.put("user", "admin");
p.put("password", "123");
DataSource ds = new DriverManagerDataSource("jdbc:h2:~/desktop/test;", p);
return ds;
}
@Bean(name = "transactionManager")
public PlatformTransactionManager getTransactionManager(DataSource ds) {
DataSourceTransactionManager txManager = new DataSourceTransactionManager();
try {
txManager.setDataSource(getDataSource());
} catch (Exception e) {
e.printStackTrace();
}
return txManager;
}
private Properties getHibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
properties.put("hibernate.hbm2ddl.auto", "create");
properties.put("hibernate.connection.charSet", "UTF-8");
properties.put("hibernate.id.new_generator_mappings", "false");
properties.put("hibernate.max_fetch_depth", "3");
properties.put("hibernate.connection.autocommit", "false");
properties.put("hibernate.connection.release_mode", "on_close");
return properties;
}
}
私のPOJOの定義:
@javax.persistence.Entity
public class Entity {
private static final long serialVersionUID = 3501352854892096642L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id ;
private String name ;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
マイリポジトリ:
@RepositoryRestResource
public interface EntityRep extends PagingAndSortingRepository<Entity,Long> {
}
と最後に、これは私のイベントハンドラ定義クラスであります:
@Component
@RepositoryEventHandler(Entity.class)
public class EventHandler {
@HandleBeforeSave
@HandleBeforeCreate
public void doSomething(Entity e){
System.out.println("Hi...");
}
}
私はコンテナの春データ休息2.6.4は、Hibernate 5.2.10.FinalとTomcat 8.5.12を使用していることに留意すべきです。さらに、Web.xmlに次の構成を使用しました。
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>db</param-value>
</context-param>
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.data.rest.webmvc.RepositoryRestDispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
ありがとうございました。
「動作しませんでした」とはどういう意味ですか?それは呼ばれることはありませんか?例外はありますか? ... –
それは呼ばれません。 – Masoud