具体来说,我有这些Java代码(A类和B类):
//Class A
package models;
import java.util.*;
import javax.persistence.*;
import play.db.ebean.Model;
@Entity
@Table(name="A")
public class A extends Model {
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="id_a_seq")
@SequenceGenerator(name="id_a_seq", sequenceName="id_a_seq")
@Column(name="ID_A", insertable=true, updatable=true, unique=true, nullable=false)
public Integer id;
@Column(name="NOME")
public String nome;
@Column(unique = true, name="CPF")
public String cpf;
@ManyToMany(cascade = {CascadeType.ALL})
@JoinTable(name="A_B",
joinColumns={@JoinColumn(name="ID_A")},
inverseJoinColumns={@JoinColumn(name="ID_B")})
public List<B> lista = new ArrayList<B>();
}
//Class B
package models;
import java.util.*;
import javax.persistence.*;
import play.db.ebean.Model;
@Entity
@Table(name="B")
public class B extends Model {
public B() {}
public B(Integer id, String nome) {
this.id = id;
this.nome = nome;
}
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="id_b_seq")
@SequenceGenerator(name="id_b_seq", sequenceName="id_b_seq")
//@AttributeOverride(name = "id", column = @Column(name = "ID_B"))
@Column(name="ID_B", insertable=true, updatable=true, unique=true, nullable=false)
public Integer id;
@Column(name="NOME")
public String nome;
@ManyToMany(mappedBy="lista")
public List<A> lista = new ArrayList<A>();
}
当我在浏览器中运行localhost:9000时,收到以下错误消息:
&#34; PersistenceException:在[models.B.lista]上加入时出错。无法找到[ID_B]的本地匹配可能是@ JoinColumn&#34;中的错误。
我可以解决这个问题,将ID_A和ID_B重命名为ID(两者都有),但我不能拥有id列的名称ID(在表A和表B中)。我需要弄清楚如何使Play接受其ID列名称的ID_A和ID_B名称。
答案 0 :(得分:1)
将lista
关系的映射更改为:
@ManyToMany(cascade = {CascadeType.ALL})
@JoinTable(name = "A_B",
joinColumns = {@JoinColumn(name = "ID_A", referencedColumnName = "ID_A")},
inverseJoinColumns = {@JoinColumn(name = "ID_B", referencedColumnName = "ID_B")})
public List<B> lista = new ArrayList<B>();
请注意referencedColumnName
添加@JoinColumn
。
在JoinTable注释中使用时,引用的键列位于拥有实体的实体表中,如果连接是反连接定义的一部分,则为反向实体。