【问题标题】:Eclipselink generated canonical metamodel doesn't extend base metamodel from another jarEclipselink 生成的规范元模型不会从另一个 jar 扩展基本元模型
【发布时间】:2015-01-07 19:12:10
【问题描述】:

我使用 Netbeans 8.0.1 创建了两个 Maven 项目来说明一个问题:common1 和 common2(jar)。

常见的1:

包pck1

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

包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>

常见的2

包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>

在构建过程之后,我在 Netbeans 中看到带有规范元模型的生成源:

常见的1

包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;

}

包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;

}

常见的2

包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;

}

问题是为什么 Entity3_ 不将 Entity1_ 扩展为 Entity2_?我做错了什么?

【问题讨论】:

    标签: java maven jpa netbeans eclipselink


    【解决方案1】:

    我遇到了同样的问题。这似乎是我使用的 Eclipse Link 2.5.0.v20130507 库中的一个错误。由于我在 JBoss 中使用休眠,因此我切换到休眠 4.3.8。元模型类是使用 hibernate-jpamodelgen 库正确生成的。

    【讨论】:

      【解决方案2】:

      没有错,只是EclipseLink注解处理器不够聪明。
      我看到你也在 EclipseLink 论坛上问过,没有答案......也许你应该提出一个问题(有人会接受吗?祝你好运!)。

      但是,我使用 EclipseLink 2.6.0 作为持久性提供程序,但我让 hibernate-jpamodelgen 生成元模型:

      <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>
      

      和以前一样,不需要 maven-processor-plugin,只需要依赖开关。

      尽管如此,我的个人建议是完全进入休眠状态(我将尽快这样做),因为社区更广泛、更活跃。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-06
        • 2015-09-20
        • 2016-01-11
        • 1970-01-01
        相关资源
        最近更新 更多