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”类型的抽象类“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
未对此进行测试,但请在
FundOperationItem
中尝试以下操作:Not tested this, but try the following in
FundOperationItem
: