/
GenericPropertyIT.java
172 lines (134 loc) · 4.34 KB
/
GenericPropertyIT.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.integrationtest.mapper.orm.model;
import static org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmUtils.with;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import jakarta.persistence.Basic;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import org.hibernate.SessionFactory;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.GenericField;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.IndexedEmbedded;
import org.hibernate.search.util.impl.integrationtest.common.rule.BackendMock;
import org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmSetupHelper;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
public class GenericPropertyIT {
@Rule
public BackendMock backendMock = new BackendMock();
@Rule
public OrmSetupHelper ormSetupHelper = OrmSetupHelper.withBackendMock( backendMock );
private SessionFactory sessionFactory;
@Before
public void setup() {
backendMock.expectSchema( IndexedEntity.INDEX, b -> b
.objectField( "genericProperty", b2 -> b2
/*
* If generics are not handled correctly, these fields will have type "T" or "Object"
* and Hibernate Search will fail to resolve the bridge for them
*/
.field( "content", String.class )
.field( "arrayContent", String.class, b3 -> b3.multiValued( true ) )
)
);
sessionFactory = ormSetupHelper.start()
.setup(
IndexedEntity.class,
GenericEntity.class,
StringGenericEntity.class
);
backendMock.verifyExpectationsMet();
}
@Test
public void index() {
with( sessionFactory ).runInTransaction( session -> {
IndexedEntity entity1 = new IndexedEntity();
entity1.setId( 1 );
GenericEntity<String> genericEntity = new StringGenericEntity();
genericEntity.setId( 2 );
genericEntity.setContent( "genericEntityContent" );
genericEntity.setArrayContent( new String[] { "entry1", "entry2" } );
entity1.setGenericProperty( genericEntity );
genericEntity.getContainingEntities().add( entity1 );
session.persist( genericEntity );
session.persist( entity1 );
backendMock.expectWorks( IndexedEntity.INDEX )
.add( "1", b -> b
.objectField( "genericProperty", b2 -> b2
.field( "content", genericEntity.getContent() )
.field( "arrayContent", genericEntity.getArrayContent()[0] )
.field( "arrayContent", genericEntity.getArrayContent()[1] )
)
);
} );
}
@Entity(name = "indexed")
@Indexed(index = IndexedEntity.INDEX)
public static final class IndexedEntity {
static final String INDEX = "IndexedEntity";
@Id
private Integer id;
@ManyToOne
private GenericEntity<String> genericProperty;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@IndexedEmbedded
public GenericEntity<String> getGenericProperty() {
return genericProperty;
}
public void setGenericProperty(GenericEntity<String> genericProperty) {
this.genericProperty = genericProperty;
}
}
@Entity(name = "generic")
public abstract static class GenericEntity<T extends Serializable> {
@Id
private Integer id;
@Basic
@GenericField
private T content;
@Basic
@GenericField
private T[] arrayContent;
@OneToMany(mappedBy = "genericProperty")
private List<IndexedEntity> containingEntities = new ArrayList<>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public T getContent() {
return content;
}
public void setContent(T content) {
this.content = content;
}
public T[] getArrayContent() {
return arrayContent;
}
public void setArrayContent(T[] arrayContent) {
this.arrayContent = arrayContent;
}
public List<IndexedEntity> getContainingEntities() {
return containingEntities;
}
}
@Entity(name = "stringgeneric")
public static class StringGenericEntity extends GenericEntity<String> {
}
}