私はHiberante 5とSpring 4.2.3を使用しています。 SessionFactoryスコープにeventListenerを追加する方法が見つかりません。私は休止状態がオブジェクトを持続する前に日付を設定する必要があります。私はSessionFactoryのはspring.xmlEventListener Hibernate 5
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>com/hibernate/mapping/User.hbm.xml</value>
<value>com/hibernate/mapping/PublicKey.hbm.xml</value>
<value>com/hibernate/mapping/File.hbm.xml</value>
<value>com/hibernate/mapping/Url.hbm.xml</value>
<value>com/hibernate/mapping/Role.hbm.xml</value>
<value>com/hibernate/mapping/Operation.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.max_size">50</prop>
<prop key="hibernate.c3p0.timeout">100</prop>
<prop key="hibernate.c3p0.max_statements">50</prop>
<prop key="hibernate.c3p0.idle_test_period">3000</prop>
<prop key="hibernate.c3p0.validate">true</prop>
</props>
</property>
</bean>
で定義されており、このSessionFactoryの取得はどこでGenericDAOImplを持っている:私はそれを行うには、いくつかの方法を見てきました
public abstract class GenericDAOImpl <T, PK extends Serializable> implements GenericDAO<T, PK> {
private SessionFactory sessionFactory;
/** Domain class the DAO instance will be responsible for */
protected Class<T> type;
@SuppressWarnings("unchecked")
public GenericDAOImpl() {
Type t = getClass().getGenericSuperclass();
ParameterizedType pt = (ParameterizedType) t;
type = (Class<T>) pt.getActualTypeArguments()[0];
}
@SuppressWarnings("unchecked")
public PK create(T o) {
return (PK) getSession().save(o);
}
public T read(PK id) {
return (T) getSession().get(type, id);
}
public void update(T o) {
getSession().update(o);
}
public void delete(T o) {
getSession().delete(o);
}
@SuppressWarnings("unchecked")
public List<T> getAll() {
return getSession().createCriteria(type).list();
}
protected Session getSession() {
return sessionFactory.getCurrentSession();
}
// getters and setters
}
を、それらのいくつかは動作しません。 Hiberante 5のために(Integrator
のように)。そして、私はこれをxml経由でeventListenerを追加する方法や、sessionFactoryスコープのコードを追加する方法を見つけることはできません。セッションスコープに追加する方法を見つけましたsessionFactory.withOptions().eventListeners(listeners);
ありがとうございました!
あなたはインターセプタと試みたことがありますか? http://docs.jboss.org/hibernate/core/3.3/reference/en/html/events.html –
@ MarcoA.Hernandez私はeventListenerの前でこの可能性を試していましたが、インターセプタを追加する方法が見つかりませんでしたXMLを介したSessionFactory。私はコードが 'new Configuration()。setInterceptor(new AuditInterceptor())'と同じくらい単純であることを知っています。しかし、私はsessionFactoryをXMLで定義しました。 – angeldev
決して試みたことはありませんが、org.springframework.orm.hibernate5.LocalSessionFactoryBeanを継承し、コンストラクタでConfiguration()。setInterceptor(...)を追加してから、xml設定ファイルで使用するクラスを作成できるとします。 –