[Resolvido] Mapear Classe Hibernate Com Chave Primária Composta

Alguém pode me ajudar no mapeamento da classe abaixo.


public class Elo {

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name = "id_elo")
	private Long id;
	
	@ManyToOne
	@JoinColumn(name = "id_user",referencedColumnName="id_user",nullable=false)
	@Cascade(CascadeType.ALL)
	private UserOff userOff;
	
	@Column(name = "id_follower",nullable=false)	
	private Long idFollower;
...

Eu coloquei um atributo id_elo mas na verdade a combinação dos atributos id_user e id_follower já corresponderia a chave primária, no caso composta.

Entretanto, eu nunca mapeei uma chave composta usando o Hibernate Annotations. Procurei na net, mas n achei nada parecido. Alguém pode me ajudar a substituir a chave primaria id_elo por uma chave primaria composta (id_user, id_follower)?

Agradecido.

public class Elo {

	@EmbeddedId
	private EloPK id;
	
	@ManyToOne
	@JoinColumn(name = "id_user", insertable=false, updatable=false)
	private UserOff userOff;
}
@Embeddable
public class EloPK {

	@Column(name = "id_user",nulable=false)
	private Long idUserOff;
	
	@Column(name = "id_follower",nulable=false)	
	private Long idFollower;
        
        /* implementar equals e hashcode*/
}

Funcionou.

O Código Final ficou assim:

@Embeddable  
public class EloPK implements Serializable{  
  
    @Column(name = "id_user",nullable=false)  
    private Long idUserOff;  
      
    @Column(name = "id_follower",nullable=false)   
    private Long idFollower;

	public Long getIdUserOff() {
		return idUserOff;
	}

	public void setIdUserOff(Long idUserOff) {
		this.idUserOff = idUserOff;
	}

	public Long getIdFollower() {
		return idFollower;
	}

	public void setIdFollower(Long idFollower) {
		this.idFollower = idFollower;
	}

}  

@Entity(name="elos_follower")
public class Elo {

    @EmbeddedId  
    private EloPK id;  
    
    @ManyToOne  
    @JoinColumn(name = "id_user", insertable=false, updatable=false,nullable=false) 
	private UserOff userOff;
...
}


Entretanto, eu não reimplementei os métodos equal e hashcode.

Poderia me explicar pq há a necessidade?

Aqui tem uma explicação completa: https://community.jboss.org/wiki/EqualsAndHashCode