- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想更新实体而不更新/编辑子实体。这是有问题的实体:
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
//other fields omitted for brevity
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "project_user",
joinColumns = @JoinColumn(name="user_id", updatable = false),
inverseJoinColumns = @JoinColumn(name="project_id",updatable = false))
@JsonIgnore
private Set<Project> projects;
//getters and setters omitted for brevity
@Override
public int hashCode() {
return id;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (id != other.id)
return false;
return true;
}
}
这是项目实体:
@Entity
@Table(name = "project")
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
//other fields omitted for brevity
@ManyToMany
@JoinTable(name = "project_user",
joinColumns = @JoinColumn(name="project_id"),
inverseJoinColumns = @JoinColumn(name="user_id"))
@JsonIgnoreProperties(value = {"projects", "connected", "notifications"})
private Set<User> members;
@Override
public int hashCode() {
return id;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Project other = (Project) obj;
if (id != other.getId())
return false;
return true;
}
}
这是合并过程:
@Override
public void update(User user) {
//user is created from a dto as a new object (that has its id set manually)
entityManager.merge(user);
}
问题是 hibernate 日志从连接表中删除。我怎样才能阻止它?
hibernate 日志
Hibernate: //login query
select
distinct authuser0_.id as id1_7_,
authuser0_.enabled as enabled8_7_,
authuser0_.password as password5_7_,
authuser0_.username as username6_7_,
roles1_.username as username1_0_0__,
roles1_.authority as authorit2_0_0__
from
users authuser0_
inner join
authorities roles1_
on authuser0_.username=roles1_.username
where
authuser0_.username=?
Hibernate: //entityManager.merge() calls this to merge it
select
user0_.id as id1_7_0_,
user0_.email as email2_7_0_,
user0_.first_name as first_na3_7_0_,
user0_.image as image7_7_0_,
user0_.last_name as last_nam4_7_0_,
user0_.password as password5_7_0_,
user0_.username as username6_7_0_
from
users user0_
where
user0_.id=?
Hibernate: //the actual update statement
update
users
set
email=?,
first_name=?,
image=?,
last_name=?,
password=?,
username=?
where
id=?
Hibernate: // not wanted, need to stop this query
delete
from
project_user
where
user_id=?
我在网上读到它与 equals 和 hashcode 有关。但是,我似乎无法让它发挥作用。
我知道两个选项都不是最佳的:
提前致谢
最佳答案
嗯,我知道您已经接受了答案,但是......
那里发生的事情比你想象的要多。
出现 Hibernate: delete from project_user where user_id=?
的原因是之后有插入。你没看到他们吗?如果该用户的关系表被清空但后来没有插入,那么关系可能没有更新?也只能重现删除操作,但您应该测试您的代码并验证该用户没有项目。这可能意味着您没有更新合并用户中的项目字段。
因此,运行一个基本的 JPA 示例,让我们从创建一个起点开始解决您的问题:
em.getTransaction().begin();
Project project = new Project();
em.persist(project);
User user = new User();
Set<Project> projects = new HashSet<>();
projects.add(project);
user.setProjects(projects);
em.persist(user);
em.getTransaction().commit();
然后,清除entityManager上下文:
em.clear();
然后重现您的问题:
em.getTransaction().begin();
Project p2 = new Project();
em.persist(p2);
projects.add(p2);
User un = em.find(User.class, 1);
un.setProjects(projects);
em.merge(un);
em.getTransaction().commit();
第二部分给我日志:
Hibernate: insert into project (id) values (default)
Hibernate: select user0_.id as id1_2_0_ from users user0_ where user0_.id=?
Hibernate: select project0_.id as id1_0_0_ from project project0_ where project0_.id=?
Hibernate: delete from project_user where user_id=?
Hibernate: insert into project_user (user_id, project_id) values (?, ?)
Hibernate: insert into project_user (user_id, project_id) values (?, ?)
现在让我们通过更改代码并再次运行来删除有问题的 delete
语句:
Project p2 = new Project();
em.persist(p2);
User un = em.find(User.class, 1);
projects = un.getProjects();
projects.add(p2);
em.merge(un);
em.getTransaction().commit();
这给了我日志:
Hibernate: insert into project (id) values (default)
Hibernate: select user0_.id as id1_2_0_ from users user0_ where user0_.id=?
Hibernate: select projects0_.user_id as user_id1_1_0_, projects0_.project_id as project_2_1_0_, project1_.id as id1_0_1_ from project_user projects0_ inner join project project1_ on projects0_.project_id=project1_.id where projects0_.user_id=?
Hibernate: insert into project_user (user_id, project_id) values (?, ?)
没有删除语句。仔细查看第一个“第二部分”,请注意我将新项目添加到了原始项目集中,该项目不再是 JPA 持久性上下文的一部分。在第二个示例中,我专门从属于 JPA 持久性上下文的 user
对象中检索了一组项目。现在,当我更新项目集并合并用户 JPA 时,不必重新开始处理用户/项目关系。
顺便说一句:就 mappedBy
逻辑而言,接受的答案是准确的,但此代码是根据您的原始示例完成的。只要 JPA 知道您在做什么,它就会正确处理它。当您在 JPA 之外执行操作时,JPA 将“下注”并重新创建。
关于mysql - 当我更新多对多关系中的实体时,如何阻止 Hibernate/JPA 删除连接表记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63690958/
我刚开始学习JSP技术,遇到了瓶颈。 如何从 JSP 声明 block ? 这不起作用: ... 服务器说没有“out”。 U: 我确实知道如何使用返回字符串的方法重写代码,但是有没有办法在 ?
在一个字段中,我想设置一个具有自定义过滤器的自定义分析器-着眼于词干-因此,“闪存卡”和“闪存卡”的词根相同,因此返回的结果相同 当我运行以下查询时,我的命中率很高,但是“闪存卡”和“闪存卡”各自返回
快速提问。 我有一个通过 PInvoke 使用 native DLL 的应用程序,这个 DLL 可能会调用 PostQuitMessage()。 如何避免? (因为我的应用程序不应该关闭) 我试过 A
一些给定的 HTML 文章,例如: Content 与一些基本的 Jquery 结合使用,例如: $(".some_
我正在构建一个灯箱相册。当第一个图像加载时,CSS 转换起作用。当加载后的每个图像都没有。任何想法为什么?加载第一张之后的照片,但没有过渡。 Image.prototype.load = functi
这个问题在这里已经有了答案: Disable recent tasks button on Android 5.0 (2 个答案) 关闭 2 年前。 我知道这个问题之前在这里被问过 Android
我是 Objective-C 的新手,我只是想弄清楚我是否可以使用 block 或选择器作为 UIAlertView 的 UIAlertViewDelegate 参数 - 哪个更合适? 我已经尝试了以
我是 Linux (UNIX) 套接字下套接字编程的新手。我在 Internet 上找到了以下代码,用于为每个连接生成一个线程的 tcp 服务器。但是它不起作用。accept() 函数立即返回,不等待
recv()库函数手册页提到: It returns the number of bytes received. It normally returns any data available, up
我有一个用于其他项目的共享 ts 库。在这个库中有被同一个库的其他资源使用的资源。该库的结构分为 components/*、interfaces/*、services/* 等目录。在每个目录的根目录中
我想在同一行中一个接一个地显示我的 ListView ,但 ListView 显示每个新行中的每个项目。我怎样才能防止换行显示。以便它显示为段落 ListView.builder( shr
我有一个包含数千行的表格。 import React from "react" import { useSelector } from "react-redux"; import { useEffec
假设我通常希望收到关于代码中不完整模式的警告,但有时我知道某个函数的模式不完整,我知道这很好。 是still true GHC 的警告粒度是每个模块的,并且没有办法更改有关特定功能或定义的警告? 最佳
我的网络应用程序发送浏览器通知,我知道如何检查通知的浏览器权限,以及如果未授予权限,如何请求权限。 但是,即使用户授予我的站点发送通知的权限,她可能仍然无法收到通知,因为它们 might be dis
我有 Xcode 3.2.1,并且喜欢使用它,但是当我编辑文本中带有超链接的文件时(例如,带有引用的注释:# see http://example.com)Xcode 将文本变成可点击的超链接。尝试编
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许在 Stack Overflow 上提出有关通用计算硬件和软件的问题。您可以编辑问题,使其成为
我有一个在 MY_Controller 中运行的 acl。如果权限被拒绝,那么此刻,我只是执行 redirect('denied') - 这是一个非常基本的 Controller ,它加载一个非常基本
我一直很好奇尝试从 Chrome 切换到 Firefox Quantum,但是对于 Web 开发遇到了一个我无法轻松解决的主要障碍——它正在缓存我的本地主机文件,因此当我尝试在本地主机加载各种 emb
这真的让我很兴奋!在任何时候,我都会参与多个项目。当我退出Xcode时,下次打开Xcode时,我前一天的所有项目都会自动一一打开。 经常我最终编辑错误的文件,AHHHHHHHHHHH!我可以阻止这种行
我的Wiki上有500个左右的Spambot和大约5个实际注册用户。我已经使用nuke删除了他们的页面,但是他们一直在重新发布。我已经使用reCaptcha控制了spambot的注册。现在,我只需要一
我是一名优秀的程序员,十分优秀!