Skip to content

Instantly share code, notes, and snippets.

@paulodiogo
Created February 19, 2014 18:43
Show Gist options
  • Save paulodiogo/9098642 to your computer and use it in GitHub Desktop.
Save paulodiogo/9098642 to your computer and use it in GitHub Desktop.
definindo uma chave composta no hibernate usando @embeddable
//Definindo
@Embeddable
public class ExemploPK implements Serializable {
private static final long serialVersionUID = -4978349929535369754L;
public ExemploPK() {
}
@Column(name = "COLUNA_1", nullable = false)
private Long coluna1;
@Column(name = "COLUNA_2")
private String coluna2;
public Long getcoluna1() {
return coluna1;
}
public void setcoluna1(Long coluna1) {
this.coluna1 = coluna1;
}
public String getcoluna2() {
return coluna2;
}
public void setcoluna2(String coluna2) {
this.coluna2 = coluna2;
}
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof ExemploPK))
return false;
if (this == obj)
return true;
ExemploPK castObj = (ExemploPK) obj;
return castObj.getcoluna1().equals(this.getcoluna1())
&& castObj.getcoluna2().equals(this.getcoluna2());
}
@Override
public int hashCode() {
return 7 * coluna1.hashCode() ^ coluna2.hashCode();
}
}
//usando
@EmbeddedId
private ExemploPK tituloPK = new ExemploPK();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment