Copygroup [message #902346] |
Fri, 17 August 2012 04:36  |
Eclipse User |
|
|
|
Hi,
I use Fetchgroup to retrieve partial data with entity manager, then I use Copygroup to make a copy of partially fetched entity. I got this exception:
IllegalStateException: Cannot get unfetched attribute [accessionAgents] from detached object Accession[ accessionId=7 ].
Please help me.
Thanks,
Ida
|
|
|
|
Re: Copygroup [message #902475 is a reply to message #902451] |
Fri, 17 August 2012 15:57   |
Eclipse User |
|
|
|
Hi Chris,
Thanks for quick replay. Here is the code, the Accession and Accessionagent are entity beans. It is one to many relationship. The fields are partial data need to fatch from Accession and the related entities, but the Accessionagent is not in fetch group. The exception occurs during the copy process.
@Entity
@Table(name = "accession")
@XmlRootElement
public class Accession extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "AccessionID")
private Integer accessionId;
@OneToMany(mappedBy = "accession", cascade= {CascadeType.PERSIST, CascadeType.MERGE}, orphanRemoval=true)
private Collection<Accessionagent> accessionAgents;
...
}
@Entity
@Table(name = "accessionagent")
@XmlRootElement
public class Accessionagent extends BaseEntity {
...
@JoinColumn(name = "AccessionID", referencedColumnName = "AccessionID")
@ManyToOne(cascade={CascadeType.MERGE, CascadeType.PERSIST})
private Accession accession;
...
}
public Accession getPartialData(List<String> fields, int id) {
Query query = entityManager.createNamedQuery("Accession.findByAccessionID");
query.setParameter("accessionID", id);
FetchGroup group = new FetchGroup();
CopyGroup cg = new CopyGroup();
cg.setDepth(3);
for(String field : fields) {
group.addAttribute(field);
cg.addAttribute(field);
}
query.setHint(QueryHints.FETCH_GROUP, group);
Accession accession = (Accession) query.getSingleResult();
JpaEntityManager nativeEM = entityManager.unwrap(JpaEntityManager.class);
Accession copyEntity = (Accession) nativeEM.copy(accession, cg);
return copyEntity;
}
Ida
|
|
|
|
Re: Copygroup [message #905091 is a reply to message #905022] |
Wed, 29 August 2012 13:03  |
Eclipse User |
|
|
|
Hi James,
I solved it with reflections. But I still think it will be nice if I can use copygroup. I will try again when I have time.
|
|
|
Powered by
FUDForum. Page generated in 0.30936 seconds