Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-users] Problem with not registered in Unit of Work objects

Hi, all! I implemented feature to store files in database. I have such entity and bean  for uploads:

@NamedQueries ({
    @NamedQuery (name = "findUploadById", query = "SELECT u FROM Upload u WHERE u.Id = :id"),
    @NamedQuery (name = "findUploadAll", query = "SELECT u FROM Upload u ORDER BY u.Id DESC")
public class Upload implements Serializable {

    private static final long serialVersionUID = 1L;

    @GeneratedValue (strategy = GenerationType.AUTO)
    private Long Id;

    private String FileName;

    private String MimeType;

    @Basic (fetch = FetchType.LAZY)
    private byte[] FileData;

    private Long FileSize;

public class UploadEJB {

    @PersistenceContext (unitName = "ru.fianitrock.ejb.pu")
    private EntityManager em;

    public Upload createUpload (Upload upload) {
        em.persist (upload);

        System.out.println ("persisted file: " + upload.getFileName () + ":" + upload.getId ());
        return upload;

    public void deleteUpload (Upload upload) {
        em.remove (em.merge (upload));

    public Upload updateUpload (Upload upload) {
        return em.merge (upload);

    public Upload findById (Long Id) {
        return em.find (Upload.class, Id);

    public List<Upload> findAll () {
        Query query = em.createNamedQuery ("findUploadAll");

        return query.getResultList ();

And I have servlet which handles such urls : /download/*
Here is the code of download servlet:

public class DownloadFile extends HttpServlet {

    private UploadEJB uploadEJB;

    protected void doGet (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String url = new String (request.getRequestURL ()).replaceFirst ("^.*\\/download\\/", "");

        String[] IdFileName = url.split ("\\/");
        if (IdFileName.length != 2)

        System.out.println ("id = :" + IdFileName [0] + "; filename = " + IdFileName [1]);

        try {
            Upload upload = uploadEJB.findById (new Long (IdFileName [0]));

            byte [] contents = upload.getFileData ();
            int len = contents.length;

            response.reset ();
            response.setContentType (upload.getMimeType ());
            response.getOutputStream ().write (contents, 0, len);
            response.getOutputStream ().flush ();
        } catch (Exception e) {
            e.printStackTrace ();

        response.getOutputStream ().close ();

     * Returns a short description of the servlet.
     * @return a String containing servlet description
    public String getServletInfo () {
        return "servlet returning file from database via enterprise bean";

When I upload files via primefaces fileUpload component and download them in the same http session everything goes well. But when someone opens another http session, first when he tries to get file he gets nothing, and my glassfish registers such exception:

[#|2010-05-01T18:05:16.854+0400|SEVERE|glassfishv3.0||_ThreadID=35;_ThreadName=Thread-1;|Local Exception Stack:
Exception [EclipseLink-6004] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.QueryException
Exception Description: The object [ru.fianitrock.entity.Upload@5f52b1], of class [class ru.fianitrock.entity.Upload], with identity hashcode (System.identityHashCode()) [6 247 089],
is not from this UnitOfWork object space, but the parent session's.  The object was never registered in this UnitOfWork,
but read from the parent session and related to an object registered in the UnitOfWork.  Ensure that you are correctly
registering your objects.  If you are still having problems, you can use the UnitOfWork.validateObjectSpace() method to
help debug where the error occurred.  For more information, see the manual or FAQ.
Query: ReadObjectQuery(referenceClass=Upload sql="SELECT ID, FILESIZE, FILEDATA, FILENAME, MIMETYPE FROM UPLOAD WHERE (ID = ?)")
        at org.eclipse.persistence.exceptions.QueryException.backupCloneIsOriginalFromParent(
        at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.getBackupClone(
        at org.eclipse.persistence.descriptors.FetchGroupManager.writePartialIntoClones(
        at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerExistingObject(
        at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerExistingObject(
        at org.eclipse.persistence.queries.ObjectBuildingQuery.registerIndividualResult(
        at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildWorkingCopyCloneNormally(
        at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObjectInUnitOfWork(
        at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(
        at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(
        at org.eclipse.persistence.queries.ObjectLevelReadQuery.buildObject(
        at org.eclipse.persistence.queries.ReadObjectQuery.registerResultInUnitOfWork(
        at org.eclipse.persistence.queries.ReadObjectQuery.executeObjectLevelReadQuery(
        at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(
        at org.eclipse.persistence.queries.DatabaseQuery.execute(
        at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(
        at org.eclipse.persistence.queries.ReadObjectQuery.execute(
        at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(
        at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(
        at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(
        at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(
        at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(
        at org.eclipse.persistence.jpa.JpaHelper.loadUnfetchedObject(
        at ru.fianitrock.entity.Upload._persistence_checkFetched(
        at ru.fianitrock.entity.Upload._persistence_getFileData(
        at ru.fianitrock.entity.Upload.getFileData(
        at ru.fianitrock.servlet.DownloadFile.doGet(

Then, when he presses refresh button browser offers  to download file. So file can be downloaded through two clicks on same link... This is very boring... Cud u tell me what's wrong with my bean, servlet or entity? Or what info I must provide to concretize problem?

I use Sun (or Oracle...) GlassFish 3 beta 74, EclipseLink - 2.0.0. (v20091127-r59).


Best regards, Alexander Makarenko


Back to the top