- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下使用 Spring JPA 进行多对多关系持久化的代码。这似乎第一次有效,但在随后的保存中却失败了。
@Entity
public class ProductCategory {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
@NotNull
@Column(unique = true)
private String name;
@OneToMany(mappedBy = "productCategory", cascade = CascadeType.ALL)
private Set<ProductCategoryToProductAttributeRel> productCategoryToProductAttributeRel = new HashSet<ProductCategoryToProductAttributeRel>();
}
和 rel 类(此处添加了一个新类,因为我需要在此表上添加其他列
@Entity
public class ProductCategoryToProductAttributeRel implements Serializable{
private static final long serialVersionUID = 1L;
private Integer id;
@Id
@ManyToOne
@JoinColumn(name = "product_category_id")
private ProductCategory productCategory;
@Id
@ManyToOne
@JoinColumn(name = "product_attribute_id")
private ProductAttribute productAttribute;
}
和类别
@Entity
public class ProductAttribute {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
@NotNull
private String name;
}
以及要保存的代码
@RequestMapping(value = "/saveProductCategory", method = RequestMethod.POST)
public String ProductCategorySave(ProductCategoryDTO productCategoryDTO, Model model) {
ProductCategory pc = new ProductCategory(productCategoryDTO);
if (productCategoryDTO.getProductAttributeIds() != null
&& productCategoryDTO.getProductAttributeIds().size() > 0) {
Iterator<ProductCategoryToProductAttributeRel> it =
pc.getProductCategoryToProductAttributeRel().iterator();
// removing elements
while (it.hasNext()) {
boolean isPresent = false;
for (Integer paId : productCategoryDTO.getProductAttributeIds()) {
if (it.next().getProductAttribute().getId().equals(paId)) {
isPresent = true; break;
}
}
if (!isPresent) { it.remove(); }
}
for (Integer paId : productCategoryDTO.getProductAttributeIds()) {
ProductAttribute pa = productAttributeRepository.findOne(paId);
boolean add = true;
for (ProductCategoryToProductAttributeRel pcToPARel :
pc.getProductCategoryToProductAttributeRel()) {
if (pcToPARel.getProductAttribute().getId().equals(paId)) {
add = false;
}
}
if (add) {
ProductCategoryToProductAttributeRel pcToPARel = new ProductCategoryToProductAttributeRel();
pcToPARel.setProductAttribute(pa);
pcToPARel.setProductCategory(pc);
pc.getProductCategoryToProductAttributeRel().add(pcToPARel);
}
}
}
productCateogryRepository.save(pc);
return "edit-product-category";
}
第一次调用保存似乎工作正常并按预期更新表。然而,当我尝试为 rel 表添加其他行时,第二次调用保存时会使用其他要保存的项目,似乎失败并出现错误
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'product_category_id' cannot be null
我在这里做错了什么
最佳答案
ProductCategory
中删除了已保存的 ProductCategoryToProductAttributeRel
记录关联。ProductCategoryToProductAttributeRel.productCategory
,但由于是主键的一部分而无法设置。ProductCategory
的关联时对 Rel 实体进行删除。如果您使用的是 JPA 2.0,请尝试使用 orphanRemoval=true
)
@OneToMany(mappedBy = "productCategory", orphanRemoval=true, cascade = CascadeType.ALL)
private Set<ProductCategoryToProductAttributeRel> productCategoryToProductAttributeRel = new HashSet<ProductCategoryToProductAttributeRel>();
下面可能是我注意到的另一个错误
<小时/>将您的 it.next()
放在 for 循环之外,因为我认为您不希望它每次 while 迭代都会增加多次。
即将 while block 替换为如下所示的内容。
// removing elements
while (it.hasNext()) {
boolean isPresent = false;
ProductCategoryToProductAttributeRel nextRel = it.next() // I think you wanted to increment once per while loop?
for (Integer paId : productCategoryDTO.getProductAttributeIds()) {
if (nextRel.getProductAttribute().getId().equals(paId)) {
isPresent = true; break;
}
}
if (!isPresent) { it.remove(); }
}
这可能是它第一次工作的原因(因为 while 循环第一次可能为空)。
关于java - Spring JPA 多对多持久性问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48733158/
我想知道将内存中的树结构存储为用于持久性目的的目录树的实用性。在我的例子中,他的目标文件系统将是 ZFS,一旦创建了结构,它将很少被多个进程访问。 使用目录树作为数据树持久化机制的性能如何? 最佳答案
我已经创建了 docker private registry 并且能够从同一网络中的其他 raspi 推送和拉取(将 registry ip 添加到 insecure-registry 选项) doc
我正在尝试构建我的第一个“真正的”Haskell 应用程序,一个使用 Servant 的 API,我在其中使用 Persistent 作为数据库后端。但是我在尝试使用 Persistent 进行某种类
我已经在 stackoverflow 上看到了 ASP.NET MVC C# 中的持久性 cookie 示例。但我不明白为什么下面的代码不起作用。 首先我写入cookie: HttpCookie co
我是 Java Persistence API 的新手.我刚学会它,现在想在我的 Java 桌面应用程序中使用它。但我有以下问题: Q1。哪个 JPA 实现的大小最小(因为我希望我的应用程序的大小尽可
我正在尝试按照 android 蓝图指南将 MVP 模式实现到我的项目中。目前,我将用户(实体数据)保存在我的 Presenter 中,我真的不喜欢这样做,因为在每次配置更改时,presenter 都
我需要一个类似 map 的数据结构(在 C++ 中)来存储具有以下功能的对 (Key,T): 可以在当前结构中插入新元素(Key,T) 可以在当前结构中根据Key搜索元素 您可以制作当前版本结构的“快
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
我的配置 MBean 的持久性存在问题。我的配置: @ManagedResource(objectName = "pl.mobile
docker 中的 RabbitMQ 在删除没有卷的容器后丢失数据。 我的 Dockerfile: FROM rabbitmq:3-management ENV RABBITMQ_HIPE_COMPI
我正在尝试编写一个类型类,以简化使用持久性、aeson 和 scotty 编写 CRUD 后端 这是我的想法: runDB x = liftIO $ do info CRUD a where
当我尝试使用 持久化对象时遇到问题多线程 . 详情: 假设我有一个对象 PaymentOrder其中有一个列表 PaymentGroup (一对多关系)和 PaymentGroup包含 CreditT
我想使用纯功能数据结构和以下操作来实现环形缓冲区 通过索引进行高效随机访问 添加到前面 从背面移除 使用持久数据结构的原因是因为我有一个写入器线程和多个读取器线程,并且我想避免读取器阻塞写入器。 这可
persistence.xml 中关于 derby 客户端驱动程序的属性声明应该是什么?例如: 当我想从 Java 更新数据库时,我不断收到下面的异常。 实际上我需要客户端驱动程序而不是 Embed
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 9 年前。 Improve this ques
我希望用户即使关闭浏览器也不必登录。我的 cookie 将在一个月后过期。 用户登录成功 $session = CGI::Session->new (undef, undef, {Directory
我正在与 JBOss 作斗争,并且遇到了一个似乎很难解决的问题,但事实证明我不能。当我尝试部署一个简单的 Java Web 应用程序时,遇到异常: org.hibernate.ejb.Hibernat
当使用具有持久性的 Workflow Foundation 时,我们想删除一些(或全部)工作流,这些工作流不再应该被持久化/运行。 我可以使用任何脚本/工具吗? 最佳答案 没有可用于执行此操作的标准
我正在使用 JPA,并且我正在为所有引用实体使用二级缓存。一切正常,我可以从二级缓存中获取实体,因为它们之前已经被选中。 现在,我有两个应用程序,它们都使用相同的数据库(因此它们都使用相同的表、值等)
所以我正在制作一个 spring-boot 应用程序,并且从外部库导入一些数据模型(通过 maven 导入)。我有一些模型,它有一个外部库类型的字段,我希望能够保留它。像这样的东西: package
我是一名优秀的程序员,十分优秀!