-->
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.  [ 7 posts ] 
Author Message
 Post subject: Cascade operations don't act through a CollectionOfElements
PostPosted: Fri Jun 20, 2008 12:58 pm 
Newbie

Joined: Fri Sep 14, 2007 2:02 pm
Posts: 10
Location: New York, NY
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
Hibernate 3.2.5, Annotations 3.3.0
Mapping documents:
(using annotations)
Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:
Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.rtrms.application.view.filter.FilterImpl
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:219)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:397)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:87)
at org.hibernate.type.ComponentType.nullSafeSet(ComponentType.java:307)
at org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:755)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1143)
at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:26)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:145)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.rtrms.persistence.hibernate.HibernateTransaction.commit(HibernateTransaction.java:123)
... 17 more


Name and version of the database you are using:
HSQL

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


The problem I have involves three classes, a top level entity called View, an embeddable object called Column, and another entity called Filter. The View contains a CollectionOfElements of Columns, and each Column has a ManyToOne reference to a Filter:

@javax.persistence.Entity()
@Entity(dynamicUpdate=true)
@Table(name="PORTFOLIO_VIEW")
class View {
....
@JoinTable(name="PORTFOLIO_VIEW_COLUMN",joinColumns={@JoinColumn(name="VIEW_ID")})
@IndexColumn(name="COLUMN_INDEX")
@Cascade(CascadeType.ALL)
@LazyCollection(LazyCollectionOption.FALSE)
@CollectionOfElements
private List<Column> columns;
....
}

@Embeddable
class Column {
....
@ManyToOne(fetch=FetchType.EAGER)
@Cascade(CascadeType.ALL)
@JoinColumn(name="FILTER")
@Target(FilterImpl.class)
private Filter filter;
....
}

@javax.persistence.Entity
@Entity(dynamicUpdate=true)
@Table(name="FILTER")
@DiscriminatorColumn(name="CLASS_NAME",length=255)
abstract class FilterImpl {
....
}

When I call save on a View which has a Column which has a Filter, I'd like that Filter to get saved (thus the CascadeType.ALL with the CollectionOfElements). It doesn't, and I get the TransientObjectException.

I'm not sure if there's an annotation that I'm missing, or whether Hibernate doesn't support this type of operation. I'm aware that I could remodel this so that Column is also an Entity, but I feel like that shouldn't be necessary. I should be able to tell components/embeddables to cascade to referenced entities.

Can anyone point me in the right direction?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 22, 2008 3:12 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

Have you tried to add @Inheritance to FilterImpl? Not sure if this makes a difference though. It just seems to be appropriate since it seems to be a base class.

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 22, 2008 5:30 pm 
Newbie

Joined: Fri Sep 14, 2007 2:02 pm
Posts: 10
Location: New York, NY
My understanding is that the you only need @Inheritance if you want to use a different inheritance strategy than the default, which is TablePerHierarchy. And there are other instances of places with a more straightforward @ManyToOne relationship to Filter whose cascade settings work fine... It's only problematic through the CollectionOfElements used for the Column collection.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 23, 2008 4:52 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

you are right. It should work. In fact there is a test case with a similar scenario in the source code. However, it does not use cascading persist.

Maybe you could create a Jira issue for this?

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 23, 2008 8:05 am 
Newbie

Joined: Fri Sep 14, 2007 2:02 pm
Posts: 10
Location: New York, NY
Logged at http://opensource.atlassian.com/project ... se/ANN-755

I'm still curious whether this is just a missing annotation implementation, or something that needs to be done in Hibernate core. Any idea?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 27, 2008 1:02 pm 
Newbie

Joined: Fri Jun 27, 2008 12:44 pm
Posts: 4
In my opinion,it's annotation implementation

_________________
MSN/Email: electronics-mac-sony@hotmail.com
http://www.electronics-mac-sony.com/


Top
 Profile  
 
 Post subject: More trouble with @CollectionOfElements
PostPosted: Sun Sep 28, 2008 11:24 am 
Newbie

Joined: Wed Feb 20, 2008 7:16 am
Posts: 3
Hello,
thanx for the JIRA entry. It helped me understand that I am not the only one with the problem. After digging into the Hibernate sources I had to give up, though.
Background:

I have a Document which holds a Map of Terms and their corresponding TF/IDF values. When persisting the Document I want the new Terms to be persisted, too.

@Entity
public class Document
{
@Id
@GeneratedValue
private Long id;

@CollectionOfElements
@MapKeyManyToMany
@Cascade(CascadeType.ALL)
private Map<Term, Integer> termFrequencies = new Hashtable<Term, Integer>();

...
}

@Entity
@Table(uniqueConstraints = @UniqueConstraint(columnNames =
{ "term", "language_id" }))
public class Term
{
@Id
@GeneratedValue
private Long id;

@ManyToOne
@Cascade(CascadeType.PERSIST)
private Language language;

private String term;

@ManyToMany(mappedBy = "terms", fetch = FetchType.LAZY)
@Cascade(CascadeType.PERSIST)
private Set<Topic> topics = new HashSet<Topic>();

....
}

This does not work, due to the reported problem. I cannot use the embedded tag for the term since they are entities in their own right and have other associations.

I feel the only way to work around the problem is to first persist all Terms and only afterwards the Document. This would mean that I can persist a Document only through an intelligent DAO. I would prefer an "integrated" solution, though.

I tracked the source of the problem down to the AnnotationBinder, where the cascade annotation of an CollectionOfElement is simply not handed over to the corresponding collection handling. Surprisingly - the code is there, but commented out. Therefore, I uncommentd (and fixed) the line. But to no avail. The reason behind is the handling of maps in the Cascade class. The offending line (inside cascadeCollection)
if ( elemType.isEntityType() || elemType.isAnyType() || elemType.isComponentType() )
The elemType in my example is an "IntegerType" which does not fulfil the condition and therefore does not reach the actual persist action.
The reason why the elemType is set like this remains unclear to me, since basically it should be a MapEntry (I checked the other CollectionTypes and the corresponding iterator-producing functions).

Does anyone care about these insights? Does anyone know a solution? I am ready and willing to get into the source some more, so I am happy about any pointers.
Thank you!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.