休眠 returns 一个错误的实体
Hibernate returns a wrong Entity
这里是场景:
我的第一个方法对实体 SContact.class 执行休眠查询。
使用第一种方法调用第二种方法来查询 SContactX.class。
每当触发第二个查询时,hibernate 都会抛出以下异常:
javax.persistence.PersistenceException: org.hibernate.WrongClassException: 对象 [id=1-1A5V6Z] 不是指定子 class [com.qvc.crm.esp.entity.siebel.SContactX] : 加载对象错误 class class com.qvc.crm.esp.entity.siebel.SContact
这里是第一种方法:
@Override
@Transactional(value = "transactionManagerAftReplicDb", readOnly = true)
public List<SContact> identifyCustomers(CompanyCode companyCode, CustomerSearchSpec customerSearchSpec) throws ApplicationException {
List<SContact> resultList = null;
QueryBuilder criteria = new QueryBuilder(SContact.class).equals(customerSearchSpec.getSearchFieldName(), customerSearchSpec.getSearchFieldValue())
.equals("buId", resultListBu.get(0).getRowId());
QueryConfig queryConfig = new QueryConfig(criteria, getEntityManager());
queryConfig.withEnd(customerSearchSpec.getCustomerLimit().intValue());
resultList = queryConfig.query(SContact.class); // executes a query agains SContact
map(resultList); // executes a query agains SContactX
return resultList;
}
@Override
public List<Customer> map(List<SContact> contacts) {
List<Customer> foundCustomer = new ArrayList<Customer>();
if (CollectionUtils.isNotEmpty(contacts)) {
for (SContact contact : contacts) {
SContactX contactExt = loadContextExt(contact);
...
}
}
return foundCustomer;
}
@Transactional(value = "transactionManagerAftReplicDb", readOnly = true)
private SContactX loadContextExt(SContact contact) {
TypedQuery<SContactX> query = getEntityManager().createQuery("FROM SContactX where rowId = :rowId", SContactX.class);
query.setParameter("rowId", contact.getRowId());
SContactX result = query.getSingleResult(); // thows exception!
return result;
}
联系人:
@Entity
@Table(name = "S_CONTACT")
@SuppressWarnings("serial")
public class SContact extends SiebelBaseEntity {
SCONTACTX:
@SuppressWarnings("serial")
@Entity
@Table(name = "S_CONTACT_X")
public class SContactX extends SiebelBaseEntity {
SIEBELBASEENTITY:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@SuppressWarnings("serial")
public abstract class SiebelBaseEntity extends BaseEntity {
基本实体。
public abstract class BaseEntity implements Serializable {
我通过将超级 class SiebelBaseEntity 的注释更改为:
解决了我的问题
@MappedSuperclass
@SuppressWarnings("serial")
public abstract class SiebelBaseEntity extends BaseEntity {
这里是场景:
我的第一个方法对实体 SContact.class 执行休眠查询。
使用第一种方法调用第二种方法来查询 SContactX.class。
每当触发第二个查询时,hibernate 都会抛出以下异常:
javax.persistence.PersistenceException: org.hibernate.WrongClassException: 对象 [id=1-1A5V6Z] 不是指定子 class [com.qvc.crm.esp.entity.siebel.SContactX] : 加载对象错误 class class com.qvc.crm.esp.entity.siebel.SContact
这里是第一种方法:
@Override
@Transactional(value = "transactionManagerAftReplicDb", readOnly = true)
public List<SContact> identifyCustomers(CompanyCode companyCode, CustomerSearchSpec customerSearchSpec) throws ApplicationException {
List<SContact> resultList = null;
QueryBuilder criteria = new QueryBuilder(SContact.class).equals(customerSearchSpec.getSearchFieldName(), customerSearchSpec.getSearchFieldValue())
.equals("buId", resultListBu.get(0).getRowId());
QueryConfig queryConfig = new QueryConfig(criteria, getEntityManager());
queryConfig.withEnd(customerSearchSpec.getCustomerLimit().intValue());
resultList = queryConfig.query(SContact.class); // executes a query agains SContact
map(resultList); // executes a query agains SContactX
return resultList;
}
@Override
public List<Customer> map(List<SContact> contacts) {
List<Customer> foundCustomer = new ArrayList<Customer>();
if (CollectionUtils.isNotEmpty(contacts)) {
for (SContact contact : contacts) {
SContactX contactExt = loadContextExt(contact);
...
}
}
return foundCustomer;
}
@Transactional(value = "transactionManagerAftReplicDb", readOnly = true)
private SContactX loadContextExt(SContact contact) {
TypedQuery<SContactX> query = getEntityManager().createQuery("FROM SContactX where rowId = :rowId", SContactX.class);
query.setParameter("rowId", contact.getRowId());
SContactX result = query.getSingleResult(); // thows exception!
return result;
}
联系人:
@Entity
@Table(name = "S_CONTACT")
@SuppressWarnings("serial")
public class SContact extends SiebelBaseEntity {
SCONTACTX:
@SuppressWarnings("serial")
@Entity
@Table(name = "S_CONTACT_X")
public class SContactX extends SiebelBaseEntity {
SIEBELBASEENTITY:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@SuppressWarnings("serial")
public abstract class SiebelBaseEntity extends BaseEntity {
基本实体。
public abstract class BaseEntity implements Serializable {
我通过将超级 class SiebelBaseEntity 的注释更改为:
解决了我的问题@MappedSuperclass
@SuppressWarnings("serial")
public abstract class SiebelBaseEntity extends BaseEntity {