[quote=linngallo]Hmmm, entendi.
Bem, acho que o que lhe falei acima lhe conduzirá à tabela com duas chaves estrangeiras, apenas.
Mas, existe uma configuração chamada “@ElementCollection(targetClass = algumaClasse.class)”, acho que isso pode te ajudar, se você usar em associação com o @JoinTable.
Dá uma pesquisada nisso.
Eu tenho um caso que é bem semelhante ao que você quer, dá uma olhada:
@ElementCollection(targetClass = UsuarioSenhaVO.class)
@JoinTable(schema = "NomeDoBancoAqui", name = "senhasUsuario", joinColumns = {
@JoinColumn(name = "idUsuario", referencedColumnName = "id"),
@JoinColumn(name = "OutroId", referencedColumnName = "OutroId") })
private List<UsuarioSenhaVO> senhasUsuario;
[/quote]
Muito Obrigado linngallo e jakefrog, consegui resolver com a ajuda de voces, vlw mesmo.
O mepeamento ficou assim:
// Person.java
package entity;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Table(name = "person")
public class Person implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@Temporal(TemporalType.DATE)
private Date birthday;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created_date")
private Date createdDate;
@ElementCollection(targetClass = Email.class)
@CollectionTable(name = "person_email", joinColumns = @JoinColumn(name = "person_id"))
//@JoinTable(name = "person_email", joinColumns = {@JoinColumn(name = "person_id", referencedColumnName = "id")})
private List<Email> emails;
public Person() {
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
public List<Email> getEmails() {
return emails;
}
public void setEmails(List<Email> emails) {
this.emails = emails;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
// Email.java
package entity;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class Email implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String email;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created_date")
private Date createdDate;
public Email() {
}
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
Se eu deixar o JPA gerar as tabelas ele gera uma Email tambem e dai ocorre o seguinte erro:
Caused by: org.apache.openjpa.lib.jdbc.ReportingSQLException: Incorrect table definition; there can be only one auto column and it must be defined as a key {stmnt 905804980 CREATE TABLE person_email (person_id BIGINT, id BIGINT AUTO_INCREMENT, created_date DATETIME, email VARCHAR(255)) ENGINE = innodb} [code=1075, state=42000]
O erro acontece porque não foi definido o campo Primary Key da tabela.
Mas eu gerei as tabelas por script SQL e desta maneira o mapeamento funcionou corretamente.
Muito Obrigado