我正在使用JPA在IntelliJ中开发简单的web-app。我有一个包含3个表和这些记录的数据库:
records.getRecordsList().stream()
.filter(r->r.getTrackListing().size()>10)
.filter(r >sourceFormat.parse(r.getReleaseDate()).before(beforeDate)))
.collect(Collectors.toList()));
同样在Entity1_Entity2表中,指定了这些关系:
(参考1) - > ENTITY1(ID)
(参考文献2) - > ENTITY2(ID)
使用JPA我创建了实体Entity1和Entity2:
Entity1 Entity1_Entity2 Entity2
ID: Ref1: Ref2: ID:
1 1 2 1
2 1 1 2
我的问题是,当我选择Entity1的实例(例如ID = 1)并调用entity1.getList()时,它总是返回null引用,但根据数据 - 应该返回2个Entity2对象。
尝试将@JoinTable注释更改为这些值,但它没有帮助:
@Entity
@Table(name = "Entity1")
@Getter
@Setter
public class Entity1 implements Serializable {
@Id
@Column(name = "ID")
private int id;
@ManyToMany
@JoinTable(name = "Entity1_Entity2",
joinColumns = { @JoinColumn(name = "Ref1") },
inverseJoinColumns = { @JoinColumn(name = "Ref2") })
private List<Entity2> list = new ArrayList<>();
}
@Entity
@Table(name = "Entity2")
@Getter
@Setter
public class Entity2 implements Serializable {
@Id
@Column(name = "ID")
private int id;
@ManyToMany(mappedBy = "list")
private List<Entity1> otherList = new ArrayList<>();
}
答案 0 :(得分:0)
我认为您在使用 @Getter 和 @Setter
时出现问题因为下一步对我很好:)
注意:我没有尝试使用 @Getter 和 @Setter
<强> ENTITY1 强>
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name = "Entity1")
public class Entity1 implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name = "ID")
private int id;
@ManyToMany
@JoinTable(name = "Entity1_Entity2",
joinColumns = { @JoinColumn(name = "Ref1", referencedColumnName = "ID") },
inverseJoinColumns = { @JoinColumn(name = "Ref2", referencedColumnName = "ID") })
private List<Entity2> list = new ArrayList<>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public List<Entity2> getList() {
return list;
}
public void setList(List<Entity2> list) {
this.list = list;
}
}
<强> ENTITY2 强>
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name = "Entity2")
public class Entity2 implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name = "ID")
private int id;
@ManyToMany(mappedBy = "list")
private List<Entity1> otherList = new ArrayList<>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public List<Entity1> getOtherList() {
return otherList;
}
public void setOtherList(List<Entity1> otherList) {
this.otherList = otherList;
}
}
并按预期生成第三个表,其中包含两列(ref1,ref2)
无论如何我正在使用JPA 2.1与Hibernate提供程序,所以做下一个代码会得到你期望的。
Entity1 entity1 = entityManager.find(Entity1.class,1);
entity1.getList();