-->
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: Hibernate adding to new entry to @ManyToMany collection
PostPosted: Fri Oct 28, 2016 7:07 pm 
Newbie

Joined: Fri Oct 28, 2016 6:49 pm
Posts: 3
Hello.

I'm currently learning how to use Hibernate and JPA and got a following question

Code:
public class Parent{

    @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    private Set<Child> child= new HashSet<>();

    public void addLink(Child c){
         this.child.add(c);
         c.getParent().add(c);   
    }
}

public class Child{

    @ManyToMany(mappedBy = "child")
    private Set<Parent> parent = new HashSet<>();
}


Is it possible to add links for existing entities without fetching both entities and collections?


Top
 Profile  
 
 Post subject: Re: Hibernate adding to new link to collection
PostPosted: Sat Oct 29, 2016 1:00 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
It is possible, but you have to switch for @ManyToMany to using 2 bidirectional @OneToMany where the link table is mapped as an entity. Check out the User Guide for more details.

That way, you can add the link entity freely even when the two parent entity associations are proxies or detached objects.


Top
 Profile  
 
 Post subject: Re: Hibernate adding to new entry to @ManyToMany collection
PostPosted: Sat Oct 29, 2016 10:18 am 
Newbie

Joined: Fri Oct 28, 2016 6:49 pm
Posts: 3
So I have to directly persist the Link entity or is it possible to directly add link using add method of parent entitity to add new link without fetching collections?


Top
 Profile  
 
 Post subject: Re: Hibernate adding to new entry to @ManyToMany collection
PostPosted: Sat Oct 29, 2016 1:54 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You can try to add the entity while the collection is still lazy. If you have a List, instead of a Set, then it might work.


Top
 Profile  
 
 Post subject: Re: Hibernate adding to new entry to @ManyToMany collection
PostPosted: Sat Oct 29, 2016 3:19 pm 
Newbie

Joined: Fri Oct 28, 2016 6:49 pm
Posts: 3
List seems to be working it doesnt fetch collections anymore on add using Parent method still fetches entities I'm not sure why. From what I know getReference shouldn't generate select unless I access property but it generates select without accessing anything.

Code:
@Entity(name = "Child")
public class Child implements Serializable{
   
    @Id
    @GeneratedValue
    private Integer id;

    @OneToMany(mappedBy = "child", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<ParentChild> parents = new ArrayList<>();

    @Override
    public boolean equals(Object o) {
        if ( this == o ) {
            return true;
        }
        if ( o == null || getClass() != o.getClass() ) {
            return false;
        }
        Child u = (Child)o;
        return Objects.equals( id, u.id );
    }

    @Override
    public int hashCode() {
        return Objects.hash( id );
    }

}

@Entity(name = "Parent")
public class Parent implements Serializable{

    @Id
    @GeneratedValue
    private Integer id;

    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<ParentChild> children = new ArrayList<>();

    public void addChild(Child child){
        ParentChild parentChild = new ParentChild(this,child);
        this.children.add(parentChild);
        child.getParents().add(parentChild);
    }

    @Override
    public boolean equals(Object o) {
        if ( this == o ) {
            return true;
        }
        if ( o == null || getClass() != o.getClass() ) {
            return false;
        }
        Parent u = (Parent)o;
        return Objects.equals( id, u.id );
    }

    @Override
    public int hashCode() {
        return Objects.hash( id );
    }
}

@Entity(name = "ParentChild")
public class ParentChild implements Serializable{

    @Id
    @ManyToOne
    private Parent parent;

    @Id
    @ManyToOne
    private Child child;

    public ParentChild(Parent parent, Child child) {
        this.parent = parent;
        this.child = child;
    }

    public ParentChild() {
    }

    @Override
    public boolean equals(Object o) {
        if ( this == o ) {
            return true;
        }
        if ( o == null || getClass() != o.getClass() ) {
            return false;
        }
        ParentChild that = (ParentChild) o;
        return Objects.equals( parent, that.parent ) &&
                Objects.equals( child, that.child );
    }

    @Override
    public int hashCode() {
        return Objects.hash( parent,child );
    }
}


Code:
    @Override
    @Transactional
    public void linkChildToParent(Integer childId, Integer parentId){
        Parent parent = em.getReference(Parent.class,childId);
        Child child = em.getReference(Child.class,parentId);
        parent.addChild(child);
    }


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.