在一个函数中使用多个查询并获得空会话异常

时间:2015-11-30 07:37:59

标签: java spring hibernate

我在一个函数中使用CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); tvCamera.setText(String.valueOf(cameraManager.getCameraCharacteristics())); (检查并保存数​​据)

multi query

}

并且更好地看到我的 Base Dao类,其他Dao类会暗示:

@Component
public class ApplicantManager {

@Autowired
private AccountDAO accountDAO;

@Autowired
private RoleDAO roleDAO;

public AccountModel signUpApplicant(PersonDTO personDTO) throws ExistException {
    if (accountDAO.isExistAccountByEmail(personDTO.getEmail())) {
        throw new ExistException(AccountEntity.class, personDTO.getEmail());
    }
    AccountEntity account = new AccountEntity(new PersonEntity(personDTO));
    account.setActive(false);
    account.addRole(roleDAO.getRoleApplicant());
    return new AccountModel(accountDAO.saveOrUpdate(account));
}

这是我的 AccountDao 类:

@Transactional(isolation = Isolation.READ_COMMITTED)
public abstract class DAOHibernateAbstract<T extends BaseEntity> implements DAO<T> {
protected final Class<T> clazz;

protected final Logger logger = LoggerFactory.getLogger(getClass());

@Autowired
private SessionFactory sessionFactory;

public DAOHibernateAbstract() {
    try {
        this.clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    } catch (Exception e) {
        logger.debug("can't determine class from generic type!");
        throw new IllegalArgumentException("can't determine class from generic type!");
    }
}

protected final Session getSession() {
    return sessionFactory.getCurrentSession();
}

@Override
public final T saveOrUpdate(T entity) {
    if (null == entity) {
        throw new IllegalArgumentException("null entity not acceptable");
    }
    Session session = this.getSession();
    session.saveOrUpdate(entity);
    return entity;
}

@Override
public final Collection<T> saveOrUpdate(Collection<T> entities) {
    if (null == entities) {
        throw new IllegalArgumentException("null entities not acceptable");
    } else if (!entities.isEmpty()) {
        entities.forEach(entity -> this.saveOrUpdate(entity));
    }
    return entities;
}


@Override
public final void delete(T entity) {
    if (null == entity) {
        throw new IllegalArgumentException("null entity not acceptable");
    }
    this.getSession().delete(entity);
}
@Override
public T get(Serializable identifier)
        throws NotExistException {
    if (null == identifier) {
        throw new IllegalArgumentException("null identifier not acceptable");
    }
    T result = this.getSession().get(clazz, identifier);
    if (null == result) {
        throw new NotExistException(clazz, identifier);
    }
    return result;
}

@Override
public List<T> get() {
    List<T> results = this.getSession()
            .createCriteria(clazz)
            .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
            .list();
    return null == results ? new ArrayList<>() : results;
}
}

但是当spring希望运行@Repository public class AccountDAOHibernateImpl extends DAOHibernateAbstract<AccountEntity> implements AccountDAO { @Override public boolean isExistAccountByEmail(String email) throws IllegalArgumentException { if (!StringUtils.hasText(email)) { throw new IllegalArgumentException("null email not acceptable"); } AccountEntity account = (AccountEntity) this.getSession() .createCriteria(AccountEntity.class, "account") .createAlias("account.person", "accountPerson") .add(Restrictions.eq("accountPerson.email", email)) .uniqueResult(); if (null == account) return false; else return true; } } saveOrUpdate函数的最后一行)时,我会看到signUpApplicant的{​​{1}} ....

NullPointerException

我的代码有什么问题?

0 个答案:

没有答案