Over a million developers have joined DZone.
Platinum Partner

No Subclass Matches This Class; for This Aggregate Mapping With Inheritance

· Java Zone

Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround.

No subclass matches this class [class com.baselogic.test.CustomerFixture$2$1] for this Aggregate mapping with inheritance. I have been writing a chapter for my new book “Java EE6 Cookbook for Securing, Tuning and Extending Enterprise applications” today, and I ran across an interesting error based on a JPA testing pattern.

I have used 2 patterns for seeding data to test some @CollectionTable functionality.
First, I used DBUnit to seed my data, and that seems to work just fine for adding @CollectionTable relationships.

The second seemed fairly straight forward. I wanted to create a Fixture utility to create an entity to insert

Customer customer = CustomerFixture.createSingleCustomer();

My CustomerFixture.createSingleCustomer() is fairly simple

public static Customer createSingleCustomer() {
Customer customer = new Customer();
customer.setUsername("testusername");
customer.setFirstName("Mick");
customer.setLastName("Knutson");
customer.setDescription("Customer with an Android Device.");
customer.setHobbies(
new HashSet<String>() {{
add("BASE-Jumping");
add("Skydiving");
add("Speed-Flying");
}}

);
customer.setPhones(createPhones());
customer.setAddresses(createAddresses());
return customer;
}

 

I was adding Address’s that looked like this

public static Map<String, Address> createAddresses() {
final int pk = getRandomInt(1, 999);
return new HashMap<String, Address>() {{
put(String.format("%03d", pk),
new Address(){{
setType(AddressType.RESIDENTIAL);
setStreet("123 Main Street");
setCity("San Francisco");
setState("CA");
setPostCode("91335");
setProvince("");
}}

);
}};

Notice that I created the Address with a default constructor…?

When running this, I get a very interesting exception:

javax.persistence.EntityExistsException:
Exception Description: No subclass matches this class [class com.baselogic.test.CustomerFixture$2$1] for this Aggregate mapping with inheritance.
Mapping: org.eclipse.persistence.mappings.AggregateCollectionMapping[addresses]
Descriptor: RelationalDescriptor(com.baselogic.chapter02.Customer --> [DatabaseTable(CUSTOMER)])

What I finally tracked this down to was from skimming the XXX codes on http://wiki.eclipse.org/EclipseLink_Exception_Error_Reference_%28ELUG%29

ECLIPSELINK-00068: The value of an aggregate in object [{0}] is null. Null values not allowed for Aggregate mappings unless “Allow Null” is specified.
Cause: The value of the aggregate in the source object object is null. Null values are not allowed for aggregate mappings unless allow null is specified in the aggregate mapping.
Action: Call the mapping method allowNull. Provide parameters only if you are making a distinction between foo() and foo(integer).

Specifically the ‘Action’.
So I started looking at my Fixture, and constructed my Address with a specific Constructor:

public static Map<String, Address> createAddresses(){
final int pk = getRandomInt(1, 999);
return new HashMap<String, Address>(){{
put(String.format("%03d", pk),
new Address(
AddressType.RESIDENTIAL,
"123 Main Street",
"San Francisco",
"CA",
"94114"
)
);
}}
;
}

This solved the issue, and the seeding of the Customer succeeded.

From http://www.baselogic.com/blog/development/java-javaee-j2ee/no-subclass-matches-this-class-for-this-aggregate-mapping-with-inheritance

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}