-->
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.  [ 1 post ] 
Author Message
 Post subject: How to move items between collectins in Hibernate 4.3.7?
PostPosted: Thu Nov 20, 2014 5:51 am 
Newbie

Joined: Thu Nov 20, 2014 5:43 am
Posts: 1
Hello

This question was originally posted in StackOverflow (http://stackoverflow.com/questions/27016778/how-to-move-items-between-collections-in-hibernate-4-3-7-final) with no satisfactory answer so far.

I have two entities mapped in my application in order to model a Team and its Members. A Team can have many Members and a Member can belong to no more than one Team. Everything is fine about handling this concepts. The problem comes when I try to move a Member from one existing Team to another.

The entities are presented below, in simplified form. The very last method, transfer(), is the one that should perform the removal of a certain Member from its Team and send it to another one.

Code:
@Entity
public class Member extends Person {
    @ManyToOne
    private Team team;

    protected Member() {
        super();
    }

    public Member(Team team, String name) {
        super(name);
        this.team = team;
    }

    // Trivial getters and setters...

    public Team getTeam() {
        return team;
    }

    protected void setTeam(Team team) {
        this.team = team;
    }
}

@Entity
public class Team {
    @Id
    private long id;
    private String name;
    @OneToMany(mappedBy="team", cascade=CascadeType.ALL)
    private List<Member> members = new ArrayList<Member>();

    protected Team() {
    }

    public Team(String name) {
        this.name = name;
    }

    // trivial getters and setters...

    public Member addMember(String name) {
        Member member = new Member(this, name);
        members.add(member);
        return member;
    }

    protected void addMember(Member member) {
        members.add(member);
        member.setTeam(this);
    }

    public void removeMember(Member member) {
        members.remove(member);
    }

    public Member memberByName(String memberName) {
        for(Member member : members)
          if(member.getName().equals(memberName))
              return member;
        return null;
    }

    public Collection<Members> getMembers() {
        return Collections.unmodifiableCollection(members);
    }

    public void transfer(Member member, Team destination) {
        members.remove(member);
        destination.addMember(member);
    }
}


I have this unit test code that is intended to validate the transfer service

Code:
Team teamA = teamRepository.teamById(idTeamA);
Team teamB = teamRepository.teamById(idTeamB);
Team teamC = teamRepository.teamById(idTeamC);

Member zaki = teamA.memberByName("Zaki");
Member denise = teamA.memberByName("Denise");

EntityTransaction t = teamRepository.transactionBegin();
teamA.transfer(zaki, teamB);
teamA.transferir(denise, teamC);
t.commit();


I have the following exception in the commit() line

Code:
javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: application.domain.Member


Any ideas?

UPDATE 1:

I decided to perform a little test and changed the code of the transfer() method as follows

Code:
public void transfer(Member member, Team destination) {
    member.setTeam(this);
}


The result was curious: no error, but also no update on the tables. Hibernate couldn't track the update and the transfer simply didn't happen.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.