在odata与jpa中使用单向@OneToMany会导致异常

时间:2017-05-26 02:41:22

标签: jpa odata olingo

使用JPA Processor Extension时会出现问题。 如果使用单向@OneToMany,则元模型的创建将失败,并出现以下异常

No [EntityType] was found for the key class [java.util.Set] in the Metamodel - please verify that the [Entity] class was referenced in persistence.xml using a specific <class>java.util.Set</class> property or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element.

示例代码:

@Entity
@Table(name = "ALERT")
public class Alert implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 4223183779603724128L;
    @Id
    @Column(name = "CUSTOMER_ID")
    private int customerId;

    @Column(name = "PROVIDER")
    private String providerName;
    @Column(name = "ADMIN_MAIL_ID")
    private String adminMailId;
    @Column(name = "WARNING_LIMIT", precision = 16, scale = 3)
    private BigDecimal warningLimit;

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumns({ @JoinColumn(name = "CUSTOMER_ID", referencedColumnName = "CUSTOMER_ID"),
            @JoinColumn(name = "PROVIDER", referencedColumnName = "PROVIDER") })
    private Set<EntityMapping> entities;
  //getters and setters here

EntityMapping类代码:

@Entity
@Table(name = "ENTITY_MAPPING")
@IdClass(EntityMappingPK.class)
@Cacheable(false)
public class EntityMapping {

    @Id
    @Column(name = "CUSTOMER_ID")
    private int customerId;

    @Id
    @Column(name = "PROVIDER")
    private String provider;

    @Id
    @Column(name = "ENTITY")
    private String entity;

Persistence.xml:

   <?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence     http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="odataJPAService"
        transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>com.sample.jpa.model.Alert</class>
        <class>com.sample.jpa.model.EntityMapping</class>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="javax.persistence.jdbc.user" value="USR_DUV0WD7DRTHE85J199EC5ZRA1" />
            <property name="javax.persistence.jdbc.password" value="u5EMQnwOvtAxoiCQXwIIfmbqS4ICo_Fb" />
            <property name="javax.persistence.jdbc.url" value="jdbc:sap://localhost:30415/?currentschema=USR_DUV0WD7DRTHE85J199EC5ZRA1" />
            <property name="javax.persistence.jdbc.driver" value="com.sap.db.jdbc.Driver"/>

        </properties>
    </persistence-unit>
</persistence>

odataServiceFactory:

public class ServiceFactory extends ODataJPAServiceFactory {

    private static final String PERSISTENCE_UNIT_NAME = "odataJPAService";

    @Override
    public ODataJPAContext initializeODataJPAContext() throws ODataJPARuntimeException {
        ODataJPAContext oDataJPAContext = getODataJPAContext();
        EntityManagerFactory emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
        oDataJPAContext.setEntityManagerFactory(emf);
        oDataJPAContext.setPersistenceUnitName(PERSISTENCE_UNIT_NAME);
        oDataJPAContext.setJPAEdmExtension(new CustomAnnotationProcessor());
        return oDataJPAContext;
    }

根据此链接,此问题已得到解决: https://issues.apache.org/jira/browse/OLINGO-526

0 个答案:

没有答案