Skip to content

Commit cf1971b

Browse files
committed
HHH-9771 - Registering JPA Entity-Listeners in orm.xml doesn't work
1 parent 5f36892 commit cf1971b

File tree

4 files changed

+209
-0
lines changed

4 files changed

+209
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2015, Red Hat Inc. or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
package org.hibernate.jpa.test.callbacks.xml;
25+
26+
import javax.persistence.EntityManager;
27+
28+
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
29+
30+
import org.hibernate.testing.TestForIssue;
31+
import org.junit.Test;
32+
33+
import static org.junit.Assert.assertEquals;
34+
35+
/**
36+
* @author Steve Ebersole
37+
*/
38+
public class EntityListenerViaXmlTest extends BaseEntityManagerFunctionalTestCase {
39+
@Override
40+
protected String[] getMappings() {
41+
return new String[] { "org/hibernate/jpa/test/callbacks/xml/MyEntity.orm.xml" };
42+
}
43+
44+
@Test
45+
@TestForIssue( jiraKey = "HHH-9771" )
46+
public void testUsage() {
47+
JournalingListener.reset();
48+
49+
EntityManager em = getOrCreateEntityManager();
50+
em.getTransaction().begin();
51+
em.persist( new MyEntity( 1, "steve" ) );
52+
em.getTransaction().commit();
53+
em.close();
54+
55+
assertEquals( 1, JournalingListener.getPrePersistCount() );
56+
57+
em = getOrCreateEntityManager();
58+
em.getTransaction().begin();
59+
em.createQuery( "delete MyEntity" ).executeUpdate();
60+
em.getTransaction().commit();
61+
em.close();
62+
}
63+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) $year, Red Hat Inc. or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
package org.hibernate.jpa.test.callbacks.xml;
25+
26+
/**
27+
* An entity listener that keeps a journal of calls
28+
*
29+
* @author Steve Ebersole
30+
*/
31+
public class JournalingListener {
32+
private static int prePersistCount;
33+
34+
public void onPrePersist(Object entity) {
35+
prePersistCount++;
36+
}
37+
38+
public static int getPrePersistCount() {
39+
return prePersistCount;
40+
}
41+
42+
public static void reset() {
43+
prePersistCount = 0;
44+
}
45+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) $year, Red Hat Inc. or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
package org.hibernate.jpa.test.callbacks.xml;
25+
26+
/**
27+
* @author Steve Ebersole
28+
*/
29+
public class MyEntity {
30+
private Integer id;
31+
private String name;
32+
33+
public MyEntity() {
34+
}
35+
36+
public MyEntity(Integer id, String name) {
37+
this.id = id;
38+
this.name = name;
39+
}
40+
41+
public Integer getId() {
42+
return id;
43+
}
44+
45+
public void setId(Integer id) {
46+
this.id = id;
47+
}
48+
49+
public String getName() {
50+
return name;
51+
}
52+
53+
public void setName(String name) {
54+
this.name = name;
55+
}
56+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
<!--
3+
~ Hibernate, Relational Persistence for Idiomatic Java
4+
~
5+
~ Copyright (c) $year, Red Hat Inc. or third-party contributors as
6+
~ indicated by the @author tags or express copyright attribution
7+
~ statements applied by the authors. All third-party contributions are
8+
~ distributed under license by Red Hat Inc.
9+
~
10+
~ This copyrighted material is made available to anyone wishing to use, modify,
11+
~ copy, or redistribute it subject to the terms and conditions of the GNU
12+
~ Lesser General Public License, as published by the Free Software Foundation.
13+
~
14+
~ This program is distributed in the hope that it will be useful,
15+
~ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
~ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
17+
~ for more details.
18+
~
19+
~ You should have received a copy of the GNU Lesser General Public License
20+
~ along with this distribution; if not, write to:
21+
~ Free Software Foundation, Inc.
22+
~ 51 Franklin Street, Fifth Floor
23+
~ Boston, MA 02110-1301 USA
24+
-->
25+
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
26+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
27+
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
28+
version="2.0">
29+
<persistence-unit-metadata>
30+
<persistence-unit-defaults>
31+
<entity-listeners>
32+
<entity-listener class="org.hibernate.jpa.test.callbacks.xml.JournalingListener">
33+
<post-persist method-name="onPrePersist"/>
34+
</entity-listener>
35+
</entity-listeners>
36+
</persistence-unit-defaults>
37+
</persistence-unit-metadata>
38+
39+
<entity class="org.hibernate.jpa.test.callbacks.xml.MyEntity">
40+
<attributes>
41+
<id name="id"/>
42+
<basic name="name"/>
43+
</attributes>
44+
</entity>
45+
</entity-mappings>

0 commit comments

Comments
 (0)