Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fails when a collection of entities is passed as a Supplier #14

Closed
arjanvlek opened this issue Mar 18, 2019 · 0 comments
Closed

Fails when a collection of entities is passed as a Supplier #14

arjanvlek opened this issue Mar 18, 2019 · 0 comments

Comments

@arjanvlek
Copy link
Contributor

The library fails with the following setup:

@Entity
public class Person extends AbstractEntity {

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(
            name = "person_pet",
            joinColumns = @JoinColumn(name = "person_id", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "pet_id", referencedColumnName = "id")
    )
    private List<Pet> pets;

public interface PersonBuildCommand extends AbstractBuildCommand<Person, PersonRepository> {

    @Override
    default Person findEntity(Person input) {
        return getRepository().findByFirstName(input.getFirstName());
    }

    PersonBuildCommand withPets(Supplier<List<Pet>> pets);
}

The following exception is thrown:

java.lang.ClassCastException: java.util.Collections$SingletonList cannot be cast to org.springframework.data.domain.Persistable

	at nl._42.heph.lazy.AbstractLazyEntity.resolve(AbstractLazyEntity.java:43)
	at java.util.ArrayList.forEach(ArrayList.java:1249)
	at nl._42.heph.DefaultBuildCommand.resolveReferences(DefaultBuildCommand.java:123)
	at nl._42.heph.DefaultBuildCommand.resolveBeforeCreateReferences(DefaultBuildCommand.java:113)
	at nl._42.heph.DefaultBuildCommand.performPreProcessing(DefaultBuildCommand.java:193)
	at nl._42.heph.DefaultBuildCommand.save(DefaultBuildCommand.java:258)
	at nl._42.heph.DefaultBuildCommand.constructOrSave(DefaultBuildCommand.java:277)
	at nl._42.heph.DefaultBuildCommand.create(DefaultBuildCommand.java:299)
	at nl._42.heph.DefaultBuildCommand$$EnhancerByCGLIB$$c0d05c85.CGLIB$create$3(<generated>)
	at nl._42.heph.DefaultBuildCommand$$EnhancerByCGLIB$$c0d05c85$$FastClassByCGLIB$$49ab7324.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
	at nl._42.heph.AbstractBuilder.lambda$instantiateBuildCommand$3(AbstractBuilder.java:256)
	at nl._42.heph.DefaultBuildCommand$$EnhancerByCGLIB$$c0d05c85.create(<generated>)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:206)
	at com.sun.proxy.$Proxy119.create(Unknown Source)
	at nl._42.hephdemo.person.PersonFixtures.robertBor_withBorisTheHamster(PersonFixtures.java:41)
	at nl._42.hephdemo.person.PersonServiceTest.findAll_shouldFindAllPersons_withTheirPets(PersonServiceTest.java:23)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
arjanvlek added a commit that referenced this issue Mar 18, 2019
If an entity has a collection type of nested entities (e.g. `OneToMany`
or `ManyToMany` mapping), you should be able to pass the set of nested
entities as a supplied collection to prevent unneccessary entity
creation in deeper tree structures.

However, the type of `AbstractLazyEntity` was bound to Persistable,
preventing passing of a `Collection` inside it.

This commit removes that restriction and now allows passing Collections
in a Supplier function. A test case has been written as well.

Fixes #14
arjanvlek added a commit that referenced this issue Mar 18, 2019
If an entity has a collection type of nested entities (e.g. `OneToMany`
or `ManyToMany` mapping), you should be able to pass the set of nested
entities as a supplied collection to prevent unneccessary entity
creation in deeper tree structures.

However, the type of `AbstractLazyEntity` was bound to Persistable,
preventing passing of a `Collection` inside it.

This commit removes that restriction and now allows passing Collections
in a Supplier function. A test case has been written as well.

Fixes #14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant