gpt4 book ai didi

java - Hibernate5 update only collection 多对多关系

转载 作者:搜寻专家 更新时间:2023-11-01 03:32:54 25 4
gpt4 key购买 nike

我用 Hibernate 5.2 映射了下图

Diagram Database

这些是我的实体类:

股票

@Entity
@Table(name = "stock", catalog = "mkyongdb", uniqueConstraints = {
@UniqueConstraint(columnNames = "STOCK_NAME"),
@UniqueConstraint(columnNames = "STOCK_CODE") })
public class Stock implements java.io.Serializable {

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "STOCK_ID", unique = true, nullable = false)
private Integer stockId;

@Column(name = "STOCK_CODE", unique = true, nullable = false, length = 10)
private String stockCode;

@Column(name = "STOCK_NAME", unique = true, nullable = false, length = 20)
private String stockName;

@ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.REFRESH})
@JoinTable(name = "stock_category", catalog = "mkyongdb", joinColumns = {
@JoinColumn(name = "STOCK_ID", nullable = false, updatable = false) },
inverseJoinColumns = { @JoinColumn(name = "CATEGORY_ID",
nullable = false, updatable = false) })
private Set<Category> categories = new HashSet<>();

//Getter and Setter

}

类别

@Entity
@Table(name = "category", catalog = "mkyongdb")
public class Category implements java.io.Serializable {

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "CATEGORY_ID", unique = true, nullable = false)
private Integer categoryId;

@Column(name = "NAME", nullable = false, length = 10)
private String name;

@Column(name = "[DESC]", nullable = false)
private String desc;

@ManyToMany(fetch = FetchType.LAZY, mappedBy = "categories", cascade = {CascadeType.MERGE, CascadeType.REFRESH})
private Set<Stock> stocks = new HashSet<>();

//Getter and Setter

}

一切正常。

我需要做的只是为一只股票添加一个类别列表。我不想修改 Stock 实体,只是在 stock_category 表中添加或删除类别。

这是我的服务:

@Override
@Transactional(rollbackFor = StockException.class)
public void addCategoriesToStock(Set<Category> categories, Stock stock) throws StockException{
stock = sessionFactory.getCurrentSession().get(Stock.class, stock.getCodStock());
stock.setCategories(categories);
sessionFactory.getCurrentSession().update(stock);
sessionFactory.getCurrentSession().flush();
}

这是对服务的测试

@Test
public void testAddCategoriesStock() throws Exception {
Stock newValues = new Stock();
newValues.setStockId(1);
Category category = new Category();
category.setCategoryId(13);
dao.addCategoriesToStock(new HashSet<>(Arrays.asList(category)), newValues);
List<Stock> stocks = dao.getAllStockeByCategoriesCriteria(category);
for (Stock stock : stocks) {
System.out.println(stock);
}
}

测试运行良好,没有错误,但是当浏览与类别“13”(getAllStockeByCategoriesCriteria)关联的股票时,它没有给我带来任何股票。那么之前执行的操作没有生效。

如何添加或删除类别?

最佳答案

您必须在两侧添加依赖项,因此您缺少将 Stock 添加到 categories 中:

@Override
@Transactional(rollbackFor = StockException.class)
public void addCategoriesToStock(List<Category> categories, Stock stock) throws StockException{
stock = sessionFactory.getCurrentSession().get(Stock.class, stock.getCodStock());
stock.setCategories(categories);

for(Category cat: categories){
cat.getStocks().add(stock);
}

sessionFactory.getCurrentSession().merge(stock);
sessionFactory.getCurrentSession().flush();
}

此外,我认为您应该使用 merge 而不是 update 来使级联工作。

之后您的代码应保存关系。

关于java - Hibernate5 update only collection 多对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43523056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com