Skip to content

Commit 26908a8

Browse files
committed
HHH-11186 - Add examples for all Hibernate annotations
Document the @ListIndexBase annotation
1 parent 379e32e commit 26908a8

File tree

4 files changed

+193
-3
lines changed

4 files changed

+193
-3
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -741,8 +741,6 @@ See the <<chapters/pc/PersistenceContext.adoc#pc-managed-state-dynamic-update,`@
741741
For reattachment of detached entities, the dynamic update is not possible without having the <<annotations-hibernate-selectbeforeupdate>> annotation as well.
742742
====
743743

744-
//TODO: Add example
745-
746744
[[annotations-hibernate-entity]]
747745
==== [line-through]#`@Entity`#
748746

@@ -933,7 +931,7 @@ The https://docs.jboss.org/hibernate/orm/{majorMinorVersion}/javadocs/org/hibern
933931

934932
By default, `List` indexes are stored starting at zero. Generally used in conjunction with <<annotations-jpa-ordercolumn>>.
935933

936-
//TODO: Add example
934+
See the <<chapters/domain/collections.adoc#collections-customizing-ordered-list-ordinal, `@ListIndexBase` mapping>> section for more info.
937935

938936
[[annotations-hibernate-loader]]
939937
==== `@Loader`

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,36 @@ include::{extrasdir}/collections-bidirectional-ordered-list-order-column-example
380380

381381
When fetching the collection, Hibernate will use the fetched ordered columns to sort the elements according to the `@OrderColumn` mapping.
382382

383+
[[collections-customizing-ordered-list-ordinal]]
384+
===== Customizing ordered list ordinal
385+
386+
You can customize the ordinal of the underlying ordered list by using the https://docs.jboss.org/hibernate/orm/{majorMinorVersion}/javadocs/org/hibernate/annotations/ListIndexBase.html[`@ListIndexBase`] annotation.
387+
388+
[[collections-customizing-ordered-list-ordinal-mapping-example]]
389+
.`@ListIndexBase` mapping example
390+
====
391+
[source,java]
392+
----
393+
include::{sourcedir}/OrderColumnListIndexBaseTest.java[tags=collections-customizing-ordered-list-ordinal-mapping-example,indent=0]
394+
----
395+
====
396+
397+
When inserting two `Phone` records, Hibernate is going to start the List index from 100 this time.
398+
399+
[[collections-customizing-ordered-list-ordinal-persist-example]]
400+
.`@ListIndexBase` persist example
401+
====
402+
[source,java]
403+
----
404+
include::{sourcedir}/OrderColumnListIndexBaseTest.java[tags=collections-customizing-ordered-list-ordinal-persist-example,indent=0]
405+
----
406+
407+
[source,sql]
408+
----
409+
include::{extrasdir}/collections-customizing-ordered-list-ordinal-persist-example.sql[]
410+
----
411+
====
412+
383413
[[collections-set]]
384414
==== Sets
385415

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
INSERT INTO Phone("number", person_id, type, id)
2+
VALUES ('028-234-9876', 1, 'landline', 1)
3+
4+
INSERT INTO Phone("number", person_id, type, id)
5+
VALUES ('072-122-9876', 1, 'mobile', 2)
6+
7+
UPDATE Phone
8+
SET order_id = 100
9+
WHERE id = 1
10+
11+
UPDATE Phone
12+
SET order_id = 101
13+
WHERE id = 2
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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.collections;
8+
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
import java.util.Objects;
12+
import javax.persistence.CascadeType;
13+
import javax.persistence.Column;
14+
import javax.persistence.Entity;
15+
import javax.persistence.Id;
16+
import javax.persistence.ManyToOne;
17+
import javax.persistence.OneToMany;
18+
import javax.persistence.OrderColumn;
19+
20+
import org.hibernate.annotations.ListIndexBase;
21+
import org.hibernate.annotations.NaturalId;
22+
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
23+
24+
import org.junit.Test;
25+
26+
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
27+
28+
/**
29+
* @author Vlad Mihalcea
30+
*/
31+
public class OrderColumnListIndexBaseTest extends BaseEntityManagerFunctionalTestCase {
32+
33+
@Override
34+
protected Class<?>[] getAnnotatedClasses() {
35+
return new Class<?>[] {
36+
Person.class,
37+
Phone.class,
38+
};
39+
}
40+
41+
@Test
42+
public void testLifecycle() {
43+
doInJPA( this::entityManagerFactory, entityManager -> {
44+
//tag::collections-customizing-ordered-list-ordinal-persist-example[]
45+
Person person = new Person( 1L );
46+
entityManager.persist( person );
47+
person.addPhone( new Phone( 1L, "landline", "028-234-9876" ) );
48+
person.addPhone( new Phone( 2L, "mobile", "072-122-9876" ) );
49+
//end::collections-customizing-ordered-list-ordinal-persist-example[]
50+
} );
51+
}
52+
53+
@Entity(name = "Person")
54+
public static class Person {
55+
56+
@Id
57+
private Long id;
58+
59+
//tag::collections-customizing-ordered-list-ordinal-mapping-example[]
60+
@OneToMany(mappedBy = "person", cascade = CascadeType.ALL)
61+
@OrderColumn(name = "order_id")
62+
@ListIndexBase(100)
63+
private List<Phone> phones = new ArrayList<>();
64+
//end::collections-customizing-ordered-list-ordinal-mapping-example[]
65+
66+
public Person() {
67+
}
68+
69+
public Person(Long id) {
70+
this.id = id;
71+
}
72+
73+
public List<Phone> getPhones() {
74+
return phones;
75+
}
76+
77+
public void addPhone(Phone phone) {
78+
phones.add( phone );
79+
phone.setPerson( this );
80+
}
81+
82+
public void removePhone(Phone phone) {
83+
phones.remove( phone );
84+
phone.setPerson( null );
85+
}
86+
}
87+
88+
@Entity(name = "Phone")
89+
public static class Phone {
90+
91+
@Id
92+
private Long id;
93+
94+
private String type;
95+
96+
@Column(name = "`number`", unique = true)
97+
@NaturalId
98+
private String number;
99+
100+
@ManyToOne
101+
private Person person;
102+
103+
public Phone() {
104+
}
105+
106+
public Phone(Long id, String type, String number) {
107+
this.id = id;
108+
this.type = type;
109+
this.number = number;
110+
}
111+
112+
public Long getId() {
113+
return id;
114+
}
115+
116+
public String getType() {
117+
return type;
118+
}
119+
120+
public String getNumber() {
121+
return number;
122+
}
123+
124+
public Person getPerson() {
125+
return person;
126+
}
127+
128+
public void setPerson(Person person) {
129+
this.person = person;
130+
}
131+
132+
@Override
133+
public boolean equals(Object o) {
134+
if ( this == o ) {
135+
return true;
136+
}
137+
if ( o == null || getClass() != o.getClass() ) {
138+
return false;
139+
}
140+
Phone phone = (Phone) o;
141+
return Objects.equals( number, phone.number );
142+
}
143+
144+
@Override
145+
public int hashCode() {
146+
return Objects.hash( number );
147+
}
148+
}
149+
}

0 commit comments

Comments
 (0)