Hibernate返回错误的实体

时间:2016-04-27 14:39:37

标签: hibernate jpa jta

这里的情景:

我的第一个方法在实体SContact.class上执行hibernate查询。

使用第一种方法调用第二种方法来查询SContactX.class。

每当第二个查询被触发时,hibernate都会抛出以下异常:

javax.persistence.PersistenceException:org.hibernate.WrongClassException:Object [id = 1-1A5V6Z]不是指定的子类[com.qvc.crm.esp.entity.siebel.SContactX] :加载的对象是类错误的类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;
    }

SCONTACT:

  @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 {

BASEENTITY。

public abstract class BaseEntity implements Serializable {

1 个答案:

答案 0 :(得分:1)

我通过将超类SiebelBaseEntity的注释更改为:

来解决了我的问题
@MappedSuperclass
@SuppressWarnings("serial")
public abstract class SiebelBaseEntity extends BaseEntity {