-->
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.  [ 9 posts ] 
Author Message
 Post subject: one to many persist question
PostPosted: Fri Jan 30, 2009 7:05 am 
Newbie

Joined: Fri Jan 30, 2009 6:31 am
Posts: 13
Location: Bucharest/Romania
Hi All,

I have a classic one to many persist problem. Bellow are my entities:

Parent class
========

@Id
@GeneratedValue(strategy = SEQUENCE, generator = "seq_idP")
private long idP;

@OneToMany(mappedBy = "idPChildren", cascade = { CascadeType.ALL }, fetch = EAGER)
private List<Children> childrenCollection;

Children class
=========

@EmbeddedId
private ChildrenPK pk;

@ManyToOne
@JoinColumn(name = "idP", referencedColumnName = "idP", insertable = false, updatable = false)
private Children idPChildren;

PK Class
======

@Column(name = "idP", insertable = false, updatable = false)
private long idP2;
private long line;

We take above example and map it to Invoice-InvoiceLines one-to-many mapping.

What are the problems?
1. if I have
1 Invoice with say 1 InvoiceLines
and I edit this, adding another InvoiceLines I'd have:

1 Invoice with 2 InvoiceLines
In my application I use em.merge to save this in the database, however only 1 InvoiceLines object is updated and the 2nd line is not added.

My understanding of "merge" is that it should update what already exists and add what's new. Am I correct?

2.

if I create a completely new object Invoice and add two new lines of InvoiceLines object then I try to "persist" them I get the error:

13:02:07,406 ERROR [JDBCExceptionReporter] ERROR: insert or update on table "InvoiceLines" violates foreign key constraint "fkInvoiceInvoicelines"
Detail: Key (idinvoice)=(0) is not present in table "Invoice".

this makes me think that during the persist first is getting inserted the children rows and then the primary, because otherwise the Parent primary key would get generated as its a sequence.

Is there another approach to solve this?

Thank you for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 30, 2009 11:59 am 
Newbie

Joined: Mon Oct 04, 2004 7:13 am
Posts: 19
remove the insertable/updatable=false


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 30, 2009 12:01 pm 
Newbie

Joined: Mon Oct 04, 2004 7:13 am
Posts: 19
remove all the insertable/updatable=false


Top
 Profile  
 
 Post subject: Repeated column in mapping for entity: InvoiceLines column:
PostPosted: Fri Jan 30, 2009 12:29 pm 
Newbie

Joined: Fri Jan 30, 2009 6:31 am
Posts: 13
Location: Bucharest/Romania
if I remove both, I get:

Code:
javax.persistence.PersistenceException: org.hibernate.MappingException: Repeated column in mapping for entity: InvoiceLines column: idP (should be mapped with insert="false" update="false")


that's because the primary key is referenced both in the PK class and Children class.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2009 12:27 pm 
Newbie

Joined: Fri Jan 30, 2009 6:31 am
Posts: 13
Location: Bucharest/Romania
hi guys,

don't want to sound too pushy but I don't think I'm the only who got into this problem. Well, if I am I guess this is a bug. Any help is very much appreciated.

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2009 12:35 pm 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Change your mapping as followed:
Code:
Parent class
========

@Id
@GeneratedValue(strategy = SEQUENCE, generator = "seq_idP")
private long idP;

@OneToMany(mappedBy = "pk.idPChildren", cascade = { CascadeType.ALL }, fetch = EAGER)
private List<Children> childrenCollection;

Children class
=========

@EmbeddedId
private ChildrenPK pk;


PK Class
======

@ManyToOne
@JoinColumn(name = "idP", referencedColumnName = "idP")
private Parent idPChildren;
private long line;


Alternatively you could use @IdClass instead of @EmbeddedId, if you don't want to navigate across "pk" to get a Parent.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 03, 2009 11:23 am 
Newbie

Joined: Fri Jan 30, 2009 6:31 am
Posts: 13
Location: Bucharest/Romania
this solved my problem, thanks :-)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 10:42 am 
Newbie

Joined: Fri Jan 30, 2009 6:31 am
Posts: 13
Location: Bucharest/Romania
Sorry to reopen this thread,

I've played around with the solution. It works well with em.persist but it produces a java.lang.StackOverflowError for em.merge.

Please find below a snippet of the stack trace:

Code:
16:32:56,796 DEBUG SocketWrapper:123 - ClientSocketWrapper[Socket[addr=/127.0.0.1,port=3873,localport=2822].107ebe1] closing
javax.ejb.EJBException: java.lang.RuntimeException: java.lang.StackOverflowError
   at org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:63)
   at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:83)
   at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:191)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
   at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:95)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
   at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
   at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
   at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:110)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
   at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:46)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
   at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
   at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:304)
   at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:106)
   at org.jboss.aspects.remoting.AOPRemotingInvocationHandler.invoke(AOPRemotingInvocationHandler.java:82)
   at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:769)
   at org.jboss.remoting.transport.socket.ServerThread.processInvocation(ServerThread.java:573)
   at org.jboss.remoting.transport.socket.ServerThread.dorun(ServerThread.java:373)
   at org.jboss.remoting.transport.socket.ServerThread.run(ServerThread.java:166)


I'm not really sure what's the problem.

[/code]

Right now the mappings are:

Code:
Parent class
========

@Id
@GeneratedValue(strategy = SEQUENCE, generator = "seq_idP")
private long idP;

@OneToMany(mappedBy = "pk.idPChildren", cascade = { CascadeType.ALL }, fetch = EAGER)
private List<Children> childrenCollection;

Children class
=========

@EmbeddedId
private ChildrenPK pk;


PK Class
======

@ManyToOne
@JoinColumn(name = "idP", referencedColumnName = "idP")
private Parent idPChildren;
private long line;


I've looked trough some of the posts in this forum but couldn't find any to match my problem.

Thanks for all your help


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2009 10:47 am 
Newbie

Joined: Fri Jan 30, 2009 6:31 am
Posts: 13
Location: Bucharest/Romania
Guys

Managed to find the solution. It seems that this is a bug in Hibernate.

Take a look at this:

http://opensource.atlassian.com/project ... se/EJB-225

WORKAROUND
==========

In primary key class set:
@ManyToOne(fetch = LAZY)

By default (fetch = EAGER) for the Primary Key class. If you have fetch=EAGER in the @OneToMany entity as well then StackOverflowError is produced.

Thanks


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