3

遅延ロードされたコレクションを持つエンティティ(連絡先)があります。これを変更する必要はありませんが、em.find(Contact.class、myID)を実行するときにコレクションをロードする必要があります。これは、エンティティを変更せずに、フェッチでjpqlステートメントを使用せずに実行できます。?

public class Contact implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id 
    @Column(name="contactId", nullable=false)
    public String contactId;    

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "contact", orphanRemoval = true, fetch=FetchType.LAZY)
    private List<ContactTaskRelation> taskRelations = new ArrayList<ContactTaskRelation>();

}

私の統計のない豆から

@PersistenceContext(unitName="myContext")
private EntityManager em;

@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
private Contact getContact(ContactMapping mappedContact){
    //force em to load the collection of taskRelations
    return em.find(Contact .class, mappedContact.getContact());
}
4

2 に答える 2

8
Contact entity = em.find(Contact.class, mappedContact.getContact());
entity.getTaskRelations().size(); // this will fetch the TaskRelations
return entity;

欠点は、これによりデータベースに対して2つのクエリが作成されることです。1つは連絡先を取得するためのもので、もう1つはTaskRelationsを取得するためのものです。

別のオプションは、次のようにクエリを作成することです。

String queryString = "SELECT model FORM Contact model JOIN FETCH model.taskRelations WHERE model.id = :id";
Query query = em.createQuery(queryString);
query.setParameter("id", mappedContact.getContact());
return query.getSingleResult(); // or getResultList();

このオプションは、クエリを1つだけ実行します。

于 2012-08-31T13:45:54.000 に答える
3

@arthuroのソリューションとリフレクションを組み合わせて、すべてのゲッターを呼び出すことができます。このようなもの:

public static <T> T findEager(EntityManager em, Class<T> type, Object id) {
    T entity = em.find(type, id);
    for (Field field: type.getDeclaredFields()) {
        OneToMany annotation = field.getAnnotation(OneToMany.class);
        if (annotation != null) {
            if (annotation.fetch().equals(FetchType.LAZY)) {
                try {
                    new PropertyDescriptor(field.getName(), type).getReadMethod().invoke(entity);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    return entity;
}

そして、次のように呼び出します。

Contact entity = findEager(em, Contact.class, mappedContact.getContact());
于 2012-08-31T14:26:56.757 に答える