-->
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.  [ 5 posts ] 
Author Message
 Post subject: Bidirectional @OneToMany relation
PostPosted: Thu Feb 25, 2010 11:16 am 
Newbie

Joined: Thu Feb 25, 2010 9:06 am
Posts: 2
Hi,

I got stuck for a while with a java.lang.StackOverflowError exception and I haven't found a solution yet, so I decided to write on forum about it. Maybe someone else run into this problem and manage to solve it.

I have two entities Invoice and Line. Line has a composite PK which contains Invoice PK's. Between Invoice and Line there is a one-to-many relation, with Line the owner side. I have mapped this relation bidirectional as following:

@Entity
@Table(name = "invoice")
public class Invoice implements Serializable
{
...
@Id
@Column(name = "id")
private int id;

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "id.invoice")
@JoinColumn(name = "id")
private List<Line> lines;
...
}


@Entity
@Table(name = "line")
public class Line implements Serializable
{
...
@EmbeddedId
private LineId id;
...
}


@Embeddable
public class LineId implements Serializable
{
...
@ManyToOne
@JoinColumn(name = "id")
private Invoice invoice;
...
}


Problem is that when I want to get all Line objects based on search criteria (I wrote a dao method that do this task), it gets into a infinite loop causing in the end a java.lang.StackOverflowError exception. I guess that for each Line in search results, for loaded Invoice it belongs to, are also loaded all Line that that Invoice has and so on.

Could someone give me some ideas how can I solve this? Thanks.

I am using hibernate 3.3.2 ga.


Top
 Profile  
 
 Post subject: Re: Bidirectional @OneToMany relation
PostPosted: Thu Feb 25, 2010 3:28 pm 
Beginner
Beginner

Joined: Tue Aug 25, 2009 11:42 am
Posts: 49
How about something like
Code:
@Entity
@Table(uniqueConstraints=@UniqueConstraint(columnNames = { "lineNumber", "invoice_id" }))
public class Line {
   @Id
   @GeneratedValue(strategy = GenerationType.SEQUENCE)
   private Long id;

   Long lineNumber;

   @ManyToOne(optional = false)
   Invoice invoice;
}


Top
 Profile  
 
 Post subject: Re: Bidirectional @OneToMany relation
PostPosted: Thu Feb 25, 2010 3:31 pm 
Beginner
Beginner

Joined: Tue Aug 25, 2009 11:42 am
Posts: 49
The cause of the infinite loop might be because to construct a LineId, we need an Invoice but to construct the Invoice we need Lines (eager fetch) which in turn need the LineIds.


Top
 Profile  
 
 Post subject: Re: Bidirectional @OneToMany relation
PostPosted: Fri Feb 26, 2010 8:17 am 
Newbie

Joined: Thu Feb 25, 2010 9:06 am
Posts: 2
I cannot give up in fetching lines eager as I use information from lines objects when I display invoice.

I am wondering if a bidirectional one to many relation will always produce an infinite loop if on both ends fetching mode is set to eager. I guess not, if I am able to set such a relation?!


Top
 Profile  
 
 Post subject: Re: Bidirectional @OneToMany relation
PostPosted: Fri Feb 26, 2010 11:06 am 
Beginner
Beginner

Joined: Tue Aug 25, 2009 11:42 am
Posts: 49
I think the fact that the related entity is itself the part of the id is what tripped Hibernate I believe. Did you consider the previous mapping I suggested - use of a surrogate key and setting uniqueness constraint on the natural id?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.