Skip to content

Commit

Permalink
HHH-7678 Attribute's type is an @embeddable static class -- incorrectly
Browse files Browse the repository at this point in the history
bound to Basic
  • Loading branch information
brmeyer committed Oct 16, 2012
1 parent 08a756a commit 7f1dbc2
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 31 deletions.
Expand Up @@ -38,14 +38,10 @@
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;

import org.jboss.jandex.IndexView;
import org.jboss.jandex.Indexer;
import org.jboss.logging.Logger;
import org.w3c.dom.Document;

import org.hibernate.HibernateException;
import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.boot.spi.CacheRegionDefinition;
import org.hibernate.jaxb.internal.JaxbMappingProcessor;
import org.hibernate.jaxb.spi.JaxbRoot;
Expand All @@ -56,8 +52,11 @@
import org.hibernate.metamodel.internal.source.annotations.xml.mocker.EntityMappingsMocker;
import org.hibernate.metamodel.spi.source.MappingException;
import org.hibernate.metamodel.spi.source.MappingNotFoundException;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.service.ServiceRegistry;
import org.jboss.jandex.IndexView;
import org.jboss.jandex.Indexer;
import org.jboss.logging.Logger;
import org.w3c.dom.Document;

/**
* Entry point into working with sources of metadata information ({@code hbm.xml}, annotations). Tell Hibernate
Expand Down Expand Up @@ -508,6 +507,11 @@ private void indexClass(Class clazz, Indexer indexer, Set<String> processedNames
// discovered while processing the annotations. To keep this behavior we index all classes in the
// hierarchy (see also HHH-7484)
indexClass( clazz.getSuperclass(), indexer, processedNames );

// Similarly, add any inner classes (see HHH-7678).
for ( Class<?> declaredClass : clazz.getDeclaredClasses() ) {
indexClass( declaredClass, indexer, processedNames );
}
}

private void indexResource(String resourceName, Indexer indexer, Set<String> processedNames) {
Expand Down
Expand Up @@ -427,18 +427,20 @@ private void createMappedAttribute(Member member, ResolvedTypeWithMembers resolv
final ResolvedMember[] resolvedMembers = Field.class.isInstance( member ) ? resolvedType.getMemberFields() : resolvedType
.getMemberMethods();
ResolvedMember resolvedMember = findResolvedMember( member.getName(), resolvedMembers );
Class<?> referencedEntityType = resolveCollectionValuedReferenceType( resolvedMember );
Class<?> attributeType = resolvedMember.getType().getErasedType();
Class<?> referencedCollectionType = resolveCollectionValuedReferenceType( resolvedMember );
final Map<DotName, List<AnnotationInstance>> annotations = JandexHelper.getMemberAnnotations(
classInfo, member.getName()
);

MappedAttribute.Nature attributeNature = determineAttributeNature( annotations, referencedEntityType );
MappedAttribute.Nature attributeNature = determineAttributeNature(
annotations, attributeType, referencedCollectionType );
String accessTypeString = accessType.toString().toLowerCase();
switch ( attributeNature ) {
case BASIC: {
BasicAttribute attribute = BasicAttribute.createSimpleAttribute(
attributeName,
resolvedMember.getType().getErasedType(),
attributeType,
attributeNature,
annotations,
accessTypeString,
Expand All @@ -463,21 +465,19 @@ else if ( attribute.isVersioned() ) {
case EMBEDDED_ID: {
final BasicAttribute attribute = BasicAttribute.createSimpleAttribute(
attributeName,
resolvedMember.getType().getErasedType(),
attributeType,
attributeNature,
annotations,
accessTypeString,
getLocalBindingContext()
);
idAttributeMap.put( attributeName, attribute );
}
//$FALL-THROUGH$
case EMBEDDED: {
final AnnotationInstance targetAnnotation = JandexHelper.getSingleAnnotation(
getClassInfo(),
HibernateDotNames.TARGET
);
Class<?> attributeType = resolvedMember.getType().getErasedType();
if ( targetAnnotation != null ) {
attributeType = localBindingContext.locateClassByName(
JandexHelper.getValue( targetAnnotation, "value", String.class )
Expand Down Expand Up @@ -507,7 +507,7 @@ else if ( attribute.isVersioned() ) {
classInfo,
attributeName,
resolvedMember.getType().getErasedType(),
referencedEntityType,
referencedCollectionType,
attributeNature,
accessTypeString,
annotations,
Expand Down Expand Up @@ -563,11 +563,15 @@ private void resolveEmbeddable(String attributeName, Class<?> type, Map<DotName,
* Given the annotations defined on a persistent attribute this methods determines the attribute type.
*
* @param annotations the annotations defined on the persistent attribute
* @param type the attribute's type
* @param referencedCollectionType the type of the collection element in case the attribute is collection valued
*
* @return an instance of the {@code AttributeType} enum
*/
private MappedAttribute.Nature determineAttributeNature(Map<DotName, List<AnnotationInstance>> annotations, Class<?> referencedCollectionType) {
private MappedAttribute.Nature determineAttributeNature( Map<DotName,
List<AnnotationInstance>> annotations,
Class<?> attributeType,
Class<?> referencedCollectionType ) {
EnumMap<MappedAttribute.Nature, AnnotationInstance> discoveredAttributeTypes =
new EnumMap<MappedAttribute.Nature, AnnotationInstance>( MappedAttribute.Nature.class );

Expand All @@ -591,16 +595,33 @@ private MappedAttribute.Nature determineAttributeNature(Map<DotName, List<Annota
discoveredAttributeTypes.put( MappedAttribute.Nature.MANY_TO_MANY, manyToMany );
}

AnnotationInstance embedded = JandexHelper.getSingleAnnotation( annotations, JPADotNames.EMBEDDED );
if ( embedded != null ) {
discoveredAttributeTypes.put( MappedAttribute.Nature.EMBEDDED, embedded );
}

AnnotationInstance embeddedId = JandexHelper.getSingleAnnotation( annotations, JPADotNames.EMBEDDED_ID );
if ( embeddedId != null ) {
discoveredAttributeTypes.put( MappedAttribute.Nature.EMBEDDED_ID, embeddedId );
}

AnnotationInstance embedded = JandexHelper.getSingleAnnotation(
annotations, JPADotNames.EMBEDDED );
if ( embedded != null ) {
discoveredAttributeTypes.put( MappedAttribute.Nature.EMBEDDED,
embedded );
} else if ( embeddedId == null ) {
// For backward compatibility, we're allowing attributes of an
// @Embeddable type to leave off @Embedded. Check the type's
// annotations. (see HHH-7678)
// However, it's important to ignore this if the field is
// annotated with @EmbeddedId.
ClassInfo typeClassInfo = localBindingContext.getIndex()
.getClassByName( DotName.createSimple( attributeType.getName() ) );
if ( typeClassInfo != null
&& JandexHelper.getSingleAnnotation(
typeClassInfo.annotations(),
JPADotNames.EMBEDDABLE ) != null ) {
discoveredAttributeTypes.put( MappedAttribute.Nature.EMBEDDED,
null );
}
}

AnnotationInstance elementCollection = JandexHelper.getSingleAnnotation(
annotations,
JPADotNames.ELEMENT_COLLECTION
Expand Down
Expand Up @@ -23,29 +23,29 @@
*/
package org.hibernate.test.annotations.access.jpa;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.test.annotations.access.Closet;
import org.hibernate.testing.FailureExpectedWithNewMetamodel;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import org.junit.Test;

/**
* @author Emmanuel Bernard
* @author Hardy Ferentschik
*/
@FailureExpectedWithNewMetamodel
public class AccessTest extends BaseCoreFunctionalTestCase {

@FailureExpectedWithNewMetamodel
@Test
public void testDefaultConfigurationModeIsInherited() throws Exception {
User john = new User();
Expand Down
Expand Up @@ -41,8 +41,12 @@
public class Gardenshed
extends
Woody {

private Integer id;

private String brand;

@Access(javax.persistence.AccessType.FIELD)
public long floors;

@Transient
Expand All @@ -66,7 +70,6 @@ public void setId(Integer id) {

// These 2 functions should not return in Hibernate, but the value should come from the field "floors"

@Access(javax.persistence.AccessType.FIELD)
public long getFloors() {
return this.floors + 2;
}
Expand Down
Expand Up @@ -37,7 +37,6 @@ public class Square {

private long id;

@Embedded
private Position position;

@Id
Expand All @@ -50,6 +49,7 @@ public void setId(long id) {
this.id = id;
}

@Embedded
public Position getPosition() {
return position;
}
Expand Down
Expand Up @@ -41,7 +41,6 @@
* @author Emmanuel Bernard
* @author Hardy Ferentschik
*/
@FailureExpectedWithNewMetamodel
public class DDLTest extends BaseCoreFunctionalTestCase {
@Test
public void testBasicDDL() {
Expand All @@ -66,6 +65,7 @@ public void testLengthConstraint() throws Exception {
assertEquals( column.getSize().getLength(), 5 );
}

@FailureExpectedWithNewMetamodel
@Test
public void testApplyOnManyToOne() throws Exception {
Column column = SchemaUtil.getColumn( TvOwner.class, "tv", metadata() );
Expand All @@ -78,6 +78,7 @@ public void testSingleTableAvoidNotNull() throws Exception {
assertTrue( "Notnull should not be applied on single tables", column.isNullable() );
}

@FailureExpectedWithNewMetamodel
@Test
public void testNotNullOnlyAppliedIfEmbeddedIsNotNullItself() throws Exception {
Column column = SchemaUtil.getColumn( Tv.class, "tuner.frequency", metadata() );
Expand Down
Expand Up @@ -26,6 +26,7 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;

import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.Id;
Expand Down

0 comments on commit 7f1dbc2

Please sign in to comment.