-->
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.  [ 12 posts ] 
Author Message
 Post subject: one to many issue
PostPosted: Sat Apr 23, 2011 2:58 pm 
Beginner
Beginner

Joined: Sun Jan 30, 2011 8:14 am
Posts: 24
Dear all,
I've a question for you.
I've to implement with annotation a one to many relation between object paziente and another object.
I post some code.
When I add a Paziente object, other object is saved, but foreign key (paziente_id) is always null.
I missed something with foreign key.
Could you help me?

Code:
public class Paziente implements java.io.Serializable{
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "ID_PAZIENTE")
    private long    idPaziente;
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name="ID_ANAGRAFICA")
    private pzAnagrafica            cAnagrafica;

    [b]@OneToMany(mappedBy="paziente_fk",cascade = CascadeType.ALL)
    private Set<pzAnamnFisioAllergia> allergie = new HashSet<pzAnamnFisioAllergia>(0);

    public Set<pzAnamnFisioAllergia> getAllergie() {
        return this.allergie;
    }
    public void setAllergie(Set<pzAnamnFisioAllergia> allergie) {
        this.allergie = allergie;[/b]
    }

    public Paziente() {
    }
     
}


Code:
@Entity
@Table(name = "an_fis_allergia")

public class pzAnamnFisioAllergia implements java.io.Serializable{
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "ID_ALLERGIA")
    private long        idAllergia;
     @ManyToOne
    @JoinColumn(name="paziente_fk", insertable=false, updatable=false)
    private Paziente paziente_fk;


    public Paziente getPaziente()
    {
        return paziente_fk;
    }


could you help me?
Where I'm going wrong?
In the child table, my parent is always NULL...

Thanks


Top
 Profile  
 
 Post subject: Re: one to many issue
PostPosted: Tue Apr 26, 2011 8:26 am 
Beginner
Beginner

Joined: Sun Jan 30, 2011 8:14 am
Posts: 24
no one can help me?
I'm not able to have a value different from null in my foreign key...


Top
 Profile  
 
 Post subject: Re: one to many issue
PostPosted: Tue Apr 26, 2011 8:34 am 
Newbie

Joined: Fri Dec 15, 2006 4:24 am
Posts: 8
I already have a similar problem...

Lets try to solve it toguether.


Top
 Profile  
 
 Post subject: Re: one to many issue
PostPosted: Tue Apr 26, 2011 9:17 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Can you post the code where you are creating and saving the object ?


Top
 Profile  
 
 Post subject: Re: one to many issue
PostPosted: Tue Apr 26, 2011 2:52 pm 
Beginner
Beginner

Joined: Sun Jan 30, 2011 8:14 am
Posts: 24
Set<pzAnamnFisioAllergia> cAllergie = new HashSet<pzAnamnFisioAllergia>(0);
for (int j = 0; j < cRows.size(); j++) {

//do some logic
cAllergie.add(cAllergia);

}
cPaziente.setAllergie(cAllergie);


Top
 Profile  
 
 Post subject: Re: one to many issue
PostPosted: Tue Apr 26, 2011 2:56 pm 
Beginner
Beginner

Joined: Sun Jan 30, 2011 8:14 am
Posts: 24
sorry...in the previous post reply was not correct and I can't edit it.
This is my logic to save this set in my cPaziente Object

Code:
Set<pzAnamnFisioAllergia> cAllergie = new HashSet<pzAnamnFisioAllergia>(0);
            for (int j = 0; j < cRows.size(); j++) {
                Allergia all = new Allergia();
                 //do some logic               
                cAllergie.add(cAllergia);

            }
            cPaziente.setAllergie(cAllergie);
            PazienteMng.create(cPaziente);
where


Code:
protected void create(Paziente cPaz) {
        try {
            startOperation();
            session.saveOrUpdate(cPaz);
            tx.commit();

        } catch (HibernateException e) {
            handleException(e);
        } finally {
            HibernateFactory.close(session);
        }
    }

             


that is, in few words, I create and fill collection of childs, I set collection for parent and I save parent.

Where I'm going in error?

Thanks, please help me


Top
 Profile  
 
 Post subject: Re: one to many issue
PostPosted: Tue Apr 26, 2011 4:37 pm 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Do you set the paziente_fk property for each pzAnamnFisioAllergia of your collection ?


Top
 Profile  
 
 Post subject: Re: one to many issue
PostPosted: Tue Apr 26, 2011 5:27 pm 
Beginner
Beginner

Joined: Sun Jan 30, 2011 8:14 am
Posts: 24
yes...at leaast I think....I do
Code:
Paziente cPaziente = new Paziente();
cPaziente .setName("Michael Best");
Set<pzAnamnFisioAllergia> cAllergie = new HashSet<pzAnamnFisioAllergia>(0);
            for (int j = 0; j < cRows.size(); j++) {
                Allergia all = new Allergia();
                all.set(....)
                all.setPaziente_fk(cPaziente);
                 //do some logic               
                cAllergie.add(cAllergia);

            }
            cPaziente.setAllergie(cAllergie);
            PazienteMng.create(cPaziente);


I do it with

Code:
all.setPaziente_fk(cPaziente);

but at that point cPaziente is not yet saved...cause I save it at the end...is this the problem?
What can I do?
Thanks very much for your helpe..


Top
 Profile  
 
 Post subject: Re: one to many issue
PostPosted: Wed Apr 27, 2011 5:32 am 
Newbie

Joined: Fri Dec 15, 2006 4:24 am
Posts: 8
I solved my problem, It was a insertable=false, updatable=false issue.

Looks that you too have it in your code


Top
 Profile  
 
 Post subject: Re: one to many issue
PostPosted: Thu Apr 28, 2011 8:24 am 
Beginner
Beginner

Joined: Sun Jan 30, 2011 8:14 am
Posts: 24
Ok thanks...I solved following your help and setting true either to insert and update.
There is still an issue...when I try to update parente (with its child), child row are duplicated...that is if I change a property of child 1, child1 property is duplicated...
can someone help me?
Maybe it's caused by cascade all in the parent object?
I tried to change it, but only with cascade all is sets a parent different from null..
I hope someone can help me


Top
 Profile  
 
 Post subject: Re: one to many issue
PostPosted: Wed May 04, 2011 7:33 am 
Beginner
Beginner

Joined: Sun Jan 30, 2011 8:14 am
Posts: 24
no one can help me?


Top
 Profile  
 
 Post subject: Re: one to many issue
PostPosted: Thu May 05, 2011 5:01 am 
Newbie

Joined: Thu May 05, 2011 4:45 am
Posts: 3
I solved following your help and setting true either to insert and update.

_________________
Give up you is not my fault, and the wrong is lack of the money-tiffany jewelry
While there is life, there is hope tiffany jewelry cheap.


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