Skip to content

Commit 3d2baa8

Browse files
committed
HHH-11186 - Add examples for all Hibernate annotations
Document @Persister annotation
1 parent a53e118 commit 3d2baa8

File tree

7 files changed

+260
-1
lines changed

7 files changed

+260
-1
lines changed

documentation/src/main/asciidoc/userguide/appendices/Annotations.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,7 @@ For entities, the custom persister must implement the https://docs.jboss.org/hib
11461146

11471147
For collections, the custom persister must implement the https://docs.jboss.org/hibernate/orm/{majorMinorVersion}/javadocs/org/hibernate/persister/collection/CollectionPersister.html[`CollectionPersister`] interface.
11481148

1149-
//TODO: Add example
1149+
See the <<chapters/domain/entity.adoc#entity-persister, `@Persister` mapping>> section for more info.
11501150

11511151
[[annotations-hibernate-polymorphism]]
11521152
==== `@Polymorphism`

documentation/src/main/asciidoc/userguide/chapters/domain/entity.adoc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
:sourcedir-locking: ../../../../../test/java/org/hibernate/userguide/locking
44
:sourcedir-mapping: ../../../../../test/java/org/hibernate/userguide/mapping/basic
55
:sourcedir-proxy: ../../../../../test/java/org/hibernate/userguide/proxy
6+
:sourcedir-persister: ../../../../../test/java/org/hibernate/userguide/persister
67
:extrasdir: extras
78

