how to activate an entity listener for all entities

Solutions on MaxInterview for how to activate an entity listener for all entities by the best coders in the world

showing results for - "how to activate an entity listener for all entities"
Cian
10 Feb 2017
1<entity-mappings>
2    <persistence-unit-metadata>
3        <persistence-unit-defaults>
4            <entity-listeners>
5                <entity-listener class="org.thoughts.on.java.model.MyEntityListener"/>
6            </entity-listeners>
7        </persistence-unit-defaults>
8    </persistence-unit-metadata>
9</entity-mappings>
10
Erika
02 Feb 2018
1public class ReplicationInsertEventListener 
2        implements PostInsertEventListener {
3 
4    public static final ReplicationInsertEventListener INSTANCE = 
5        new ReplicationInsertEventListener();
6 
7    @Override
8    public void onPostInsert(
9            PostInsertEvent event) 
10            throws HibernateException {
11        final Object entity = event.getEntity();
12 
13        if(entity instanceof Post) {
14            Post post = (Post) entity;
15 
16            event.getSession().createNativeQuery(
17                "INSERT INTO old_post (id, title, version) " +
18                "VALUES (:id, :title, :version)")
19            .setParameter("id", post.getId())
20            .setParameter("title", post.getTitle())
21            .setParameter("version", post.getVersion())
22            .setFlushMode(FlushMode.MANUAL)
23            .executeUpdate();
24        }
25    }
26 
27    @Override
28    public boolean requiresPostCommitHanding(
29            EntityPersister persister) {
30        return false;
31    }
32}
33