Eclipselink generated canonical metamodel doesn

2019-07-21 19:05发布

问题:

I created two Maven projects using Netbeans 8.0.1 to illustrate a problem: common1 and common2 (jars).

common1:

package pck1

@MappedSuperclass
public class Entity1 implements Serializable {
    @Basic(optional = false)
    @Column(name = "val")
    protected String val;
}

package pck2

@Entity
@Table(name = "entity2")
public class Entity2 extends Entity1 {
    @Id
    private Long id;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
}

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="PU-1" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
  </persistence-unit>
</persistence>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.ent</groupId>
    <artifactId>common1</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.5.2</version>            
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
            <version>2.5.2</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>    
</project>

common2

package pck3

@Entity
@Table(name = "entity3")
public class Entity3 extends Entity1 {
    @Id
    private Long id;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
}

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="PU-2" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
    </persistence-unit>
</persistence>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.ent</groupId>
    <artifactId>common2</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.ent</groupId>
            <artifactId>common1</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.5.2</version>            
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
            <version>2.5.2</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>    
</project>

After build process I see generated sources with canonical metamodels in Netbeans:

common1

package pck1

@Generated(value="EclipseLink-2.5.2.v20140319-rNA", date="2014-11-11T13:39:25")
@StaticMetamodel(Entity1.class)
public class Entity1_ { 

    public static volatile SingularAttribute<Entity1, String> val;

}

package pck2

@Generated(value="EclipseLink-2.5.2.v20140319-rNA", date="2014-11-11T13:39:25")
@StaticMetamodel(Entity2.class)
public class Entity2_ extends Entity1_ {

    public static volatile SingularAttribute<Entity2, Long> id;

}

common2

package pck3

@Generated(value="EclipseLink-2.5.2.v20140319-rNA", date="2014-11-11T13:39:31")
@StaticMetamodel(Entity3.class)
public class Entity3_ { 

    public static volatile SingularAttribute<Entity3, Long> id;

}

The question is why Entity3_ not extends Entity1_ as Entity2_? What am I doing wrong?

回答1:

I had this same problem. This appears to be a bug in the Eclipse Link 2.5.0.v20130507 libraries I was using. Since I'm using hibernate with JBoss I switched to Hibernate 4.3.8. The metamodel classes are generated correctly using hibernate-jpamodelgen library.



回答2:

Nothing wrong, it's just EclipseLink annotation processor is not clever enough.
I see you asked on EclipseLink forum too, with no answer... Maybe you should file an issue (will someone ever take it? good luck!).

However, I'm using EclipseLink 2.6.0 as persistence provider, but I let hibernate-jpamodelgen generate the metamodel instead:

<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>org.eclipse.persistence.jpa</artifactId>
    <version>${eclipselink.version}</version>
    <scope>provided</scope>
</dependency>
<!-- <dependency> -->
<!-- <groupId>org.eclipse.persistence</groupId> -->
<!-- <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId> -->
<!-- <version>${eclipselink.version}</version> -->
<!-- <scope>provided</scope> -->
<!-- </dependency> -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-jpamodelgen</artifactId>
    <version>4.3.10.Final</version>
    <scope>provided</scope>
</dependency>

like before, no maven-processor-plugin is needed, just the dependency switch.

Nevertheless, my personal advice is to move to hibernate completely (that's what I'll do ASAP) because of a wider and more present community.