|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * Copyright (c) 2012, Red Hat Inc. or third-party contributors as |
| 5 | + * indicated by the @author tags or express copyright attribution |
| 6 | + * statements applied by the authors. All third-party contributions are |
| 7 | + * distributed under license by Red Hat Inc. |
| 8 | + * |
| 9 | + * This copyrighted material is made available to anyone wishing to use, modify, |
| 10 | + * copy, or redistribute it subject to the terms and conditions of the GNU |
| 11 | + * Lesser General Public License, as published by the Free Software Foundation. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 15 | + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 16 | + * for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Lesser General Public License |
| 19 | + * along with this distribution; if not, write to: |
| 20 | + * Free Software Foundation, Inc. |
| 21 | + * 51 Franklin Street, Fifth Floor |
| 22 | + * Boston, MA 02110-1301 USA |
| 23 | + */ |
| 24 | +package org.hibernate.metamodel.internal.source.hbm; |
| 25 | + |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.List; |
| 28 | + |
| 29 | +import org.hibernate.engine.FetchStyle; |
| 30 | +import org.hibernate.engine.FetchTiming; |
| 31 | +import org.hibernate.mapping.PropertyGeneration; |
| 32 | +import org.hibernate.metamodel.internal.Binder; |
| 33 | +import org.hibernate.metamodel.spi.binding.AttributeBinding; |
| 34 | +import org.hibernate.metamodel.spi.binding.CompositeAttributeBinding; |
| 35 | +import org.hibernate.metamodel.spi.binding.SingularAttributeBinding; |
| 36 | +import org.hibernate.metamodel.spi.relational.Value; |
| 37 | +import org.hibernate.metamodel.spi.source.ExplicitHibernateTypeSource; |
| 38 | +import org.hibernate.metamodel.spi.source.MappingException; |
| 39 | +import org.hibernate.metamodel.spi.source.ToOneAttributeSource; |
| 40 | + |
| 41 | +/** |
| 42 | + * @author Gail Badner |
| 43 | + */ |
| 44 | +public abstract class AbstractToOneAttributeSourceImpl extends AbstractHbmSourceNode implements ToOneAttributeSource{ |
| 45 | + private final SingularAttributeBinding.NaturalIdMutability naturalIdMutability; |
| 46 | + private final String propertyRef; |
| 47 | + |
| 48 | + AbstractToOneAttributeSourceImpl( |
| 49 | + MappingDocument sourceMappingDocument, |
| 50 | + SingularAttributeBinding.NaturalIdMutability naturalIdMutability, |
| 51 | + String propertyRef) { |
| 52 | + super( sourceMappingDocument ); |
| 53 | + this.naturalIdMutability = naturalIdMutability; |
| 54 | + this.propertyRef = propertyRef; |
| 55 | + |
| 56 | + } |
| 57 | + @Override |
| 58 | + public ExplicitHibernateTypeSource getTypeInformation() { |
| 59 | + return Helper.TO_ONE_ATTRIBUTE_TYPE_SOURCE; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public SingularAttributeBinding.NaturalIdMutability getNaturalIdMutability() { |
| 64 | + return naturalIdMutability; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public boolean isSingular() { |
| 69 | + return true; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public boolean isVirtualAttribute() { |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public PropertyGeneration getGeneration() { |
| 79 | + return PropertyGeneration.NEVER; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public boolean isLazy() { |
| 84 | + return getFetchTiming() != FetchTiming.IMMEDIATE; |
| 85 | + } |
| 86 | + |
| 87 | + protected abstract String getFetchSelectionString(); |
| 88 | + protected abstract String getLazySelectionString(); |
| 89 | + protected abstract String getOuterJoinSelectionString(); |
| 90 | + |
| 91 | + @Override |
| 92 | + public boolean isUnWrapProxy() { |
| 93 | + final String lazySelection = getLazySelectionString(); |
| 94 | + return lazySelection != null && lazySelection.equals( "no-proxy" ); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public FetchTiming getFetchTiming() { |
| 99 | + final String lazySelection = getLazySelectionString(); |
| 100 | + |
| 101 | + if ( lazySelection == null ) { |
| 102 | + if ( "join".equals( getFetchSelectionString() ) || "true".equals( getOuterJoinSelectionString() ) ) { |
| 103 | + return FetchTiming.IMMEDIATE; |
| 104 | + } |
| 105 | + else if ( "false".equals( getOuterJoinSelectionString() ) ) { |
| 106 | + return FetchTiming.DELAYED; |
| 107 | + } |
| 108 | + else { |
| 109 | + return bindingContext().getMappingDefaults().areAssociationsLazy() |
| 110 | + ? FetchTiming.DELAYED |
| 111 | + : FetchTiming.IMMEDIATE; |
| 112 | + } |
| 113 | + } |
| 114 | + else if ( "extra".equals( lazySelection ) ) { |
| 115 | + return FetchTiming.EXTRA_DELAYED; |
| 116 | + } |
| 117 | + else if ( "true".equals( lazySelection ) || "proxy".equals( lazySelection ) ) { |
| 118 | + return FetchTiming.DELAYED; |
| 119 | + } |
| 120 | + else if ( "false".equals( lazySelection ) ) { |
| 121 | + return FetchTiming.IMMEDIATE; |
| 122 | + } |
| 123 | + |
| 124 | + throw new MappingException( |
| 125 | + String.format( |
| 126 | + "Unexpected lazy selection [%s] on '%s'", |
| 127 | + lazySelection, |
| 128 | + getName() |
| 129 | + ), |
| 130 | + origin() |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public FetchStyle getFetchStyle() { |
| 136 | + // todo : handle batch fetches? |
| 137 | + |
| 138 | + if ( getFetchSelectionString() == null ) { |
| 139 | + if ( getOuterJoinSelectionString() == null ) { |
| 140 | + return FetchStyle.SELECT; |
| 141 | + } |
| 142 | + else { |
| 143 | + if ( "auto".equals( getOuterJoinSelectionString() ) ) { |
| 144 | + return bindingContext().getMappingDefaults().areAssociationsLazy() |
| 145 | + ? FetchStyle.SELECT |
| 146 | + : FetchStyle.JOIN; |
| 147 | + } |
| 148 | + else { |
| 149 | + return "true".equals( getOuterJoinSelectionString() ) ? FetchStyle.JOIN : FetchStyle.SELECT; |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + else { |
| 154 | + return "join".equals( getFetchSelectionString() ) ? FetchStyle.JOIN : FetchStyle.SELECT; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + // TODO: change to return the default name for a single column |
| 160 | + public List<Binder.DefaultNamingStrategy> getDefaultNamingStrategies(final String entityName, final String tableName, final AttributeBinding referencedAttributeBinding) { |
| 161 | + if ( CompositeAttributeBinding.class.isInstance( referencedAttributeBinding ) ) { |
| 162 | + CompositeAttributeBinding compositeAttributeBinding = CompositeAttributeBinding.class.cast( |
| 163 | + referencedAttributeBinding |
| 164 | + ); |
| 165 | + List<Binder.DefaultNamingStrategy> result = new ArrayList<Binder.DefaultNamingStrategy>(); |
| 166 | + for ( final AttributeBinding attributeBinding : compositeAttributeBinding.attributeBindings() ) { |
| 167 | + result.addAll( getDefaultNamingStrategies( entityName, tableName, attributeBinding ) ); |
| 168 | + } |
| 169 | + return result; |
| 170 | + } |
| 171 | + else { |
| 172 | + List<Binder.DefaultNamingStrategy> result = new ArrayList<Binder.DefaultNamingStrategy>( 1 ); |
| 173 | + result.add( |
| 174 | + new Binder.DefaultNamingStrategy() { |
| 175 | + @Override |
| 176 | + public String defaultName() { |
| 177 | + return bindingContext().getNamingStrategy().propertyToColumnName( getName() ); |
| 178 | + } |
| 179 | + } |
| 180 | + ); |
| 181 | + return result; |
| 182 | + } |
| 183 | + |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public JoinColumnResolutionDelegate getForeignKeyTargetColumnResolutionDelegate() { |
| 188 | + return propertyRef == null |
| 189 | + ? null |
| 190 | + : new JoinColumnResolutionDelegateImpl(); |
| 191 | + } |
| 192 | + |
| 193 | + public class JoinColumnResolutionDelegateImpl implements JoinColumnResolutionDelegate { |
| 194 | + @Override |
| 195 | + public String getReferencedAttributeName() { |
| 196 | + return propertyRef; |
| 197 | + } |
| 198 | + |
| 199 | + @Override |
| 200 | + public List<Value> getJoinColumns(JoinColumnResolutionContext context) { |
| 201 | + return context.resolveRelationalValuesForAttribute( propertyRef ); |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | +} |
0 commit comments