如何在jpa中的一对多关系中select child
how to select child in one to many relation in jpa
我想 select parent 和我想要的 child。
但是当我 select 我的 parent 我必须显示所有 childs
我该怎么做?
示例:
public class parent{
private Integer id;
@OnetoMany
@JoinColumn(name="parentId")
private List<child> children;
}
public class child{
private Integer id;
private Integer parentId;
}
findByIdAndchildType(Integer id, String type)
我想看:parent(id) -> child (type)
但我可以看到 parent(id) - > child(othertype), child(othertype1), child(type)
在我看来,您正在尝试建立 bi-directional 关系。这可以通过将映射添加到关系的两侧来实现。
例如,将 @ManyToOne
映射添加到 Child
实体。请注意,您可能应该删除 parentId
字段,因为现在您可以使用 child.getParent().getId()
.
访问它
@Entity
public class Child {
@Id
private Integer id;
@ManyToOne
@JoinColumn(name = "parentId")
private Parent parent;
// Remove parentId field
// Getters + Setters ...
}
NOTE: If you want to keep the parentId
field, you'll have to choose which two of the mappings (getParentId()
or getParent().getId()
) you want to use for inserting and updating entities. The other field should have both insertable = false
and updatable = false
.
下一步是将 @OneToMany
映射更改为使用 mappedBy
:
@Entity
public class Parent {
@Id
private Integer id;
@OneToMany(mappedBy = "parent") // Change this
private List<Child> children;
// Getters + Setters ...
}
如果您想检索特定的 child 及其 parent,您现在可以为 Child
个实体创建存储库:
public interface ChildRepository extends JpaRepository<Child, Integer> {
}
之后,您可以通过以下方式获得特定的child:
Optional<Child> child = repository.findById(123); // 123 is the ID of the child in this case
Optional<Parent> parent = child.map(Child::getParent);
使用 Spring 启动 1.x 将是:
Child child = repository.findOne(123);
Parent parent = null;
if (child != null) {
parent = child.getParent();
}
我想 select parent 和我想要的 child。
但是当我 select 我的 parent 我必须显示所有 childs
我该怎么做?
示例:
public class parent{
private Integer id;
@OnetoMany
@JoinColumn(name="parentId")
private List<child> children;
}
public class child{
private Integer id;
private Integer parentId;
}
findByIdAndchildType(Integer id, String type)
我想看:parent(id) -> child (type)
但我可以看到 parent(id) - > child(othertype), child(othertype1), child(type)
在我看来,您正在尝试建立 bi-directional 关系。这可以通过将映射添加到关系的两侧来实现。
例如,将 @ManyToOne
映射添加到 Child
实体。请注意,您可能应该删除 parentId
字段,因为现在您可以使用 child.getParent().getId()
.
@Entity
public class Child {
@Id
private Integer id;
@ManyToOne
@JoinColumn(name = "parentId")
private Parent parent;
// Remove parentId field
// Getters + Setters ...
}
NOTE: If you want to keep the
parentId
field, you'll have to choose which two of the mappings (getParentId()
orgetParent().getId()
) you want to use for inserting and updating entities. The other field should have bothinsertable = false
andupdatable = false
.
下一步是将 @OneToMany
映射更改为使用 mappedBy
:
@Entity
public class Parent {
@Id
private Integer id;
@OneToMany(mappedBy = "parent") // Change this
private List<Child> children;
// Getters + Setters ...
}
如果您想检索特定的 child 及其 parent,您现在可以为 Child
个实体创建存储库:
public interface ChildRepository extends JpaRepository<Child, Integer> {
}
之后,您可以通过以下方式获得特定的child:
Optional<Child> child = repository.findById(123); // 123 is the ID of the child in this case
Optional<Parent> parent = child.map(Child::getParent);
使用 Spring 启动 1.x 将是:
Child child = repository.findOne(123);
Parent parent = null;
if (child != null) {
parent = child.getParent();
}