JPA -- Cannot instantiate abstract class exception
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?
Not tested this, but try the following in FundOperationItem
:
@DiscriminatorValue(value="0")
精彩评论