JPA 2.0:TYPE表达式异常 [英] JPA 2.0: TYPE expression exception

查看:123
本文介绍了JPA 2.0:TYPE表达式异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个继承结构与类,让我们说Parent(作为根类)和Child作为子类。

I have a inheritance structure with to classes, let's say Parent (as the root class) and Child as the subclass.

所以使用JPA 2.0没有我可以选择只使用父类

So with JPA 2.0 no I can select only the Parent class by using

SELECT p FROM Parent p WHERE TYPE(p) = Parent

这只应该返回Parent的条目,而不是孩子的条目。

This only should return entries of Parent and not also the entries of child.

但是我的EclipseLink 2.1.1和Glassfish v3上的MySql,我总是会收到以下错误:

But with my EclipseLink 2.1.1 and MySql on Glassfish v3, I always get the following error:

"Invalid Type Expression on [my.domain.Parent].  The class  
does not have a descriptor, or a descriptor that does not use  
inheritance or uses a ClassExctractor for inheritance". 

此外,我手动定义没有orm-mapping。我认为这都是在部署时自动完成的。

Additionally, I define no orm-mapping by hand. This is all done automatically on deployment, I think.

我是否需要添加到我的Parent / Child类(注释即)来声明继承结构? (但我认为这不应该是必要的,因为继承是由Java声明的,是吗?)

Is there something I have to add to my Parent /Child class (an annotation i.e.) to declare the inheritance structure? (But I think this shouldn't be necessary, because the inheritance is declared by Java, is it?)

编辑:

我没有提到的一个重要方面是我正在使用继承方法TABLE_PER_CLASS。


One important aspect I've didn't mentioned is that I'm using the inheritance method "TABLE_PER_CLASS".

推荐答案

忘记我之前说过的话。这适用于 SINGLE_TABLE 策略:

Forget what I said before. This will work for SINGLE_TABLE strategy:

@Entity
@Table(name="PERSON")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="GENDER", discriminatorType=DiscriminatorType.STRING, length=6)
public abstract class Person implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name="PERSON_PERSONID_GENERATOR", sequenceName="PERSON_ID_SEQ")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="PERSON_PERSONID_GENERATOR")
    @Column(name="PERSON_ID", updatable=false, unique=true, nullable=false, precision=22)
    private long personId;

    @Column(nullable=false, length=32)
    private String surname;

    @Column(name="GIVEN_NAME", nullable=false, length=32)
    private String givenName;

    // ...
}


@Entity
@DiscriminatorValue("FEMALE")
public class Daughter extends Person implements Serializable {

    @Column(name="NUMBER_OF_DOLLS", precision=22)
    private int numberOfDolls;

    // ...
}


@Entity
@DiscriminatorValue("MALE")
public class Son extends Person implements Serializable {

    @Column(name="NUMBER_OF_TOY_CARS", precision=22)
    private Integer numberOfToyCars;

    // ...
}


// JUnit test method
public void testInheritance() {
    EntityManager em = createNewEntityManagerInstance();

    EntityTransaction tx = em.getTransaction();
    tx.begin();
    Daughter d = new Daughter();
    d.setGivenName("Sue");
    d.setSurname("Smith");
    d.setNumberOfDolls(5);
    em.persist(d);
    Son s = new Son();
    s.setGivenName("Joe");
    s.setSurname("Smith");
    s.setNumberOfToyCars(8);
    em.persist(s);
    tx.commit();

    Query q;
    List<?> personList;
    Person p;

    q = em.createQuery("SELECT p FROM Person p WHERE TYPE(p) = Daughter");
    personList = q.getResultList();
    assertEquals(1, personList.size());
    p = (Person)personList.get(0);
    System.out.println(
        "This Daughter is: " + p.getGivenName() + " " + p.getSurname());
    q = em.createQuery("SELECT p FROM Person p WHERE TYPE(p) = Son");
    personList = q.getResultList();
    assertEquals(1, personList.size());
    p = (Person)personList.get(0);
    System.out.println(
        "This Son is: " + p.getGivenName() + " " + p.getSurname());
    q = em.createQuery("SELECT p FROM Person p");
    personList = q.getResultList();
    assertEquals(2, personList.size());
    for (Object o : personList) {
        assertTrue(o instanceof Person);
        p = (Person)o;
        System.out.println(
            "This person is: " + p.getGivenName() + " " + p.getSurname());
    }
    em.close();
}

数据库(我使用的是Oracle)DDL看起来像这样:

The database (I'm using Oracle) DDL looks like this:

CREATE TABLE "DEV"."PERSON"  
(  
"PERSON_ID" NUMBER NOT NULL ENABLE,   
"GIVEN_NAME" VARCHAR2(32 BYTE) NOT NULL ENABLE,   
"SURNAME" VARCHAR2(32 BYTE) NOT NULL ENABLE,   
"GENDER" VARCHAR2(6 BYTE) NOT NULL ENABLE,   
"NUMBER_OF_DOLLS" NUMBER,   
"NUMBER_OF_TOY_CARS" NUMBER,   
 CONSTRAINT "PERSON_PK" PRIMARY KEY ("PERSON_ID")  
);  

现在你说你正在尝试使用 TABLE_PER_CLASS 策略。我无法帮助你,因为 JPA 2.0规范说供应商不需要支持它。您的实现可能无法通过JPA接口正确支持它。

Now you said that you're trying to use TABLE_PER_CLASS strategy. I can't help you there, since the JPA 2.0 spec says that vendors are not required to support it. Your implementation may not support it properly via the JPA interfaces.

这篇关于JPA 2.0:TYPE表达式异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