Skip to content

Commit 519acb2

Browse files
committed
HHH-6132 @FetchProfiles should throw MappingException when invalid
entity or association used
1 parent b679529 commit 519acb2

File tree

6 files changed

+131
-80
lines changed

6 files changed

+131
-80
lines changed

hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/annotations/global/FetchProfileProcessor.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@
3434
import org.hibernate.metamodel.source.internal.annotations.AnnotationBindingContext;
3535
import org.hibernate.metamodel.source.internal.annotations.util.HibernateDotNames;
3636
import org.hibernate.metamodel.source.internal.annotations.util.JandexHelper;
37+
import org.hibernate.metamodel.spi.binding.EntityBinding;
3738
import org.hibernate.metamodel.spi.binding.FetchProfile;
3839
import org.hibernate.metamodel.spi.binding.FetchProfile.Fetch;
39-
40+
import org.hibernate.metamodel.spi.domain.Attribute;
4041
import org.jboss.jandex.AnnotationInstance;
4142

4243
/**
@@ -54,7 +55,6 @@ private FetchProfileProcessor() {
5455
*
5556
* @param bindingContext the context for annotation binding
5657
*/
57-
// TODO verify that association exists. See former VerifyFetchProfileReferenceSecondPass
5858
public static void bind(AnnotationBindingContext bindingContext) {
5959

6060
Collection<AnnotationInstance> annotations = bindingContext.getJandexAccess()
@@ -93,8 +93,19 @@ private static void bind(AnnotationBindingContext bindingContext, AnnotationInst
9393
if ( !fetchMode.equals( org.hibernate.annotations.FetchMode.JOIN ) ) {
9494
throw new MappingException( "Only FetchMode.JOIN is currently supported" );
9595
}
96+
9697
final String entityName = JandexHelper.getValue( override, "entity", String.class, classLoaderService );
9798
final String associationName = JandexHelper.getValue( override, "association", String.class, classLoaderService );
99+
100+
EntityBinding entityBinding = bindingContext.getMetadataCollector().getEntityBinding( entityName );
101+
if ( entityBinding == null ) {
102+
throw new MappingException( "FetchProfile " + name + " references an unknown entity: " + entityName );
103+
}
104+
Attribute attributeBinding = entityBinding.getAttributeContainer().locateAttribute( associationName );
105+
if ( attributeBinding == null ) {
106+
throw new MappingException( "FetchProfile " + name + " references an unknown association: " + associationName );
107+
}
108+
98109
fetches.add( new Fetch( entityName, associationName, fetchMode.toString().toLowerCase() ) );
99110
}
100111
bindingContext.getMetadataCollector().addFetchProfile( new FetchProfile( name, fetches ) );

hibernate-core/src/test/java/org/hibernate/metamodel/internal/source/Foo.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,24 @@
2323
*/
2424
package org.hibernate.metamodel.internal.source;
2525

26+
import java.util.List;
27+
28+
import javax.persistence.ElementCollection;
29+
import javax.persistence.Entity;
30+
import javax.persistence.GeneratedValue;
31+
import javax.persistence.Id;
32+
2633
/**
2734
* @author Hardy Ferentschik
2835
*/
36+
@Entity
2937
public class Foo {
38+
@Id
39+
@GeneratedValue
40+
public long id;
41+
42+
@ElementCollection
43+
public List<String> bars;
3044
}
3145

3246

hibernate-core/src/test/java/org/hibernate/metamodel/internal/source/MetadataImplTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public void testAddingNullPackageName() {
6565
public void testAddingPackageName() {
6666
MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().build() );
6767
sources.addPackage( "org.hibernate.metamodel.internal.source" );
68+
sources.addAnnotatedClass( Foo.class );
6869
MetadataImpl metadata = (MetadataImpl) sources.buildMetadata();
6970

7071
assertFetchProfile( metadata );
@@ -74,6 +75,7 @@ public void testAddingPackageName() {
7475
public void testAddingPackageNameWithTrailingDot() {
7576
MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().build() );
7677
sources.addPackage( "org.hibernate.metamodel.internal.source." );
78+
sources.addAnnotatedClass( Foo.class );
7779
MetadataImpl metadata = (MetadataImpl) sources.buildMetadata();
7880

7981
assertFetchProfile( metadata );

hibernate-core/src/test/java/org/hibernate/metamodel/internal/source/annotations/global/FetchProfileBinderTest.java

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
package org.hibernate.metamodel.internal.source.annotations.global;
2525

2626
import java.util.Iterator;
27+
import java.util.List;
28+
29+
import javax.persistence.ElementCollection;
30+
import javax.persistence.Entity;
31+
import javax.persistence.GeneratedValue;
32+
import javax.persistence.Id;
2733

2834
import org.hibernate.MappingException;
2935
import org.hibernate.annotations.FetchMode;
@@ -33,7 +39,6 @@
3339
import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl;
3440
import org.hibernate.metamodel.Metadata;
3541
import org.hibernate.metamodel.MetadataSources;
36-
3742
import org.hibernate.testing.junit4.BaseUnitTestCase;
3843
import org.junit.After;
3944
import org.junit.Before;
@@ -62,9 +67,16 @@ public void tearDown() {
6267
@Test
6368
public void testSingleFetchProfile() {
6469
@FetchProfile(name = "foo", fetchOverrides = {
65-
@FetchProfile.FetchOverride(entity = Foo.class, association = "bar", mode = FetchMode.JOIN)
70+
@FetchProfile.FetchOverride(entity = Foo.class, association = "bars", mode = FetchMode.JOIN)
6671
})
72+
@Entity
6773
class Foo {
74+
@Id
75+
@GeneratedValue
76+
public long id;
77+
78+
@ElementCollection
79+
public List<String> bars;
6880
}
6981

7082
Metadata meta = new MetadataSources( serviceRegistry ).addAnnotatedClass( Foo.class ).buildMetadata();
@@ -74,7 +86,7 @@ class Foo {
7486
org.hibernate.metamodel.spi.binding.FetchProfile profile = mappedFetchProfiles.next();
7587
assertEquals( "Wrong fetch profile name", "foo", profile.getName() );
7688
org.hibernate.metamodel.spi.binding.FetchProfile.Fetch fetch = profile.getFetches().iterator().next();
77-
assertEquals( "Wrong association name", "bar", fetch.getAssociation() );
89+
assertEquals( "Wrong association name", "bars", fetch.getAssociation() );
7890
assertEquals( "Wrong association type", Foo.class.getName(), fetch.getEntity() );
7991
}
8092

@@ -95,12 +107,12 @@ public void testFetchProfiles() {
95107
private void assertProfiles(org.hibernate.metamodel.spi.binding.FetchProfile profile) {
96108
if ( profile.getName().equals( "foobar" ) ) {
97109
org.hibernate.metamodel.spi.binding.FetchProfile.Fetch fetch = profile.getFetches().iterator().next();
98-
assertEquals( "Wrong association name", "foobar", fetch.getAssociation() );
110+
assertEquals( "Wrong association name", "fubars", fetch.getAssociation() );
99111
assertEquals( "Wrong association type", FooBar.class.getName(), fetch.getEntity() );
100112
}
101113
else if ( profile.getName().equals( "fubar" ) ) {
102114
org.hibernate.metamodel.spi.binding.FetchProfile.Fetch fetch = profile.getFetches().iterator().next();
103-
assertEquals( "Wrong association name", "fubar", fetch.getAssociation() );
115+
assertEquals( "Wrong association name", "fubars", fetch.getAssociation() );
104116
assertEquals( "Wrong association type", FooBar.class.getName(), fetch.getEntity() );
105117
}
106118
else {
@@ -111,23 +123,37 @@ else if ( profile.getName().equals( "fubar" ) ) {
111123
@Test(expected = MappingException.class)
112124
public void testNonJoinFetchThrowsException() {
113125
@FetchProfile(name = "foo", fetchOverrides = {
114-
@FetchProfile.FetchOverride(entity = Foo.class, association = "bar", mode = FetchMode.SELECT)
126+
@FetchProfile.FetchOverride(entity = Foo.class, association = "bars", mode = FetchMode.SELECT)
115127
})
128+
@Entity
116129
class Foo {
130+
@Id
131+
@GeneratedValue
132+
public long id;
133+
134+
@ElementCollection
135+
public List<String> bars;
117136
}
118137

119138
new MetadataSources( serviceRegistry ).addAnnotatedClass( Foo.class ).buildMetadata();
120139
}
121140

122141
@FetchProfiles( {
123142
@FetchProfile(name = "foobar", fetchOverrides = {
124-
@FetchProfile.FetchOverride(entity = FooBar.class, association = "foobar", mode = FetchMode.JOIN)
143+
@FetchProfile.FetchOverride(entity = FooBar.class, association = "fubars", mode = FetchMode.JOIN)
125144
}),
126145
@FetchProfile(name = "fubar", fetchOverrides = {
127-
@FetchProfile.FetchOverride(entity = FooBar.class, association = "fubar", mode = FetchMode.JOIN)
146+
@FetchProfile.FetchOverride(entity = FooBar.class, association = "fubars", mode = FetchMode.JOIN)
128147
})
129148
})
149+
@Entity
130150
class FooBar {
151+
@Id
152+
@GeneratedValue
153+
public long id;
154+
155+
@ElementCollection
156+
public List<String> fubars;
131157
}
132158
}
133159

hibernate-core/src/test/java/org/hibernate/metamodel/internal/source/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* Boston, MA 02110-1301 USA
2323
*/
2424
@FetchProfile(name = "package-configured-profile", fetchOverrides = {
25-
@FetchProfile.FetchOverride(entity = Foo.class, association = "bar", mode = FetchMode.JOIN)
25+
@FetchProfile.FetchOverride(entity = Foo.class, association = "bars", mode = FetchMode.JOIN)
2626
})
2727
package org.hibernate.metamodel.internal.source;
2828

0 commit comments

Comments
 (0)