- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有那些实体,它不是来自真实代码,但我认为这并不重要。
@Entity
public class Country{
private String name;
//other fields with getters and setters
@OneToMany
private List<City> cites;
}
@Entity
public class City {
private String name;
//other fields with getters and setters
@OneToMany
private List<Employee> emps;//list size could be millions
}
@Entity
public class Employee {
private String name;
//other fields with getters and setters
@OneToMany
private List<Cars> cars;// list size could be 1000
}
@Entity
public class Car {
private String name;
@ManyToOne
private Employee emp;
}
我想获得单个 Car 实体,但也有其他数据(国家、城市、员工),就像这样
1 个国家/地区/1 个城市/1 个员工/1 辆车(我选择了哪个 Id)
所以当我从 Country 加入 jpa 时,比如
select c from Country c
inner join c.cites ci
inner join ci.emps em
inner join ci.cars car where car.id = ?
我获得了国家(包括所有城市)的所有数据。
我应该怎么做才能获得 1 个国家、1 个城市、1 个员工、1 辆汽车。
如果用一个 jpa 查询不可能做到这一点,那么请建议其他方式。
所有关系都是双向和惰性的。
I tried this way.
1)select car by id. then get Id of employee from car.
2)select Employee by id - but at this point Employee data are nulls -
我认为是因为从 Car 到 Employee 的 ManyToOne 是懒惰的。你怎么看?
最佳答案
只需选择您想要的其他实体:
select c, ci, em, car from Country c
inner join c.cites ci
inner join ci.emps em
inner join ci.cars car where car.id = ?
或者,由于您的关联是双向的,因此选择汽车:它的员工将有一个 ManyToOne,它的城市将有一个 ManyToOne,它的国家将有一个 ManyToOne:
select car from Car car where car.id = ?
或者只是
em.find(Car.class, carId);
现在你可以做
car.getEmployee().getCity().getCountry()
关于java - 从 JPA 嵌套的 OneToMany 关系中获取单个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45185886/
我有如下三个实体相关:A(一对多) B(一对多) C我如何从 C 中获取基于 A->id 的所有记录?????? 最佳答案 类似于: $entityManager = $this->getDoctri
代码之间有什么区别? @LazyCollection(LazyCollectionOption.FALSE) @OneToMany(mappedBy = "siteesTypeSite", casca
public class ClientEntity { @Id @Column(name="id", nullable = false, unique = true) @Gen
现在,我明白这是不可能的,但我无法想象这是一个从未遇到过的问题。 我在不同的 PU 中有两个实体。我想从一个外键到另一个。我想知道谁已经遇到过这个问题,他们是否找到了一个好的解决方案? 最佳答案 Ec
我有两个与 OneToMany - ManyToOne 映射关联的类。当我选择父实体时,它也会选择子实体,但是分配给它的所有子实例都是每个父实例,而不是分配相关实例。 采购入口.java @Entit
我在 OneToMany 字段的持久性方面遇到了麻烦。这是两个简化的类 我是用来做测试的。 public class User implements Serializable { ... privat
我有一个持久化实体,它有一个 @OneToMany另一个实体的列表,我需要列表顺序才能由用户编辑,这非常有效。我可以完全重新排序内存中的 java 列表,当我 save() 对象时,链接表中的链接顺序
我是 Hibernate 的新手,正在尝试一些应该很容易的事情,但我无法让它工作。 有两个表,一个人和一个地址。一个人可以有一个或多个地址,即:一个 OneToMany 映射。当我尝试将相同的地址添加
我有双向、一对多和多对一的关系。说,一个公司有很多人,一个人有一个公司,所以,在公司中, @OneToMany(mappedBy = "company", fetch = FetchType.EAGE
在JPA中设置一对多关系时如何设置外键的列名? 我想将“items_id”的名称更改为“item_id” @OneToMany private List items; 我尝试了以下注释但没有成功: @
我有2张 table : 第一个是“人”: person_id, 人名 第二个是“PersonsGraphs”: person_id1, person_id2, 关系类型 我正在寻找一种建立“家谱”的
请帮我解决这个问题。我尝试了很多组合,但似乎没有任何效果。我正在尝试使用注释实现 hibernate 映射,但在保存我的父对象及其子对象期间,我注意到正在调用更新语句而不是插入语句。 我有两个彼此具有
我是 JPA 新手。假设我有这两个实体: //Imports @Entity @Table(name="article", schema = "sch_client") public class Ar
我有使用 Hibernate 归档 @ManyToOne 关系的代码。我有两个类 Package 和 Address。在 Address 中,我希望有唯一的条目,其中每个地址都与其他地址有所不同。然后
在订单表中插入时遇到此错误 org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to
我是 Hibernate 新手,我正在尝试在 Person 和 Vehicle 类之间建立 OneToMany/ManyToOne 双向关系。在我的示例中,一个人可以拥有许多车辆,而一辆车辆只属于一个
我们是 JPA 新手,尝试建立一个非常简单的一对多关系,其中名为 Message 的 pojo 可以具有由名为 GROUP_ASSOC 的联接表定义的整数组 id 列表。 。这是 DDL: CREAT
我在使用 JPA 时遇到了一些小问题。我有三个表(OTHER 表与此处无关,但我添加它只是为了解释为什么存在没有 PK 的表 USERS): ADDRESS id (PK) user_id (FK)
请帮我解决这个问题。我尝试了很多组合,但似乎没有任何效果。我正在尝试使用注释实现 hibernate 映射,但在保存我的父对象及其子对象期间,我注意到正在调用更新语句而不是插入语句。 我有两个彼此具有
我的 Task 实体表示为: @Entity @Getter @NoArgsConstructor public class Task { @Id @GeneratedValue(st
我是一名优秀的程序员,十分优秀!