- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在我的项目中结合使用 **JPA 2.1** 和 **Hibernate 5.0.2.final**。
问题是,当我加载类型为 {@EventTO} 的实例时,如果该实例一次没有与会者和 assignedResources 或这些集合之一,它将正确加载;但是,如果它同时拥有这两个集合,则实体图的提取结果将携带冗余数据。例如,如果有 3 个与会者和 2 个 assignedResources,作为线索,它将返回 4 个与会者和 3 个 assignedResources。我对这样的行为感到困惑。我不知道为什么???。可能与 hibernate 查询有关,但我不确定。
现在,如果有人能帮我找到解决这个问题的天才解决方案,我将不胜感激。
提到的实体以及调用 JPA 实体管理器的 find 方法的代码片段如下:
EventTO 实体:
@Entity
@Table(name = "CALT_EVENT")
@SequenceGenerator(name = "seq", sequenceName = "seq_cal_event", allocationSize = 1)
@NamedEntityGraph(name = "loadGraph",
attributeNodes = {
@NamedAttributeNode(value = "attendees", subgraph = "attendees"),
@NamedAttributeNode(value = "assignedResources", subgraph = "assignedResources")
},
subgraphs = {
@NamedSubgraph(name = "attendees", attributeNodes = {@NamedAttributeNode("departmentAttendee"), @NamedAttributeNode(value = "personAttendee")}),
@NamedSubgraph(name = "assignedResources", attributeNodes = @NamedAttributeNode("resourceTO"))
}
)
})
public class EventTO {
private List<EventAttendeeTO> attendees = new ArrayList<EventAttendeeTO>();
private List<EventAssignedResourceTO> assignedResources = new ArrayList<EventAssignedResourceTO>();
/**
* @return the attendeesTOList
*/
@OneToMany(mappedBy = "eventTO", cascade = CascadeType.ALL, orphanRemoval = true)
public List<EventAttendeeTO> getAttendees() {
return attendees;
}
/**
* @param attendees the attendeesTOList to set
*/
public void setAttendees(List<EventAttendeeTO> attendees) {
if (attendees != null) {
this.attendees = attendees;
}
}
/**
* @return the assignedResourceTOList
*/
@OneToMany(cascade = CascadeType.ALL, mappedBy = "eventTO", orphanRemoval = true)
public List<EventAssignedResourceTO> getAssignedResources() {
return assignedResources;
}
/**
* @param assignedResources the assignedResourceTOList to set
*/
public void setAssignedResources(List<EventAssignedResourceTO> assignedResources) {
if (assignedResources != null) {
this.assignedResources = assignedResources;
}
}
}
EventAttendeeTO 实体:
@Entity
@Table(name = "CALT_EVENT_ATTENDEES")
@SequenceGenerator(name = "seq", sequenceName = "seq_cal_event_attendees", allocationSize = 1)
public class EventAttendeeTO extends RecordableExtEntityTO {
private EventTO eventTO;
private PersonTO personAttendee;
private DepartmentTO departmentAttendee;
/**
* @return the eventTO
*/
@JSON(serialize = false)
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "EAT_EVN_ID", nullable = false)
public EventTO getEventTO() {
return eventTO;
}
/**
* @param eventTO
* the eventTO to set
*/
public void setEventTO(EventTO eventTO) {
this.eventTO = eventTO;
}
/**
* @return the attendee
*/
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "EAT_PER_ID")
public PersonTO getPersonAttendee() {
return personAttendee;
}
/**
* @param personAttendee
* the personAttendee to set
*/
public void setPersonAttendee(PersonTO personAttendee) {
this.personAttendee = personAttendee;
if (personAttendee != null) {
this.perId = personAttendee.getId();
}
}
}
EventAssignedResourceTO 实体:
@Entity
@Table(name = "CALT_EVENT_ASSIGNED_RESOURCE")
@SequenceGenerator(name = "seq", sequenceName = "seq_cal_event_assigned_res", allocationSize = 1)
public class EventAssignedResourceTO extends ExtEntityTO {
private EventTO eventTO;
private EventResourceTO resourceTO;
/**
* @return the eventTO
*/
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "EAR_EVN_ID", nullable = false)
public EventTO getEventTO() {
return eventTO;
}
/**
* @param eventTO
* the eventTO to set
*/
public void setEventTO(EventTO eventTO) {
this.eventTO = eventTO;
}
/**
* @return the resourceTO
*/
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "EAR_ERS_ID")
public EventResourceTO getResourceTO() {
return resourceTO;
}
/**
* @param resourceTO
* the resourceTO to set
*/
public void setResourceTO(EventResourceTO resourceTO) {
this.resourceTO = resourceTO;
}
}
通过hint方式调用entity manager的find方法的代码片段:
try {
EntityGraph graph = this.em.getEntityGraph("loadGraph");
Map hints = new HashMap();
hints.put("javax.persistence.fetchgraph", graph);
return em.find(EventTO.class, id, hints);
} catch (Exception e) {
new DAOException(e);
}
最佳答案
在修改我的代码并仔细思考这个问题之后,我想到了以下原因:
此问题可能与 hibernate 在同时加载多个集合方面的限制有关。由于我的 ORM 处于 hibernate 状态,因此在获取我的 TO 时可能会出现这样的问题。
更多详细信息:Hibernate 在一次查询中获取多个包时抛出以下异常:
org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags
也许使用 NamedGraph 会导致 hibernate 创建基于错误的外部左连接的查询,这会导致获取冗余或换句话说无效数据。
关于hibernate - JPA 2.1 通过提示查找,如果@NamedEntityGraph 中存在多个@NamedSubgraph,则返回冗余结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33263154/
我的 friend 编写了一个程序,它比较随机排列的骰子面,以找到分布最均匀的面——尤其是当面不仅仅是序列时。 我将他的程序翻译成 haskell 是因为我一直在寻找一个理由来让别人知道 haskel
我需要对表单中的某些字段进行评论/提示。我的想法是在模型中描述它,就像attributeLabels一样。我该怎么做? 然后它会是理想的,如果 Gii 模型(和 Crud)生成器直接从 mysql 列
我们使用 FastReport 来生成报告。事实上,我们为访问源代码付费。 我们目前使用的是 FastReport 的最新稳定版本。虽然它对于我们的生产来说足够稳定,但每当我编译时,我都会看到以下内容
我需要创建一个对话框/提示,包括用于用户输入的文本框。我的问题是,确认对话框后如何获取文本?通常我会为此创建一个类,将文本保存在属性中。不过我想使用 XAML 设计对话框。因此,我必须以某种方式扩展
我想提示用户是否要执行操作(删除) - 用警报框说"is"或“否”,如果是,则运行删除脚本,如果否,则不执行任何操作 我不太了解 javascript,因此是否有人可以使用 javascript 获得
所以我正在编写一个简单的 JS 代码。我们刚刚开始学习函数。我需要创建一个名为“printStars”的函数。 我需要从用户那里获取一个号码,并根据该号码打印“*”。 这就是我所做的:
我在我的页面上添加了一个提示,但它在页面加载之前加载了。如何仅在整个页面可见时才显示消息? 这是我的提示: if (name == null || name == "") { txt == "No
我在我的页面上添加了一个提示,但它在页面加载之前加载了。如何仅在整个页面可见时才显示消息? 这是我的提示: if (name == null || name == "") { txt == "No
我正在自定义我的 zsh 提示,并发现以下内容来检查是否有任何后台作业: if [[ $(jobs | wc -l) -gt 0 ]]; then # has background job(s)
这个问题在这里已经有了答案: JavaScript object: access variable property by name as string [duplicate] (3 个答案) pa
我正在尝试用 javascript 制作一个简单的数学练习程序。在提示警报中给出不同的值,并将答案与用户输入进行比较。这是代码: Calculations generate(); functio
在这段代码中,尽管我使用了文本对齐属性在“编辑文本” View 的中心设置“提示”。但它无法正常工作。 最佳答案 尝试 关于android - 如何在编辑文本的中心对齐文本(提示),我们在Sta
我正在尝试让我的 EditText 显示一个提示,例如“请在此处输入答案”,当用户点击 EditText 以键入他们的答案时,文本应该消失并留空,以便他们在其中输入答案. 截至目前,这就是我的 .xm
我当前的 android 应用程序中有两个微调器,我想要一个默认值,例如 editText 的 android:hint 功能。有没有办法这样做,但不会将提示添加到填充微调器的字符串数组。例如从微调器
如果我的表单已完全填写,我如何提示“感谢您填写表单,“name”!” function submit_onclick() { if(confirm("Thanks for completing t
我刚刚了解了prompt()命令;我知道 Prompt() 命令以字符串的形式返回用户输入。我正在搞乱下面的程序,我输入了Per“Dead”Ohlin作为男性名字。为什么这有效并且没有引起任何问题?
void openUpNow(FILE *x, FILE *y) { x = fopen("xwhatever", "r"); y = fopen("ywhatever", "r");
我有一个作业正在处理,但我在使用 prompt() 方法时遇到了问题。我看到我可以做一个提示,但我需要几个并且有数量。 例如... 我创建了一个 HTML 表格,其中包含许多艺术家和包含 DVD、CD
我正在学习 Big Nerd Ranch 的 iOS Programming, 2nd Edition,我已经来到第 4 章挑战:标题。该练习暗示我感到困惑;它说我需要做一些我认为不需要做的事情。 到
抱歉,如果这是微不足道的,但我没有找到任何解决此问题的建议。我在 Ubuntu 上,我的 Yii 项目需要 PHPUnit。我已经安装了 PHPUnit 两次,方法是下载 phpunit.phar 并
我是一名优秀的程序员,十分优秀!