89
.Usage of the word _entity_
@@ -426,4 +427,28 @@ include::{sourcedir-proxy}/tuplizer/TuplizerTest.java[tag=entity-tuplizer-dynami
426427
----
427428
====
428429

430+
[[entity-persister]]
431+
==== Define a custom entity persister
429432

433+
The https://docs.jboss.org/hibernate/orm/{majorMinorVersion}/javadocs/org/hibernate/annotations/Persister.html[`@Persister`] annotation is used to specify a custom entity or collection persister.
434+
435+
For entities, the custom persister must implement the https://docs.jboss.org/hibernate/orm/{majorMinorVersion}/javadocs/org/hibernate/persister/entity/EntityPersister.html[`EntityPersister`] interface.
436+
437+
For collections, the custom persister must implement the https://docs.jboss.org/hibernate/orm/{majorMinorVersion}/javadocs/org/hibernate/persister/collection/CollectionPersister.html[`CollectionPersister`] interface.
438+
439+
[[entity-persister-mapping]]
440+
.Entity persister mapping
441+
====
442+
[source,java]
443+
----
444+
include::{sourcedir-persister}/Author.java[tag=entity-persister-mapping,indent=0]
445+
----
446+
447+
[source,java]
448+
----
449+
include::{sourcedir-persister}/Book.java[tag=entity-persister-mapping,indent=0]
450+
----
451+
====
452+
453+
By providing you own `EntityPersister` and `CollectionPersister` implementations,
454+
you can control how entities and collections are persisted in to the database.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.userguide.persister;
8+
9+
import java.util.HashSet;
10+
import java.util.Set;
11+
import javax.persistence.Entity;
12+
import javax.persistence.Id;
13+
import javax.persistence.OneToMany;
14+
15+
import org.hibernate.annotations.Persister;
16+
17+
18+
/**
19+
* @author Shawn Clowater
20+
*/
21+
//tag::entity-persister-mapping[]
22+
@Entity
23+
@Persister( impl = EntityPersister.class )
24+
public class Author {
25+
26+
@Id
27+
public Integer id;
28+
29+
@OneToMany( mappedBy = "author" )
30+
@Persister( impl = CollectionPersister.class )
31+
public Set<Book> books = new HashSet<>();
32+
33+
//Getters and setters omitted for brevity
34+
//end::entity-persister-mapping[]
35+
36+
public Integer getId() {
37+
return id;
38+
}
39+
40+
public void setId(Integer id) {
41+
this.id = id;
42+
}
43+
44+
public Set<Book> getBooks() {
45+
return books;
46+
}
47+
48+
//tag::entity-persister-mapping[]
49+
public void addBook(Book book) {
50+
this.books.add( book );
51+
book.setAuthor( this );
52+
}
53+
}
54+
//end::entity-persister-mapping[]
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.userguide.persister;
8+
9+
import javax.persistence.Entity;
10+
import javax.persistence.FetchType;
11+
import javax.persistence.Id;
12+
import javax.persistence.ManyToOne;
13+
14+
import org.hibernate.annotations.Persister;
15+
16+
/**
17+
* @author Shawn Clowater
18+
*/
19+
//tag::entity-persister-mapping[]
20+
21+
@Entity
22+
@Persister( impl = EntityPersister.class )
23+
public class Book {
24+
25+
@Id
26+
public Integer id;
27+
28+
private String title;
29+
30+
@ManyToOne(fetch = FetchType.LAZY)
31+
public Author author;
32+
33+
//Getters and setters omitted for brevity
34+
//end::entity-persister-mapping[]
35+
36+
public Integer getId() {
37+
return id;
38+
}
39+
40+
public void setId(Integer id) {
41+
this.id = id;
42+
}
43+
44+
public String getTitle() {
45+
return title;
46+
}
47+
48+
public void setTitle(String title) {
49+
this.title = title;
50+
}
51+
52+
public Author getAuthor() {
53+
return author;
54+
}
55+
56+
public void setAuthor(Author author) {
57+
this.author = author;
58+
}
59+
//tag::entity-persister-mapping[]
60+
}
61+
//end::entity-persister-mapping[]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.userguide.persister;
8+
9+
import org.hibernate.MappingException;
10+
import org.hibernate.cache.CacheException;
11+
import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy;
12+
import org.hibernate.mapping.Collection;
13+
import org.hibernate.persister.collection.OneToManyPersister;
14+
import org.hibernate.persister.spi.PersisterCreationContext;
15+
16+
/**
17+
* @author Shawn Clowater
18+
*/
19+
//tag::entity-persister-mapping[]
20+
21+
public class CollectionPersister
22+
extends OneToManyPersister {
23+
24+
public CollectionPersister(
25+
Collection collectionBinding,
26+
CollectionRegionAccessStrategy cacheAccessStrategy,
27+
PersisterCreationContext creationContext)
28+
throws MappingException, CacheException {
29+
super( collectionBinding, cacheAccessStrategy, creationContext );
30+
}
31+
}
32+
//end::entity-persister-mapping[]
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.userguide.persister;
8+
9+
import org.hibernate.HibernateException;
10+
import org.hibernate.cache.spi.access.EntityRegionAccessStrategy;
11+
import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy;
12+
import org.hibernate.mapping.PersistentClass;
13+
import org.hibernate.persister.entity.SingleTableEntityPersister;
14+
import org.hibernate.persister.spi.PersisterCreationContext;
15+
16+
/**
17+
* @author Shawn Clowater
18+
*/
19+
//tag::entity-persister-mapping[]
20+
21+
public class EntityPersister
22+
extends SingleTableEntityPersister {
23+
24+
public EntityPersister(
25+
PersistentClass persistentClass,
26+
EntityRegionAccessStrategy cache,
27+
NaturalIdRegionAccessStrategy naturalIdRegionAccessStrategy,
28+
PersisterCreationContext creationContext)
29+
throws HibernateException {
30+
super( persistentClass, cache, naturalIdRegionAccessStrategy, creationContext );
31+
}
32+
}
33+
//end::entity-persister-mapping[]
34+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.userguide.persister;
8+
9+
import org.hibernate.mapping.Collection;
10+
import org.hibernate.mapping.PersistentClass;
11+
import org.hibernate.persister.entity.SingleTableEntityPersister;
12+
13+
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
14+
import org.junit.Test;
15+
16+
import static org.junit.Assert.assertEquals;
17+
18+
/**
19+
* @author Shawn Clowater
20+
*/
21+
public class PersisterTest extends BaseNonConfigCoreFunctionalTestCase {
22+
@Test
23+
public void testEntityEntityPersisterAndPersisterSpecified() throws Exception {
24+
//checks to see that the persister specified with the @Persister annotation takes precedence if a @Entity.persister() is also specified
25+
PersistentClass persistentClass = metadata().getEntityBinding( Author.class.getName() );
26+
assertEquals( "Incorrect Persister class for " + persistentClass.getMappedClass(), EntityPersister.class,
27+
persistentClass.getEntityPersisterClass() );
28+
}
29+
30+
@Test
31+
public void testEntityEntityPersisterSpecified() throws Exception {
32+
//tests the persister specified with an @Entity.persister()
33+
PersistentClass persistentClass = metadata().getEntityBinding( Book.class.getName() );
34+
assertEquals( "Incorrect Persister class for " + persistentClass.getMappedClass(),
35+
SingleTableEntityPersister.class, persistentClass.getEntityPersisterClass() );
36+
}
37+
38+
@Test
39+
public void testCollectionPersisterSpecified() throws Exception {
40+
//tests the persister specified by the @Persister annotation on a collection
41+
Collection collection = metadata().getCollectionBinding( Author.class.getName() + ".cards" );
42+
assertEquals( "Incorrect Persister class for collection " + collection.getRole(), CollectionPersister.class,
43+
collection.getCollectionPersisterClass() );
44+
}
45+
46+
@Override
47+
protected Class[] getAnnotatedClasses() {
48+
return new Class[]{
49+
Book.class,
50+
Author.class
51+
};
52+
}
53+
}

0 commit comments

Comments
 (0)