/
ManyToOneType.java
275 lines (240 loc) · 7.8 KB
/
ManyToOneType.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* 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.type;
import java.io.Serializable;
import java.util.Arrays;
import org.hibernate.AssertionFailure;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.engine.internal.ForeignKeys;
import org.hibernate.engine.spi.*;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.type.spi.TypeConfiguration;
/**
* A many-to-one association to an entity.
*
* @author Gavin King
*/
public class ManyToOneType extends EntityType {
private final String propertyName;
private final boolean ignoreNotFound;
private final boolean isLogicalOneToOne;
/**
* Creates a many-to-one association type with the given referenced entity.
*/
public ManyToOneType(TypeConfiguration typeConfiguration, String referencedEntityName) {
this( typeConfiguration, referencedEntityName, false );
}
/**
* Creates a many-to-one association type with the given referenced entity and the
* given laziness characteristic
*/
public ManyToOneType(TypeConfiguration typeConfiguration, String referencedEntityName, boolean lazy) {
this( typeConfiguration, referencedEntityName, true, null, null, lazy, true, false, false );
}
public ManyToOneType(
TypeConfiguration typeConfiguration,
String referencedEntityName,
boolean referenceToPrimaryKey,
String uniqueKeyPropertyName,
String propertyName,
boolean lazy,
boolean unwrapProxy,
boolean ignoreNotFound,
boolean isLogicalOneToOne) {
super( typeConfiguration, referencedEntityName, referenceToPrimaryKey, uniqueKeyPropertyName, !lazy, unwrapProxy );
this.propertyName = propertyName;
this.ignoreNotFound = ignoreNotFound;
this.isLogicalOneToOne = isLogicalOneToOne;
}
public ManyToOneType(ManyToOneType original, String superTypeEntityName) {
super( original, superTypeEntityName );
this.propertyName = original.propertyName;
this.ignoreNotFound = original.ignoreNotFound;
this.isLogicalOneToOne = original.isLogicalOneToOne;
}
public ManyToOneType(String name, TypeConfiguration typeConfiguration) {
this( typeConfiguration, name );
}
@Override
public boolean isNullable() {
return ignoreNotFound;
}
@Override
public String getPropertyName() {
return propertyName;
}
@Override
public boolean isAlwaysDirtyChecked() {
// always need to dirty-check, even when non-updatable;
// this ensures that when the association is updated,
// the entity containing this association will be updated
// in the cache
return true;
}
@Override
public boolean isOneToOne() {
return false;
}
@Override
public boolean isLogicalOneToOne() {
return isLogicalOneToOne;
}
@Override
public int getColumnSpan(Mapping mapping) throws MappingException {
return requireIdentifierOrUniqueKeyType( mapping ).getColumnSpan( mapping );
}
@Override
public int[] getSqlTypeCodes(Mapping mapping) throws MappingException {
return requireIdentifierOrUniqueKeyType( mapping ).getSqlTypeCodes( mapping );
}
@Override
public ForeignKeyDirection getForeignKeyDirection() {
return ForeignKeyDirection.FROM_PARENT;
}
/**
* Register the entity as batch loadable, if enabled
*/
private void scheduleBatchLoadIfNeeded(Object id, SharedSessionContractImplementor session) throws MappingException {
//cannot batch fetch by unique key (property-ref associations)
if ( uniqueKeyPropertyName == null && id != null ) {
final EntityPersister persister = getAssociatedEntityPersister( session.getFactory() );
if ( session.getLoadQueryInfluencers().effectivelyBatchLoadable( persister ) ) {
final EntityKey entityKey = session.generateEntityKey( id, persister );
final PersistenceContext persistenceContext = session.getPersistenceContextInternal();
if ( !persistenceContext.containsEntity( entityKey ) ) {
persistenceContext.getBatchFetchQueue().addBatchLoadableEntityKey( entityKey );
}
}
}
}
@Override
public boolean useLHSPrimaryKey() {
return false;
}
@Override
public boolean isModified(
Object old,
Object current,
boolean[] checkable,
SharedSessionContractImplementor session) throws HibernateException {
if ( current == null ) {
return old != null;
}
if ( old == null ) {
// we already know current is not null...
return true;
}
// the ids are fully resolved, so compare them with isDirty(), not isModified()
return getIdentifierOrUniqueKeyType( session.getFactory() )
.isDirty( old, getIdentifier( current, session ), session );
}
@Override
public Serializable disassemble(
Object value,
SharedSessionContractImplementor session,
Object owner) throws HibernateException {
if ( value == null ) {
return null;
}
else {
// cache the actual id of the object, not the value of the
// property-ref, which might not be initialized
Object id = ForeignKeys.getEntityIdentifierIfNotUnsaved(
getAssociatedEntityName(),
value,
session
);
if ( id == null ) {
throw new AssertionFailure(
"cannot cache a reference to an object with a null id: " +
getAssociatedEntityName()
);
}
return getIdentifierType( session ).disassemble( id, session, owner );
}
}
@Override
public Serializable disassemble(Object value, SessionFactoryImplementor sessionFactory) throws HibernateException {
if ( value == null ) {
return null;
}
else {
// cache the actual id of the object, not the value of the
// property-ref, which might not be initialized
Object id = getIdentifier( value, sessionFactory );
if ( id == null ) {
throw new AssertionFailure(
"cannot cache a reference to an object with a null id: " +
getAssociatedEntityName()
);
}
return getIdentifierType( sessionFactory ).disassemble( id, sessionFactory );
}
}
@Override
public Object assemble(
Serializable oid,
SharedSessionContractImplementor session,
Object owner) throws HibernateException {
//TODO: currently broken for unique-key references (does not detect
// change to unique key property of the associated object)
Object id = assembleId( oid, session );
if ( id == null ) {
return null;
}
else {
return resolveIdentifier( id, session );
}
}
private Object assembleId(Serializable oid, SharedSessionContractImplementor session) {
//the owner of the association is not the owner of the id
return getIdentifierType( session ).assemble( oid, session, null );
}
@Override
public void beforeAssemble(Serializable oid, SharedSessionContractImplementor session) {
scheduleBatchLoadIfNeeded( assembleId( oid, session ), session );
}
@Override
public boolean[] toColumnNullness(Object value, Mapping mapping) {
boolean[] result = new boolean[ getColumnSpan( mapping ) ];
if ( value != null ) {
Arrays.fill( result, true );
}
return result;
}
@Override
public boolean isDirty(
Object old,
Object current,
SharedSessionContractImplementor session) throws HibernateException {
if ( isSame( old, current ) ) {
return false;
}
Object oldid = getIdentifier( old, session );
Object newid = getIdentifier( current, session );
return getIdentifierType( session ).isDirty( oldid, newid, session );
}
@Override
public boolean isDirty(
Object old,
Object current,
boolean[] checkable,
SharedSessionContractImplementor session) throws HibernateException {
if ( isAlwaysDirtyChecked() ) {
return isDirty( old, current, session );
}
else {
if ( isSame( old, current ) ) {
return false;
}
Object oldid = getIdentifier( old, session );
Object newid = getIdentifier( current, session );
return getIdentifierType( session ).isDirty( oldid, newid, checkable, session );
}
}
}