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:无法实例化对象标识为“ 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的问题吗?
这是一个已知问题吗?
最佳答案
尚未对此进行测试,但是请在FundOperationItem
中尝试以下操作:
@DiscriminatorValue(value="0")