1
//$Id: Property.java 10247 2006-08-11 18:49:33Z steve.ebersole@jboss.com $
2
package org.hibernate.mapping;
4
import java.io.Serializable;
5
import java.util.HashMap;
6
import java.util.Iterator;
7
import java.util.StringTokenizer;
9
import org.hibernate.MappingException;
10
import org.hibernate.PropertyNotFoundException;
11
import org.hibernate.EntityMode;
12
import org.hibernate.engine.CascadeStyle;
13
import org.hibernate.engine.Mapping;
14
import org.hibernate.property.Getter;
15
import org.hibernate.property.PropertyAccessor;
16
import org.hibernate.property.PropertyAccessorFactory;
17
import org.hibernate.property.Setter;
18
import org.hibernate.type.AbstractComponentType;
19
import org.hibernate.type.Type;
20
import org.hibernate.util.ArrayHelper;
23
* Represents a property as part of an entity or a component.
27
public class Property implements Serializable, MetaAttributable {
31
private String cascade;
32
private boolean updateable = true;
33
private boolean insertable = true;
34
private boolean selectable = true;
35
private boolean optimisticLocked = true;
36
private PropertyGeneration generation = PropertyGeneration.NEVER;
37
private String propertyAccessorName;
39
private boolean optional;
40
private String nodeName;
41
private java.util.Map metaAttributes;
42
private PersistentClass persistentClass;
43
private boolean naturalIdentifier;
45
public boolean isBackRef() {
49
public Type getType() throws MappingException {
50
return value.getType();
53
public int getColumnSpan() {
54
return value.getColumnSpan();
57
public Iterator getColumnIterator() {
58
return value.getColumnIterator();
61
public String getName() {
65
public boolean isComposite() {
66
return value instanceof Component;
69
public Value getValue() {
73
public boolean isPrimitive(Class clazz) {
74
return getGetter(clazz).getReturnType().isPrimitive();
77
public CascadeStyle getCascadeStyle() throws MappingException {
78
Type type = value.getType();
79
if ( type.isComponentType() && !type.isAnyType() ) {
80
AbstractComponentType actype = (AbstractComponentType) type;
81
int length = actype.getSubtypes().length;
82
for ( int i=0; i<length; i++ ) {
83
if ( actype.getCascadeStyle(i)!=CascadeStyle.NONE ) return CascadeStyle.ALL;
85
return CascadeStyle.NONE;
87
else if ( cascade==null || cascade.equals("none") ) {
88
return CascadeStyle.NONE;
91
StringTokenizer tokens = new StringTokenizer(cascade, ", ");
92
CascadeStyle[] styles = new CascadeStyle[ tokens.countTokens() ] ;
94
while ( tokens.hasMoreTokens() ) {
95
styles[i++] = CascadeStyle.getCascadeStyle( tokens.nextToken() );
97
return new CascadeStyle.MultipleCascadeStyle(styles);
101
public String getCascade() {
105
public void setCascade(String cascade) {
106
this.cascade = cascade;
109
public void setName(String name) {
110
this.name = name==null ? null : name.intern();
113
public void setValue(Value value) {
117
public boolean isUpdateable() {
118
// if the property mapping consists of all formulas,
119
// make it non-updateable
120
final boolean[] columnUpdateability = value.getColumnUpdateability();
121
return updateable && (
122
//columnUpdateability.length==0 ||
123
!ArrayHelper.isAllFalse(columnUpdateability)
127
public boolean isInsertable() {
128
// if the property mapping consists of all formulas,
129
// make it insertable
130
final boolean[] columnInsertability = value.getColumnInsertability();
131
return insertable && (
132
columnInsertability.length==0 ||
133
!ArrayHelper.isAllFalse(columnInsertability)
137
public PropertyGeneration getGeneration() {
141
public void setGeneration(PropertyGeneration generation) {
142
this.generation = generation;
145
public void setUpdateable(boolean mutable) {
146
this.updateable = mutable;
149
public void setInsertable(boolean insertable) {
150
this.insertable = insertable;
153
public String getPropertyAccessorName() {
154
return propertyAccessorName;
157
public void setPropertyAccessorName(String string) {
158
propertyAccessorName = string;
164
boolean isNullable() {
165
return value==null || value.isNullable();
168
public boolean isBasicPropertyAccessor() {
169
return propertyAccessorName==null || "property".equals(propertyAccessorName);
172
public java.util.Map getMetaAttributes() {
173
return metaAttributes;
176
public MetaAttribute getMetaAttribute(String attributeName) {
177
return metaAttributes==null?null:(MetaAttribute) metaAttributes.get(attributeName);
180
public void setMetaAttributes(java.util.Map metas) {
181
this.metaAttributes = metas;
184
public boolean isValid(Mapping mapping) throws MappingException {
185
return getValue().isValid(mapping);
188
public String toString() {
189
return getClass().getName() + '(' + name + ')';
192
public void setLazy(boolean lazy) {
196
public boolean isLazy() {
197
if ( value instanceof ToOne ) {
198
// both many-to-one and one-to-one are represented as a
199
// Property. EntityPersister is relying on this value to
200
// determine "lazy fetch groups" in terms of field-level
201
// interception. So we need to make sure that we return
202
// true here for the case of many-to-one and one-to-one
203
// with lazy="no-proxy"
205
// * impl note - lazy="no-proxy" currently forces both
206
// lazy and unwrap to be set to true. The other case we
207
// are extremely interested in here is that of lazy="proxy"
208
// where lazy is set to true, but unwrap is set to false.
209
// thus we use both here under the assumption that this
210
// return is really only ever used during persister
211
// construction to determine the lazy property/field fetch
212
// groupings. If that assertion changes then this check
213
// needs to change as well. Partially, this is an issue with
214
// the overloading of the term "lazy" here...
215
ToOne toOneValue = ( ToOne ) value;
216
return toOneValue.isLazy() && toOneValue.isUnwrapProxy();
221
public boolean isOptimisticLocked() {
222
return optimisticLocked;
225
public void setOptimisticLocked(boolean optimisticLocked) {
226
this.optimisticLocked = optimisticLocked;
229
public boolean isOptional() {
230
return optional || isNullable();
233
public void setOptional(boolean optional) {
234
this.optional = optional;
237
public PersistentClass getPersistentClass() {
238
return persistentClass;
241
public void setPersistentClass(PersistentClass persistentClass) {
242
this.persistentClass = persistentClass;
245
public boolean isSelectable() {
249
public void setSelectable(boolean selectable) {
250
this.selectable = selectable;
253
public String getNodeName() {
257
public void setNodeName(String nodeName) {
258
this.nodeName = nodeName;
261
public String getAccessorPropertyName( EntityMode mode ) {
262
if ( mode == EntityMode.DOM4J ) {
271
public Getter getGetter(Class clazz) throws PropertyNotFoundException, MappingException {
272
return getPropertyAccessor(clazz).getGetter(clazz, name);
276
public Setter getSetter(Class clazz) throws PropertyNotFoundException, MappingException {
277
return getPropertyAccessor(clazz).getSetter(clazz, name);
281
public PropertyAccessor getPropertyAccessor(Class clazz) throws MappingException {
282
return PropertyAccessorFactory.getPropertyAccessor( clazz, getPropertyAccessorName() );
285
public boolean isNaturalIdentifier() {
286
return naturalIdentifier;
289
public void setNaturalIdentifier(boolean naturalIdentifier) {
290
this.naturalIdentifier = naturalIdentifier;