-->
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.  [ 4 posts ] 
Author Message
 Post subject: Cascade.Persist on OneToMany lazy associations
PostPosted: Thu Jul 27, 2006 3:40 pm 
Beginner
Beginner

Joined: Fri Jan 16, 2004 11:05 am
Posts: 33
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.2cr2
Annotations 3.2cr1
EntityManager 3.2cr1

Mapping documents:
Mapping something equivalent to the following:

Code:
@Entity
public class A
{
    private Set<B> bs;
    private Set<C> cs;

    public A()
    {
        bs = new HashSet<B>();
        cs = new HashSet<C>();
    }

    public void addB( B b )
    {
        b.setA( this );
        bs.add( b );
    }

    public void addC( C c )
    {
        c.setA( this );
        cs.add( c );
    }

    @OneToMany( mappedBy="a", fetch=FetchType.LAZY )
    @Cascade( value={CascadeType.ALL, CascadeType.DELETE_ORPHAN} )
    public Set<B> getBs()
    {
        return bs;
    }

    @OneToMany( mappedBy="a", fetch=FetchType.LAZY )
    @Cascade( value={CascadeType.ALL, CascadeType.DELETE_ORPHAN} )
    public Set<C> getCs()
    {
        return cs;
    }

}

@Entity
public class B
{
    private A a;
   
    @ManyToOne( fetch=FetchType.LAZY )
    @JoinColumn( name = "a_fk" )
    public getA()
    {
        return a;
    }
}

@Entity
public class C
{
    private A a;
   
    @ManyToOne( fetch=FetchType.LAZY )
    @JoinColumn( name = "a_fk" )
    public getA()
    {
        return a;
    }
}


Code between sessionFactory.openSession() and session.close():
Code:
A a = new A();
B b = new B();
a.addB( b );
em.persist( a );


Is it expected that the above would cause all cs to be read/initialized from the database, because we added a b?

What I would really want is to not load either bs or cs, when I add a child. It appears that I would either need to switch to using a bag or get rid of the cascades. Since i prefer to use Sets this means I cannot cascade.

Any help would be greatly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 28, 2006 2:23 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
@ManyToOne(CascadeType.PERSIST)

b.setA(a);
s.persist(b)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 03, 2006 7:12 am 
Newbie

Joined: Wed May 31, 2006 6:40 am
Posts: 19
emmanuel wrote:
@ManyToOne(CascadeType.PERSIST)

b.setA(a);
s.persist(b)


i'm using jboss 4.04 ga... it says...

12:27:30,664 INFO [Version] Hibernate EntityManager 3.2.0.CR1
12:27:30,680 INFO [Version] Hibernate Annotations 3.2.0.CR1
12:27:30,696 INFO [Environment] Hibernate 3.2 cr2

sorry i need a bit more explanation....

i have objects Gruppo, Abilitazione in many to many relations througth the object AbilitazioneGruppo.....

a Gruppo can have zero to many AbilitazioneGruppo while AbilitazioneGruppo is linked with exactly one Gruppo and exactly one Abilitazione

with hibernate tools i got correctly that a Gruppo has Set<AbilitazioneGruppo> abilitazioneGruppos

and AbilitazioneGruppo has a Gruppo gruppo;

i try to delete an AbilitazioneGruppo from Set<AbilitazioneGruppo> abilitazioneGruppos, i see in debug that this works fine but i try to merge gruppo... but the AbilitazioneGruppo was still there!?!??

i have these annotations....

in Gruppo
@OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.LAZY, mappedBy="gruppo")
public Set<AbilitazioneGruppo> getAbilitazioneGruppos() {
return this.abilitazioneGruppos;

}

in AbilitazioneGruppo (i think it's here the problem)

@ManyToOne(cascade={},
fetch=FetchType.LAZY)

@JoinColumn(name="codicegruppo", unique=false, nullable=false, insertable=false, updatable=false)
public Gruppo getGruppo() {
return this.gruppo;

}


i have to put @ManyToOne(CascadeType.PERSIST) in AbilitazioneGruppo for property gruppo?
[OT] how can i get it in my entity with hibernate tools??? i made a lot of modification to template... but i think this is over my knowledge :(
thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 07, 2006 1:29 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
From what I understand you are lookinf for the delete orphaned feature. Check the hibernate ref doc, it gives an example on how it works

_________________
Emmanuel


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