/
AbstractHibernateOrmTypeContext.java
166 lines (146 loc) · 6.25 KB
/
AbstractHibernateOrmTypeContext.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
/*
* 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.mapper.orm.mapping.impl;
import java.util.List;
import java.util.stream.Collectors;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.metamodel.MappingMetamodel;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.search.mapper.orm.event.impl.HibernateOrmListenerTypeContext;
import org.hibernate.search.mapper.orm.loading.impl.HibernateOrmEntityIdEntityLoadingStrategy;
import org.hibernate.search.mapper.orm.loading.impl.HibernateOrmEntityLoadingStrategy;
import org.hibernate.search.mapper.orm.loading.impl.HibernateOrmNonEntityIdPropertyEntityLoadingStrategy;
import org.hibernate.search.mapper.orm.loading.impl.LoadingTypeContext;
import org.hibernate.search.mapper.orm.session.impl.HibernateOrmSessionTypeContext;
import org.hibernate.search.mapper.pojo.identity.spi.IdentifierMapping;
import org.hibernate.search.mapper.pojo.mapping.building.spi.PojoTypeExtendedMappingCollector;
import org.hibernate.search.mapper.pojo.model.path.spi.PojoPathFilter;
import org.hibernate.search.mapper.pojo.model.spi.PojoPropertyModel;
import org.hibernate.search.mapper.pojo.model.spi.PojoRawTypeIdentifier;
import org.hibernate.search.mapper.pojo.model.spi.PojoRawTypeModel;
import org.hibernate.search.mapper.pojo.model.spi.PojoTypeContext;
import org.hibernate.search.util.common.reflect.spi.ValueReadHandle;
abstract class AbstractHibernateOrmTypeContext<E>
implements PojoTypeContext<E>, HibernateOrmListenerTypeContext, HibernateOrmSessionTypeContext<E>, LoadingTypeContext<E> {
private final PojoRawTypeIdentifier<E> typeIdentifier;
private final String jpaEntityName;
private final EntityPersister entityPersister;
private final boolean documentIdIsEntityId;
private final HibernateOrmEntityLoadingStrategy<? super E, ?> loadingStrategy;
private final IdentifierMapping identifierMapping;
private final PojoPathFilter dirtyFilter;
private final List<PojoRawTypeIdentifier<? super E>> ascendingSuperTypes;
// Casts are safe because the loading strategy will target either "E" or "? super E", by contract
@SuppressWarnings("unchecked")
AbstractHibernateOrmTypeContext(AbstractBuilder<E> builder, SessionFactoryImplementor sessionFactory) {
this.typeIdentifier = builder.typeIdentifier;
this.jpaEntityName = builder.jpaEntityName;
MappingMetamodel metamodel = sessionFactory.getMetamodel();
this.entityPersister = metamodel.getEntityDescriptor( builder.hibernateOrmEntityName );
this.identifierMapping = builder.identifierMapping;
this.ascendingSuperTypes = builder.ascendingSuperTypes;
if ( builder.documentIdSourcePropertyName != null ) {
if ( builder.documentIdSourcePropertyName.equals( entityPersister().getIdentifierPropertyName() ) ) {
documentIdIsEntityId = true;
loadingStrategy = (HibernateOrmEntityLoadingStrategy<? super E, ?>)
HibernateOrmEntityIdEntityLoadingStrategy.create( sessionFactory, entityPersister() );
}
else {
// The entity ID is not the property used to generate the document ID
// We need to use a criteria query to load entities from the document IDs
documentIdIsEntityId = false;
loadingStrategy = (HibernateOrmEntityLoadingStrategy<? super E, ?>)
HibernateOrmNonEntityIdPropertyEntityLoadingStrategy.create( sessionFactory, entityPersister(),
builder.documentIdSourcePropertyName, builder.documentIdSourcePropertyHandle
);
}
}
else {
// Can only happen for contained types, which may not be loadable.
documentIdIsEntityId = false;
loadingStrategy = null;
}
this.dirtyFilter = builder.dirtyFilter;
}
@Override
public String toString() {
return typeIdentifier().toString();
}
@Override
public PojoRawTypeIdentifier<E> typeIdentifier() {
return typeIdentifier;
}
@Override
public String jpaEntityName() {
return jpaEntityName;
}
public String hibernateOrmEntityName() {
return entityPersister.getEntityName();
}
@Override
public EntityPersister entityPersister() {
return entityPersister;
}
@Override
public IdentifierMapping identifierMapping() {
return identifierMapping;
}
@Override
public HibernateOrmEntityLoadingStrategy<? super E, ?> loadingStrategy() {
return loadingStrategy;
}
@Override
public List<PojoRawTypeIdentifier<? super E>> ascendingSuperTypes() {
return ascendingSuperTypes;
}
@Override
public Object toIndexingPlanProvidedId(Object entityId) {
if ( documentIdIsEntityId ) {
return entityId;
}
else {
// The entity ID is not the property used to generate the document ID
// Return null, meaning the document ID has to be extracted from the entity
return null;
}
}
@Override
public PojoPathFilter dirtyFilter() {
return dirtyFilter;
}
abstract static class AbstractBuilder<E> implements PojoTypeExtendedMappingCollector {
private final PojoRawTypeIdentifier<E> typeIdentifier;
private final String jpaEntityName;
private final String hibernateOrmEntityName;
private String documentIdSourcePropertyName;
private ValueReadHandle<?> documentIdSourcePropertyHandle;
private IdentifierMapping identifierMapping;
private PojoPathFilter dirtyFilter;
private final List<PojoRawTypeIdentifier<? super E>> ascendingSuperTypes;
AbstractBuilder(PojoRawTypeModel<E> typeModel, String jpaEntityName, String hibernateOrmEntityName) {
this.typeIdentifier = typeModel.typeIdentifier();
this.jpaEntityName = jpaEntityName;
this.hibernateOrmEntityName = hibernateOrmEntityName;
this.ascendingSuperTypes = typeModel.ascendingSuperTypes()
.map( PojoRawTypeModel::typeIdentifier )
.collect( Collectors.toList() );
}
@Override
public void documentIdSourceProperty(PojoPropertyModel<?> documentIdSourceProperty) {
this.documentIdSourcePropertyName = documentIdSourceProperty.name();
this.documentIdSourcePropertyHandle = documentIdSourceProperty.handle();
}
@Override
public void identifierMapping(IdentifierMapping identifierMapping) {
this.identifierMapping = identifierMapping;
}
@Override
public void dirtyFilter(PojoPathFilter dirtyFilter) {
this.dirtyFilter = dirtyFilter;
}
}
}