Skip to content

Commit

Permalink
HHH-7571 Starting to process @jointable and @CollectionTable
Browse files Browse the repository at this point in the history
Moving JoinColumn processing into association attribute
Making anonymous ExplicitHibernateTypeSource implementation its own class
  • Loading branch information
hferentschik committed Sep 6, 2012
1 parent d661c38 commit ebcc59f
Show file tree
Hide file tree
Showing 8 changed files with 253 additions and 54 deletions.
Expand Up @@ -22,7 +22,7 @@ public BasicPluralAttributeElementSourceImpl(AssociationAttribute associationAtt

@Override
public ExplicitHibernateTypeSource getExplicitHibernateTypeSource() {
return null; //To change body of implemented methods use File | Settings | File Templates.
return new ExplicitHibernateTypeSourceImpl( associationAttribute );
}

@Override
Expand All @@ -42,7 +42,7 @@ else if ( MappedAttribute.Nature.ELEMENT_COLLECTION_EMBEDDABLE.equals( associati

@Override
public List<RelationalValueSource> relationalValueSources() {
List<RelationalValueSource> valueSources = new ArrayList<RelationalValueSource>( );
List<RelationalValueSource> valueSources = new ArrayList<RelationalValueSource>();
if ( !associationAttribute.getColumnValues().isEmpty() ) {
for ( Column columnValues : associationAttribute.getColumnValues() ) {
valueSources.add( new ColumnSourceImpl( associationAttribute, null, columnValues ) );
Expand All @@ -51,6 +51,7 @@ public List<RelationalValueSource> relationalValueSources() {
return valueSources;
}

// TODO - these values are also hard coded in the hbm version of this source implementation. Do we really need them? (HF)
@Override
public boolean areValuesIncludedInInsertByDefault() {
return true;
Expand Down
@@ -0,0 +1,52 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.metamodel.internal.source.annotations;

import java.util.Map;

import org.hibernate.metamodel.internal.source.annotations.attribute.AssociationAttribute;
import org.hibernate.metamodel.spi.source.ExplicitHibernateTypeSource;

/**
* @author Hardy Ferentschik
*/
public class ExplicitHibernateTypeSourceImpl implements ExplicitHibernateTypeSource {
private final AssociationAttribute attribute;

public ExplicitHibernateTypeSourceImpl(AssociationAttribute attribute) {
this.attribute = attribute;
}

@Override
public String getName() {
return attribute.getHibernateTypeResolver().getExplicitHibernateTypeName();
}

@Override
public Map<String, String> getParameters() {
return attribute.getHibernateTypeResolver().getExplicitHibernateTypeParameters();
}
}


Expand Up @@ -61,17 +61,7 @@ public PluralAttributeSourceImpl(final PluralAssociationAttribute attribute) {
this.nature = resolveAttributeNature();
this.keySource = new PluralAttributeKeySourceImpl( attribute );
this.elementSource = determineElementSource();
this.typeSource = new ExplicitHibernateTypeSource() {
@Override
public String getName() {
return attribute.getHibernateTypeResolver().getExplicitHibernateTypeName();
}

@Override
public Map<String, String> getParameters() {
return attribute.getHibernateTypeResolver().getExplicitHibernateTypeParameters();
}
};
this.typeSource = new ExplicitHibernateTypeSourceImpl( attribute );
}

@Override
Expand Down
Expand Up @@ -23,6 +23,8 @@
*/
package org.hibernate.metamodel.internal.source.annotations.attribute;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -61,14 +63,12 @@ public class AssociationAttribute extends MappedAttribute {
private final boolean isOptional;
private final boolean isLazy;
private final boolean isOrphanRemoval;
// todo FetchMode is currently used in the persisters. This will probably get replaced bt FetchStyle and FetchTiming
private final FetchMode fetchMode;
private final FetchStyle fetchStyle;
private final boolean mapsId;
private final String referencedIdAttributeName;

private boolean isInsertable = true;
private boolean isUpdatable = true;
private final List<Column> joinColumnValues;
private final boolean definesExplicitJoinTable;
private AttributeTypeResolver resolver;

public static AssociationAttribute createAssociationAttribute(
Expand All @@ -78,11 +78,17 @@ public static AssociationAttribute createAssociationAttribute(
String accessType,
Map<DotName, List<AnnotationInstance>> annotations,
EntityBindingContext context) {
return new AssociationAttribute( name, attributeType, attributeType, attributeNature, accessType, annotations, context );
return new AssociationAttribute(
name,
attributeType,
attributeType,
attributeNature,
accessType,
annotations,
context
);
}



AssociationAttribute(
String name,
Class<?> attributeType,
Expand All @@ -106,11 +112,14 @@ public static AssociationAttribute createAssociationAttribute(
this.isLazy = determineIsLazy( associationAnnotation );
this.isOrphanRemoval = determineOrphanRemoval( associationAnnotation );
this.cascadeTypes = determineCascadeTypes( associationAnnotation );
this.joinColumnValues = determineJoinColumnAnnotations( annotations );

this.fetchMode = determineFetchMode();
this.fetchStyle = determineFetchStyle();
this.referencedIdAttributeName = determineMapsId();
this.mapsId = referencedIdAttributeName != null;

this.definesExplicitJoinTable = determineExplicitJoinTable( annotations );
}

public boolean isIgnoreNotFound() {
Expand Down Expand Up @@ -149,6 +158,14 @@ public boolean mapsId() {
return mapsId;
}

public List<Column> getJoinColumnValues() {
return joinColumnValues;
}

public boolean definesExplicitJoinTable() {
return definesExplicitJoinTable;
}

@Override
public AttributeTypeResolver getHibernateTypeResolver() {
if ( resolver == null ) {
Expand All @@ -169,12 +186,12 @@ public boolean isOptional() {

@Override
public boolean isInsertable() {
return isInsertable;
return true;
}

@Override
public boolean isUpdatable() {
return isUpdatable;
return true;
}

@Override
Expand Down Expand Up @@ -316,6 +333,77 @@ private String determineMapsId() {
}
return JandexHelper.getValue( mapsIdAnnotation, "value", String.class );
}

private List<Column> determineJoinColumnAnnotations(Map<DotName, List<AnnotationInstance>> annotations) {
ArrayList<Column> joinColumns = new ArrayList<Column>();

// single @JoinColumn
AnnotationInstance joinColumnAnnotation = JandexHelper.getSingleAnnotation(
annotations,
JPADotNames.JOIN_COLUMN
);
if ( joinColumnAnnotation != null ) {
joinColumns.add( new Column( joinColumnAnnotation ) );
}

// @JoinColumns
AnnotationInstance joinColumnsAnnotation = JandexHelper.getSingleAnnotation(
annotations,
JPADotNames.JOIN_COLUMNS
);
if ( joinColumnsAnnotation != null ) {
List<AnnotationInstance> columnsList = Arrays.asList(
JandexHelper.getValue( joinColumnsAnnotation, "value", AnnotationInstance[].class )
);
for ( AnnotationInstance annotation : columnsList ) {
joinColumns.add( new Column( annotation ) );
}
}
joinColumns.trimToSize();
return joinColumns;
}

private boolean determineExplicitJoinTable(Map<DotName, List<AnnotationInstance>> annotations) {
AnnotationInstance collectionTableAnnotation = JandexHelper.getSingleAnnotation(
annotations,
JPADotNames.COLLECTION_TABLE
);

AnnotationInstance joinTableAnnotation = JandexHelper.getSingleAnnotation(
annotations,
JPADotNames.JOIN_TABLE
);

// sanity checks
if ( collectionTableAnnotation != null && joinTableAnnotation != null ) {
throw new MappingException(
"@CollectionTable and JoinTable specified on the same attribute",
getContext().getOrigin()
);
}

if ( collectionTableAnnotation != null ) {
if ( JandexHelper.getSingleAnnotation( annotations, JPADotNames.ELEMENT_COLLECTION ) == null ) {
throw new MappingException(
"@CollectionTable annotation without a @ElementCollection",
getContext().getOrigin()
);
}
}

if ( joinTableAnnotation != null ) {
if ( JandexHelper.getSingleAnnotation( annotations, JPADotNames.ONE_TO_ONE ) == null
&& JandexHelper.getSingleAnnotation( annotations, JPADotNames.ONE_TO_MANY ) == null
&& JandexHelper.getSingleAnnotation( annotations, JPADotNames.MANY_TO_MANY ) == null ) {
throw new MappingException(
"@JoinTable annotation without an association",
getContext().getOrigin()
);
}
}

return collectionTableAnnotation != null || joinTableAnnotation != null;
}
}


Expand Up @@ -63,7 +63,7 @@ public abstract class MappedAttribute implements Comparable<MappedAttribute> {
/**
* The nature of the attribute
*/
Nature attributeNature;
private final Nature attributeNature;

/**
* The access type for this property. At the moment this is either 'field' or 'property', but Hibernate
Expand All @@ -77,8 +77,6 @@ public abstract class MappedAttribute implements Comparable<MappedAttribute> {
*/
private List<Column> columnValues = new ArrayList<Column>();

private List<Column> joinColumnValues = new ArrayList<Column>();

/**
* Is this property an id property (or part thereof).
*/
Expand Down Expand Up @@ -154,10 +152,6 @@ public List<Column> getColumnValues() {
return columnValues;
}

public List<Column> getJoinColumnValues() {
return joinColumnValues;
}

public boolean isId() {
return isId;
}
Expand Down Expand Up @@ -250,15 +244,6 @@ private void checkColumnAnnotations(Map<DotName, List<AnnotationInstance>> annot
columnValues.add( new Column( columnAnnotation ) );
}

// single @JoinColumn
AnnotationInstance joinColumnAnnotation = JandexHelper.getSingleAnnotation(
annotations,
JPADotNames.JOIN_COLUMN
);
if ( joinColumnAnnotation != null ) {
joinColumnValues.add( new Column( joinColumnAnnotation ) );
}

// @org.hibernate.annotations.Columns
AnnotationInstance columnsAnnotation = JandexHelper.getSingleAnnotation(
annotations,
Expand All @@ -278,20 +263,6 @@ private void checkColumnAnnotations(Map<DotName, List<AnnotationInstance>> annot
columnValues.add( new Column( annotation ) );
}
}

// @JoinColumns
AnnotationInstance joinColumnsAnnotation = JandexHelper.getSingleAnnotation(
annotations,
JPADotNames.JOIN_COLUMNS
);
if ( joinColumnsAnnotation != null ) {
List<AnnotationInstance> columnsList = Arrays.asList(
JandexHelper.getValue( joinColumnsAnnotation, "value", AnnotationInstance[].class )
);
for ( AnnotationInstance annotation : columnsList ) {
joinColumnValues.add( new Column( annotation ) );
}
}
}

private String checkCheckAnnotation() {
Expand Down

0 comments on commit ebcc59f

Please sign in to comment.