-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Problem with updating collection in a Transaction
PostPosted: Tue Aug 08, 2006 9:49 am 
Newbie

Joined: Tue Aug 08, 2006 9:23 am
Posts: 2
Hibernate version: 3.1.2

Hallo:)
When my entity is saveOrUpdate() it 's collection objects are not saveOrUpdate().
here is a parent class:
Code:
@Entity
@Table(name = "documents")
@Inheritance(strategy = InheritanceType.JOINED)
public class Document extends BaseBO {
    private Set<Binary> resources;
    ....

    /**
     * removes the binary from the document if it is part of it
     * @param binary the binary to be removed from the document
     */
    protected void removeResource(Binary binary){
        resources.remove(binary);
        binary.setParent(null);
    }

    /**
     * adds a resource to the document
     * @param binary the binary to be added to the document
     */
    protected void addResource(Binary binary){
        if ( getResources() == null )
            resources = new HashSet<Binary>(5);
        resources.add(binary);
        binary.setParent(this);
    }

    .....

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "parent", targetEntity = Binary.class)
    public Set<Binary> getResources() {
        return resources;
    }

    public void setResources(Set<Binary> resources) {
        this.resources = resources;
    }

}


here is child class:
Code:
@Entity
@Table(name = "blobs")
@Inheritance(strategy = InheritanceType.JOINED)
public class Binary extends BaseBO {
    private Timestamp lastUpdate;
    ....
    private Document parent;
    .....

    @Column (nullable = false)
    public Timestamp getLastUpdate() {
        return lastUpdate;
    }

    protected void setLastUpdate(Timestamp lastUpdate) {
        this.lastUpdate = lastUpdate;
    }

    @ManyToOne
    @JoinColumn(name = "parent", nullable = false)
    public Document getParent() {
        return parent;
    }

    public void setParent(Document parent) {
        this.parent = parent;
    }

    /**
     * checks for the update of the default value, only one bean can be default one
     *
     * @param session the session for data modify
     * @return true if the process should fail
     * @throws org.hibernate.CallbackException
     *
     */
    @Override
    public boolean onSave(Session session) throws CallbackException {
        setLastUpdate(new Timestamp(System.currentTimeMillis()));
        return super.onSave(session);
    }

    /**
     * checks for the update of the default value, only one bean can be default one
     *
     * @param session the session for data modify
     * @return true if the process should fail
     * @throws org.hibernate.CallbackException
     *
     */
    @Override
    public boolean onUpdate(Session session) throws CallbackException {
        setLastUpdate(new Timestamp(System.currentTimeMillis()));
        return super.onUpdate(session);
    }
}


here is code that makes update:
Code:
public void storeFile(String userId, String docAliasId, String fileAliasId, String fileName, String contentType,
                          InputStream inputStream)
            throws PersistenceException, IllegalArgumentException, IOException {
        Transaction tx = newTransaction();
      ....
            Document document = findByAlias(userId, docAliasId);
            if (document == null) {
                //now creating the file
                FileResource fileResource = new FileResource();
                document = initDocument(docAlias, registrator, fileResource, fileName, contentType, binaryAlias, inputStream);
                persist(document);
            } else {
       ....
                FileResource fileResource = (FileResource) query.uniqueResult();[b]//this class extends Binery[/b]
                if (fileResource == null) {
                    fileResource = new FileResource(); //now creating the file
                    fileResource.setId(UUID.randomUUID().toString());
                }
                fileResource.setFileName(fileName);
                fileResource.setContentType(contentType);
                fileResource.setAlias(binaryAlias);
                fileResource.setData(IOUtils.toByteArray(inputStream));
                document.addResource(fileResource);
                persist(document);
            }
            commit(tx);
        } catch (HibernateException e) {
            rollback(tx);
            throw new PersistenceException("can not store the document", e);
        } catch (IOException e) {
            rollback(tx);
            throw e;
        }
    }


it seems that methos onSave and onUpdate from Binery are not invoked and lastUpdate property is not updated.

Has any one explanation of this problem?

Best regards,
Ivan[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 09, 2006 7:18 am 
Newbie

Joined: Tue Aug 08, 2006 9:23 am
Posts: 2
Sorry for unclean explanation of my problem.

Here is more properly explanation:
When saveOrUpdate() is called upon an Entity, a collection in it is updated but it seems that methods onSave and onUpdate are not called for collection-value entities where one or more of them have a change in contentent.

I hope this is more clear then my privious post.
If any one has idea what may be wrong тхе help will be appreciated

Thanx again,

Ivan


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.