-->
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: Composite id and list index solution
PostPosted: Thu Jun 25, 2009 5:09 pm 
Newbie

Joined: Thu Jun 25, 2009 3:45 am
Posts: 4
Hi, see the following code

Code:
@Entity
public class Team {

    @Id
    @GeneratedValue(GenerationType.AUTO)
    @Column(name="TEAM_ID")
    private Integer id;

    // One way relationship
    @OneToMany(cascade=CascadeType.PERSIST)
    @JoinColumn(name="TEAM_ID", nullable=false)
    @IndexColumn(name="PLAYER_INDEX", nullable=false)
    private List<Player> playerList = new ArrayList<Player>();

    // getter's and setter's
   
}

@Entity
public class Player {

    @Embeddable
    public static class Player$Id {

        @Column(name="TEAM_ID", insertable=false, updatable=false)       
        private Integer teamId;
       
        @Column(name="PLAYER_INDEX", insertable=false, updatable=false)       
        private Integer playerIndex; 

        // equals and hashcode implementation

    }

    @EmbeddedId
    private Player$Id id;

    // getter's and setter's

}



So, if i use the following

Code:

Team team = new Team();

team.getPlayerList().add(new Player());
team.getPlayerList().add(new Player());

Session session = HibernateUtil().getSessionFactory().openSession();
session.beginTransaction();

session.save(team); // Hibernate complains duplicate column(insert=false, update=false)

session.getTransaction().commit();
session.close();



Notice that i want to save a team among your players. Player is, in fact, a entity, not a component - it has relationship with other entities. TEAM_ID and PLAYER_INDEX in Team class mapping could play the role of Player's composite identifier in cascadeType.PERSIST operation.

I do not understand why. Notice there are a similar mapping in Hibernate reference documentation but is it also does not work

http://docs.jboss.org/hibernate/stable/core/reference/en/html/misc.html#example-mappings-composite-key

I hope someone can help me. Thank you

Regards,
Arthur Ronald F D Garcia


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.