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

发布于 2024-11-19 11:29:47 字数 3258 浏览 1 评论 0原文

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”类型的抽象类“rba.pm.persistency.operation.FundOperationItem-1”;这可能表明该类的继承鉴别器配置不正确。

DB内容:

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”没有任何关系。

知道吗,发生了什么事? 这是类加载器的问题吗? 这是一个已知问题吗?

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

}

Using this in such manner:

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

Got such exception:

org.apache.openjpa.persistence.ArgumentException: Cannot instantiate abstract class of type "rba.pm.persistency.operation.FundOperationItem" with object id "rba.pm.persistency.operation.FundOperationItem-1"; this may indicate that the inheritance discriminator for the class is not configured correctly.

DB content:

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

Have I made something wrong?

Update:

**There is an solution, if add to the test above a new line

    SimpleOperationItem sio = new SimpleOperationItem();

it works

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

Note: Object 'sio' does not have any relation to 'oper'.

Any idea, what is going on?
Is this a problem with classloader?
Is this a known problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你如我软肋 2024-11-26 11:29:47

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

@DiscriminatorValue(value="0")

Not tested this, but try the following in FundOperationItem:

@DiscriminatorValue(value="0")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文