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-keyI hope someone can help me. Thank you
Regards,
Arthur Ronald F D Garcia