Skip to main content



      Home
Home » Eclipse Projects » EclipseLink » Copygroup(IllegalStateException: Cannot get unfetched attribute [object] from detached object)
Copygroup [message #902346] Fri, 17 August 2012 04:36 Go to next message
Eclipse UserFriend
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 #902451 is a reply to message #902346] Fri, 17 August 2012 12:02 Go to previous messageGo to next message
Eclipse UserFriend
More information is needed, but as the exception states, it is thrown when accessing an unfetched attribute on a detached object. Are you accessing something from your clone, or is this occuring within the clone process itself? If this is within the clone, it means that the copy group includes the accessionAgents field while the fetch group did not, and the entity is detached so that the session is null or unavailable to use to fetch it.

Regards,
Chris

[Updated on: Fri, 17 August 2012 12:05] by Moderator

Re: Copygroup [message #902475 is a reply to message #902451] Fri, 17 August 2012 15:57 Go to previous messageGo to next message
Eclipse UserFriend
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 #905022 is a reply to message #902475] Wed, 29 August 2012 11:07 Go to previous messageGo to next message
Eclipse UserFriend
Can you include the full stack to the exception.

Try a simpler test to see if CopyGroup is working as you expect it.
Note that you should not be setting cg.setDepth(3), but this should not matter as the addAttribute calls setDepth(CASCADE_TREE)
Re: Copygroup [message #905091 is a reply to message #905022] Wed, 29 August 2012 13:03 Go to previous message
Eclipse UserFriend
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.
Previous Topic:setTableQualifier(schema) without transaction
Next Topic:preventing on jpa exception setRollbackOnly
Goto Forum:
  


Current Time: Tue Jul 08 03:29:41 EDT 2025

Powered by FUDForum. Page generated in 0.30936 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top