Created
February 19, 2014 18:43
-
-
Save paulodiogo/9098642 to your computer and use it in GitHub Desktop.
definindo uma chave composta no hibernate usando @embeddable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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