-3

我在使用 Hibernate 延迟加载时遇到了一些问题。请参阅下面我的实体:

@Entity
@Table(name = "diagnoses")
public class Diagnosis extends Domain implements IDiagnosis {

    @Column(name = "short_name")
    private String shortName;

    @Column(name = "full_name")
    private String fullName;

    @Column(name = "code")
    private int code;

    @OneToMany(fetch = FetchType.LAZY, targetEntity = Anamnesis.class, cascade = CascadeType.ALL,
        orphanRemoval = true)
    @JoinColumn(name = "diagnoses_id", nullable = false)
    private Set<IAnamnesis> anamneses = new HashSet<>();

    @OneToMany(fetch = FetchType.LAZY, targetEntity = Complaint.class, cascade = CascadeType.ALL,
        orphanRemoval = true)
    @JoinColumn(name = "diagnoses_id", nullable = false)
    private Set<IComplaint> complaints = new HashSet<>();
        ...
}

但是当我在测试 findAll() 或 findById() 方法中调用时,休眠初始化集合......

@Service("diagnosisService")
public class DiagnosisService implements IDiagnosisService {

    @Autowired
    private IDiagnosisRepository diagnosisRepository;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public IDiagnosis getById(String id) {
        return diagnosisRepository.findById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public boolean saveOrUpdate(IDiagnosis diagnosis) {
        boolean result = false;
        if (diagnosis != null) {
            if (StringUtils.isEmpty(diagnosis.getId())) {
                diagnosisRepository.insert(diagnosis);
                result = true;
            } else {
                diagnosisRepository.update(diagnosis);
                result = true;
            }
        }
        return result;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public boolean delete(IDiagnosis diagnosis) {
        boolean deleted = false;
        if (diagnosis != null) {
            diagnosisRepository.delete(diagnosis);
            deleted = true;
        }
        return deleted;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public List<IDiagnosis> getAll() {
        return diagnosisRepository.findAll();
    }



public class DiagnosisRepository{
... 
    public T findById(ID id) {
        T t = null;
        List<T> data = sessionFactory.getCurrentSession().createQuery(String.format("from %s where id='%s'",
            getClassName(), id)).list();
        if (CollectionUtils.isNotEmpty(data)) {
            t = data.get(FIRST_ENTITY);
        }
        return t;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<T> findAll() {
        return sessionFactory.getCurrentSession().createQuery(String.format("from %s", getClassName())).list();
    }
... 
}   

我使用 Hibernate 4。为什么会这样?也许它有额外的设置?

4

1 回答 1

1

为什么你认为它被初始化了?在调试时,您是否看到集合中的已初始化字段设置为 true?您是否尝试过在事务之外访问集合。它应该通过一个 LazyInitializationException。

顺便说一句, Onetomany映射默认是惰性的。您无需明确提及。

于 2012-12-13T04:34:05.947 回答