-->
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: problems with a one-to-many relation using annotation
PostPosted: Tue Jun 19, 2007 6:33 am 
Beginner
Beginner

Joined: Thu Aug 31, 2006 7:41 am
Posts: 24
Hi,

recently I changed from using .hbm.xml files to using annotations and now a particular association is giving me headaches when comitting transactions that store new objects in the database.

In particular this is a bidirectional One-To-Many association with the Many-Side being the owner. I'm trying to store the new objects of both classes within the same transactions and always get an error about violating the foreign key constraint of the many-table, because the object in the one-table doesn't exist.

The 2 tables involved are media and logentry and it seems that for some reason hibernate wants to persist logentry before media. The association is 1 media has multiple logentries. I already checked that when I add a new logentry to media I also set the media attribute of that logentry properly. And the Many-To-One annotation in logentry uses a CascadeType.ALL.

The 2 classes annotations are this:
Code:
@Entity
public class Media implements java.io.Serializable
{
    @Id @GeneratedValue
    private int id;
   
    @OneToMany( mappedBy="media" ) 
    private Set<LogEntry> logEntries = new HashSet<LogEntry>(0);
}

@Entity
@Table (name="log")
public class LogEntry
{
    @Id @GeneratedValue
    private int id;
   
    @ManyToOne( cascade = {CascadeType.ALL} )
    private Media media;
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 19, 2007 7:39 am 
Beginner
Beginner

Joined: Thu Aug 31, 2006 7:41 am
Posts: 24
Turns out the logging stuff wasn't the real problem. The real problem was that saveOrUpdate decided to do an SQL update instead of an insert, even though the instance didn't exist yet in the database.


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.