JPA - 无法实例化抽象类异常

时间:2011-07-10 16:27:20

标签: java java-ee jpa

FundOperationItem.java

@Entity
@Table(name = "OPERATION_ITEMS")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "D_TYPE", discriminatorType = DiscriminatorType.INTEGER)
public abstract class FundOperationItem implements Serializable {

   @ManyToOne(fetch = FetchType.LAZY, optional=false)
   @JoinColumn(name = "PARENT_OPERATION_ID", nullable=false)
   private FundOperation operation;

   public FundOperation getOperation() {
      return this.operation;
   }

   public void setOperation(final FundOperation operation) {
      this.operation = operation;
   }

}

ExchangeOperationItem.java

@Entity
@Table(name = "EXCHANGE_OPERATION_ITEMS")
@DiscriminatorValue(value="2")
public class ExchangeOperationItem extends FundOperationItem {

}

SimpleOperationItem.java

@Entity
@Table(name = "SIMPLE_OPERATION_ITEMS")
@DiscriminatorValue(value="1")
public class SimpleOperationItem extends FundOperationItem {

}

FundOperation.java

@Entity
@Table(name = "OPERATIONS")
public class FundOperation implements java.io.Serializable{

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "operation", fetch = FetchType.LAZY)
    private List<FundOperationItem> operationItems = new ArrayList<FundOperationItem>();

    public List<FundOperationItem> getOperationItems() {
        return this.operationItems;
    }

    public void setOperationItems(final List<FundOperationItem> operationItems) {
        this.operationItems = operationItems;
    }

}

以这种方式使用它:

@Test
public void test(){
    FundOperation oper = operationRepository.findById(1L);
    System.out.println(oper.getOperationItems().size());
}

有这样的例外:

org.apache.openjpa.persistence.ArgumentException:无法使用对象ID“rba.pm.persistency.operation.FundOperationItem-1”实例化类型为“rba.pm.persistency.operation.FundOperationItem”的抽象类;这可能表示该类的继承鉴别符未正确配置。

数据库内容:

Insert into OPERATIONS (OPERATION_ID) values (1);
Insert into OPERATIONS (OPERATION_ID) values (2);
Insert into OPERATIONS (OPERATION_ID) values (3);

Insert into OPERATION_ITEMS (OPERATION_ITEM_ID,PARENT_OPERATION_ID,D_TYPE) values (1,1,1);
Insert into OPERATION_ITEMS (OPERATION_ITEM_ID,PARENT_OPERATION_ID,D_TYPE) values (2,2,1);
Insert into OPERATION_ITEMS (OPERATION_ITEM_ID,PARENT_OPERATION_ID,D_TYPE) values (3,3,1);

Insert into SIMPLE_OPERATION_ITEMS (OPERATION_ITEM_ID) values (1);
Insert into SIMPLE_OPERATION_ITEMS (OPERATION_ITEM_ID) values (2);
Insert into SIMPLE_OPERATION_ITEMS (OPERATION_ITEM_ID) values (3);

我做错了吗?

更新

**有一个解决方案,如果添加到新行上方的测试

    SimpleOperationItem sio = new SimpleOperationItem();

它有效

    @Test
    public void test(){
        SimpleOperationItem sio = new SimpleOperationItem();
        FundOperation oper = operationRepository.findById(1L);
        System.out.println(oper.getOperationItems().size());
    }

注意:对象'sio'与'oper'没有任何关系。

任何想法,发生了什么? 这是classloader的问题吗? 这是一个已知的问题吗?

1 个答案:

答案 0 :(得分:0)

未对此进行测试,但请在FundOperationItem中尝试以下内容:

@DiscriminatorValue(value="0")