-->
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.  [ 11 posts ] 
Author Message
 Post subject: ManyToOne annotation problem
PostPosted: Mon Feb 25, 2008 12:24 pm 
Regular
Regular

Joined: Wed Apr 25, 2007 10:29 am
Posts: 110
Location: France
Hello,

I searched a lot but I don't found the answer. I want to map a bidirectionnal relation.

My 2 classes :

@Entity
public class FichierOriginal
{
...
private List<Action> actions;

@OneToMany( cascade = CascadeType.ALL, mappedBy="fichierOriginal")
public List<Action> getActions() {
return actions;
}

...

}


@Entity
public class Action
{
...
private FichierOriginal fichierOriginal;
...

@ManyToOne
@JoinColumn(name="idFichierOriginal")
public FichierOriginal getFichierOriginal() {
return fichierOriginal;
}

...
}


getActions always returns an empty List and getFichierOriginal is always a null value.

Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 27, 2008 5:52 am 
Regular
Regular

Joined: Wed Apr 25, 2007 10:29 am
Posts: 110
Location: France
Up ... it's not a common problem ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 27, 2008 10:13 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
The Entities look like good, can you show some code?

You update both sides of relation in the code?
Code:
fichierOriginal.getActions.add(action);
action.setFichierOriginal(fichierOriginal)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 27, 2008 10:32 am 
Regular
Regular

Joined: Wed Apr 25, 2007 10:29 am
Posts: 110
Location: France
Thank you for your help.

Code:
Transaction tx = session.beginTransaction();

FichierOriginal fichierOriginal = new FichierOriginal("path");
Action action = new Action(TypeAction.CONVERSION);
fichierOriginal.getActions().add(action);
action.setFichierOriginal(fichierOriginal);
   
session.persist(fichierOriginal);

tx.commit();


In a second time, I test the values :

Code:
FichierOriginal fichierOriginal2 = (FichierOriginal) session2.get(FichierOriginal.class,new Integer(1));

System.out.println(fichierOriginal2.getActions());


But it displays [] because the list is empty.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 27, 2008 10:57 am 
Newbie

Joined: Fri Jan 16, 2004 9:59 am
Posts: 18
Location: La Plata-Argentina / Luxembourg
inverse=true in the list is missing


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 27, 2008 11:13 am 
Regular
Regular

Joined: Wed Apr 25, 2007 10:29 am
Posts: 110
Location: France
surrizola wrote:
inverse=true in the list is missing


Hello,

Where must I write inverse=true ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 27, 2008 12:51 pm 
Newbie

Joined: Fri Jan 16, 2004 9:59 am
Posts: 18
Location: La Plata-Argentina / Luxembourg
in the mapping @OneToMany


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 27, 2008 12:57 pm 
Regular
Regular

Joined: Wed Apr 25, 2007 10:29 am
Posts: 110
Location: France
surrizola wrote:
in the mapping @OneToMany


@OneToMany( cascade = CascadeType.ALL, mappedBy="fichierOriginal", inverse="true") ?

Is it necessary to update both sides ?
Code:
fichierOriginal.getActions.add(action);
action.setFichierOriginal(fichierOriginal)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 27, 2008 1:47 pm 
Senior
Senior

Joined: Tue Jul 25, 2006 9:05 am
Posts: 163
Location: Stuttgart/Karlsruhe, Germany
inverse is not in annotations, but mappedBy is the same thing

Cheers,

Andy

_________________
Rules are only there to be broken


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 27, 2008 6:00 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
Is it necessary to update both sides ?

Yes it is.

You could be in trouble because of the @JoinColumn specification; try commenting that to see if it helps. To specify physical mappings on a bidirectional association you need some special care, you'll find more information on the reference: http://www.hibernate.org/hib_docs/annotations/reference/en/html_single/#d0e1136
in particular at "2.2.5.3.2.1. Bidirectional"


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 28, 2008 5:03 am 
Regular
Regular

Joined: Wed Apr 25, 2007 10:29 am
Posts: 110
Location: France
Thank you for your help.

I solved the problem, : I forgot to delete tables between tests :S

Code:
(SchemaExport se = new SchemaExport and se.create(true,true)).


Thanks again


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