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]