-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#53 - Create AnnotationUsage for entity-listener and callback methods
- Loading branch information
Showing
7 changed files
with
236 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...models-orm/src/test/java/org/hibernate/models/orm/xml/lifecycle/EntityLifecycleTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright: Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.models.orm.xml.lifecycle; | ||
|
||
import java.util.List; | ||
|
||
import org.hibernate.boot.internal.BootstrapContextImpl; | ||
import org.hibernate.boot.internal.MetadataBuilderImpl; | ||
import org.hibernate.boot.model.process.spi.ManagedResources; | ||
import org.hibernate.boot.registry.StandardServiceRegistry; | ||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; | ||
import org.hibernate.models.orm.process.ManagedResourcesImpl; | ||
import org.hibernate.models.orm.spi.CategorizedDomainModel; | ||
import org.hibernate.models.orm.spi.EntityTypeMetadata; | ||
import org.hibernate.models.orm.xml.SimpleEntity; | ||
import org.hibernate.models.source.spi.AnnotationUsage; | ||
import org.hibernate.models.source.spi.ClassDetails; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import jakarta.persistence.EntityListeners; | ||
import jakarta.persistence.PostLoad; | ||
import jakarta.persistence.PostPersist; | ||
import jakarta.persistence.PostRemove; | ||
import jakarta.persistence.PostUpdate; | ||
import jakarta.persistence.PrePersist; | ||
import jakarta.persistence.PreRemove; | ||
import jakarta.persistence.PreUpdate; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.hibernate.models.orm.spi.ManagedResourcesProcessor.processManagedResources; | ||
|
||
/** | ||
* @author Marco Belladelli | ||
*/ | ||
public class EntityLifecycleTests { | ||
@Test | ||
void testEntityLifecycle() { | ||
final ManagedResources managedResources = new ManagedResourcesImpl.Builder() | ||
.addXmlMappings( "mappings/lifecycle/entity-lifecycle.xml" ) | ||
.build(); | ||
try (StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().build()) { | ||
final BootstrapContextImpl bootstrapContext = new BootstrapContextImpl( | ||
serviceRegistry, | ||
new MetadataBuilderImpl.MetadataBuildingOptionsImpl( serviceRegistry ) | ||
); | ||
final CategorizedDomainModel categorizedDomainModel = processManagedResources( | ||
managedResources, | ||
bootstrapContext | ||
); | ||
|
||
assertThat( categorizedDomainModel.getEntityHierarchies() ).hasSize( 1 ); | ||
final EntityTypeMetadata rootEntity = categorizedDomainModel.getEntityHierarchies() | ||
.iterator() | ||
.next() | ||
.getRoot(); | ||
final ClassDetails classDetails = rootEntity.getClassDetails(); | ||
assertThat( classDetails.getName() ).isEqualTo( SimpleEntity.class.getName() ); | ||
|
||
// lifecycle callback methods | ||
assertThat( classDetails.findMethodByName( "prePersist" ).getAnnotationUsage( PrePersist.class ) ).isNotNull(); | ||
assertThat( classDetails.findMethodByName( "preRemove" ).getAnnotationUsage( PreRemove.class ) ).isNotNull(); | ||
assertThat( classDetails.findMethodByName( "preUpdate" ).getAnnotationUsage( PreUpdate.class ) ).isNotNull(); | ||
|
||
// entity listeners | ||
final AnnotationUsage<EntityListeners> entityListenersAnn = classDetails.getAnnotationUsage( EntityListeners.class ); | ||
assertThat( entityListenersAnn ).isNotNull(); | ||
final List<ClassDetails> entityListeners = entityListenersAnn.getAttributeValue( "value" ); | ||
assertThat( entityListeners ).hasSize( 1 ); | ||
final ClassDetails listener = entityListeners.get( 0 ); | ||
assertThat( listener.getName() ).isEqualTo( SimpleEntityListener.class.getName() ); | ||
assertThat( listener.findMethodByName( "postPersist" ).getAnnotationUsage( PostPersist.class ) ).isNotNull(); | ||
assertThat( listener.findMethodByName( "postRemove" ).getAnnotationUsage( PostRemove.class ) ).isNotNull(); | ||
assertThat( listener.findMethodByName( "postUpdate" ).getAnnotationUsage( PostUpdate.class ) ).isNotNull(); | ||
assertThat( listener.findMethodByName( "postLoad" ).getAnnotationUsage( PostLoad.class ) ).isNotNull(); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...models-orm/src/test/java/org/hibernate/models/orm/xml/lifecycle/SimpleEntityListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright: Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.models.orm.xml.lifecycle; | ||
|
||
/** | ||
* @author Marco Belladelli | ||
*/ | ||
public class SimpleEntityListener { | ||
public void postPersist() { | ||
// used by hibernate lifecycle callbacks | ||
} | ||
|
||
public void postRemove() { | ||
// used by hibernate lifecycle callbacks | ||
} | ||
|
||
public void postUpdate() { | ||
// used by hibernate lifecycle callbacks | ||
} | ||
|
||
public void postLoad() { | ||
// used by hibernate lifecycle callbacks | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
hibernate-models-orm/src/test/resources/mappings/lifecycle/entity-lifecycle.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<!-- | ||
~ Hibernate, Relational Persistence for Idiomatic Java | ||
~ | ||
~ SPDX-License-Identifier: Apache-2.0 | ||
~ Copyright: Red Hat Inc. and Hibernate Authors | ||
--> | ||
<entity-mappings xmlns="http://www.hibernate.org/xsd/orm/mapping" | ||
version="3.1"> | ||
<entity class="org.hibernate.models.orm.xml.SimpleEntity" access="FIELD"> | ||
<entity-listeners> | ||
<entity-listener class="org.hibernate.models.orm.xml.lifecycle.SimpleEntityListener"> | ||
<post-persist method-name="postPersist"/> | ||
<post-remove method-name="postRemove"/> | ||
<post-update method-name="postUpdate"/> | ||
<post-load method-name="postLoad"/> | ||
</entity-listener> | ||
</entity-listeners> | ||
<pre-persist method-name="prePersist"/> | ||
<pre-remove method-name="preRemove"/> | ||
<pre-update method-name="preUpdate"/> | ||
|
||
<attributes> | ||
<id name="id"/> | ||
<basic name="name"/> | ||
</attributes> | ||
</entity> | ||
</entity-mappings> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters