Eclipselink Entity is not recognized

2019-07-04 06:59发布

问题:

I have Versions which may rely on other Version. The dependent Versions are stored in the List dependsOn. Because I need to be able to determine, in what other Versions a specific Version is used I have the List usedIn which is the inverse of dependsOn.

My Problem is as soon as I add methods to add/remove a single Version from dependsOn EclipseLink does not recognize my class Version as entity anymore.

My Version class:

@Entity
@Table( name = "tbl_version" )
@NamedQueries( /*...*/ )
public class Version extends BaseEntity
{
    @ManyToMany
    @JoinTable( name = "tbl_depends_on",
        joinColumns = { @JoinColumn( name = "PK_VERSION" ) },
        inverseJoinColumns = { @JoinColumn( name = "PK_DEPENDENCY" ) }
    )
    private List<Version> dependsOn;

    @ManyToMany( mappedBy = "dependsOn" )
    private List<Version> usedIn;

    public void addDependency( Version dependency )
    {
        if( dependency == null )
        {
            return;
        }
        dependsOn.forEach( ( Version version) -> 
        {
            if( dependency.equals( version ) )
            {
                return;
            }
        } );
        dependsOn.add( dependency );
    }

    public void removeDependency( Version dependency )
    {
        if( dependency == null )
        {
            return;
        }
        dependsOn.forEach( ( Version version ) ->
        {
            if( dependency.equals( version ) )
            {
                dependsOn.remove( version );
                return;
            }
        } );
    }
}

As soon as I delete addDependency and removeDependency EclipseLink works perfect again. Also it is not the field usedIn, I already tried to delete it. I am using EclipseLink 2.5 as persistence api.
Has anyone an idea what I am doing wrong?

Update
The following exception is thrown

java.lang.IllegalArgumentException: Object: Version [id=null, 

name=1.2.5] is not a known entity type.
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4222)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496)
    at persistence.VersionTest.setUp(VersionTest.java:71)
    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:483)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Line 71 is part of a the setUp in a JUnit test:

@Before
public void setUp() throws Exception
{
    Version version1 = new Version();
    version1.setName( versionName1 );

    Version version2 = new Version();
    version2.setName( versionName2 );

    EntityTransaction transaction = manager.getTransaction();
    try
    {
        transaction.begin();
        manager.persist( lVersion1 ); //line 71
        //...

回答1:

If you want to use Java 8 lambdas in your entity code, you should have an up-to-date version of EclipseLink that is Java 8 bytecode compatible or you will likely run into conflicts. Use EclipseLink version 2.6.0 or higher.

The source of the problem can be read in this bug report, which was resolved in 2.6.0:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=429992