我正在尝试使用 JPA 注释在 Hibernate 中使用 JOINED 继承策略。我的基类是抽象的,并且与其他实体具有多对一关联。但是我得到了休眠异常:无法实例化抽象类或接口。以下是我的班级结构:
账户.java
@Entity(name="BANK_ACCOUNTS")
@Inheritance(strategy=InheritanceType.JOINED)
public abstract class Account {
@Id
@Column(name="ACC_ID")
@SequenceGenerator(name="account_id_seq", sequenceName="BANK_ACCOUNT_ACC_ID", allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="account_id_seq")
private Long id;
@Column(name="IS_ACTIVE")
@Type(type="yes_no")
private Boolean isActive;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="ACC_BRANCH_ID")
private Branch branch;
}
SavingsAccount.java
@Entity(name="BANK_SAVINGS_ACCOUNT")
public class SavingsAccount extends Account{
@Column(name="CURR_BAL")
private BigDecimal currentBalance;
}
分支类
@Entity(name="BANK_BRANCH")
public class Branch {
@Id
@Column(name="BRANCH_ID")
@SequenceGenerator(name="branch_id_seq", sequenceName="BANK_BRANCH_BRANCH_ID", allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="branch_id_seq")
private Long id;
@OneToMany(mappedBy="branch",orphanRemoval=true, fetch= FetchType.LAZY)
@Cascade(value=org.hibernate.annotations.CascadeType.ALL)
private Set<Account> accounts = new HashSet<Account>();
}
因此,每当我尝试使用以下代码获取分支机构持有的帐户列表时,都会出现以下异常:
Branch branch = (Branch) session.get(Branch.class, branchId);
SavingsAccount acc = new SavingsAccount();
acc.setBranch(branch);
branch.getAccounts().add(acc);
异常跟踪:
org.hibernate.InstantiationException: Cannot instantiate abstract class or interface: : com.sapient.bank.entities.Account
at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:114)
at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:136)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.instantiate(AbstractEntityTuplizer.java:737)
at org.hibernate.persister.entity.AbstractEntityPersister.instantiate(AbstractEntityPersister.java:4761)
at org.hibernate.internal.SessionImpl.instantiate(SessionImpl.java:1427)
at org.hibernate.internal.SessionImpl.instantiate(SessionImpl.java:1415)
at org.hibernate.loader.plan.exec.process.internal.EntityReferenceInitializerImpl.hydrateEntityState(EntityReferenceInitializerImpl.java:235)
at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.readRow(AbstractRowReader.java:107)
at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:129)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:138)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:102)
at org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitializer.java:100)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:693)
at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:92)
at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1933)
at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:559)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:261)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:555)
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:143)
at org.hibernate.collection.internal.AbstractPersistentCollection$3.doWork(AbstractPersistentCollection.java:336)
at org.hibernate.collection.internal.AbstractPersistentCollection$3.doWork(AbstractPersistentCollection.java:324)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:261)
at org.hibernate.collection.internal.AbstractPersistentCollection.readElementExistence(AbstractPersistentCollection.java:323)
at org.hibernate.collection.internal.PersistentSet.add(PersistentSet.java:200)
at com.sapient.bank.dao.AccountDao.createNewSavingsAcc(AccountDao.java:28)
at com.sapient.util.MainClass.main(MainClass.java:32)
请帮助我了解我的代码有什么问题。如果我删除 abstract 关键字,它工作正常,但我想将 Account 类设为抽象。有什么办法可以做到这一点?