gpt4 book ai didi

java - 当这些表的值没有更改时,为什么 hibernate 会执行冗余更新语句?

转载 作者:行者123 更新时间:2023-11-29 13:49:32 24 4
gpt4 key购买 nike

我有以下代码:

public int makePayment(User forUser, Collection<Movie> cartItems, 
Address deliverTo)
{
int returnCode = 0;

LOGGER.debug("Number of items in cart - {}", cartItems.size());

Rental rental = new Rental();
rental.setRentalID(RandomStringUtils.randomAlphanumeric(ID_LENGTH).toUpperCase());
rental.setUser(forUser); // Update reference to parent
rental.setAddress(deliverTo);

// Each rental has its own set of rented movies
Set<RentedMovie> rentedMovies = new HashSet<>(cartItems.size());
for (Iterator<Movie> iterator = cartItems.iterator(); iterator.hasNext();)
{
Movie movie = iterator.next();
RentedMovie rentedMovie = new RentedMovie();
rentedMovie.setMovie(movie);
rentedMovie.setRental(rental); // Update reference to parent
rentedMovies.add(rentedMovie);
}

rental.setRentedMovies(rentedMovies);
rentalDAO.create(rental);

Set<Rental> userRentals = userDAO.findById(forUser.getUserID()).getRentals();
userRentals.add(rental); // Add to existing set of rentals

try
{
String addressID = addressDAO.create(deliverTo);
LOGGER.debug("Delivery Address successfully inserted. Generated address ID - {}", addressID);

forUser.setRentals(userRentals);
userDAO.update(forUser); // Internally calls session.merge()
}
catch (Exception e) {
returnCode = -1;
LOGGER.error("Exception during payment - {}", e.getMessage(), e);
}

return returnCode;
}

现在,当上述方法完成时,hibernate 会执行某些冗余更新查询,如下所述:

假设我向购物车添加了 2 件商品,所以...

DEBUG PaymentService - Number of items in cart - 2

接下来,在 RENTALS 表中插入一行(预期)

insert 
into
rentals
(bookingDate, userID, addressID, rentalID)
values
(?, ?, ?, ?)

USERADDRESS 表自在 RENTALS 中插入 1 行后更新一次(冗余)

update
users
set
userName=?,
password=?,
firstName=?,
lastName=?,
email=?,
addressID=?
where
userID=?

update
addresses
set
line1=?,
line2=?,
city=?,
state=?,
pincode=?
where
addressID=?

MOVIES 表更新次数与购物车中的商品数量相同(本例中为 2 次,同样是多余的)

update
movies
set
movieName=?,
releaseYear=?,
rating=?,
nCopies=?,
imageUri=?,
plotLine=?
where
movieID=?

/* And another query with different movieID */

RENTED_MOVIE 表中插入 2 条记录(预期)

insert 
into
rented_movie
(rentalID, returnDate, movieID)
values
(?, ?, ?)

/* And one more with same rentalID and diff. movieID */

最后但并非最不重要的一点是,对 RENTALS 表进行选择查询(为什么??)

上述冗余查询的原因是什么?

我正在使用 Hibernate 4.2.1 和 MySQL 5.5

<小时/>

链接到Schema Diagram

链接到User.hbm.xml

链接到Rental.hbm.xml

<小时/>

更新:似乎无论我尝试什么可能的组合,我似乎都无法避免这些n+2更新(n =购物车):(

有人可以告诉我这是否是已知行为吗?如果是,可能的解决方法是什么?

最佳答案

我终于解决了我的问题。问题在于级联设置。我已将其设置为 cascasde="all"在所有协会中。

我对 Rental.hbm.xml 进行了更改为关联设置特定级联选项:

<!-- n:1 mapping with User -->
<many-to-one name="user"
column="userID"
class="User"
not-null="true"
fetch="join"
cascade="none" /> <!-- No cascading required for User -->

<!-- 1:1 mapping with Address. Association is MANDATORY. -->
<many-to-one name="address"
column="addressID"
class="Address"
unique="true"
not-null="true"
cascade="persist"/> <!-- Either create a new delivery address or use an existing user address -->

<!-- m:n mapping with Movie -->
<set name="rentedMovies" table="rented_movie" lazy="true"
cascade="save-update, delete-orphan"> <!-- Selective update / delete -->
<key column="rentalID"/>
<composite-element class="RentedMovie">
<parent name="rental"/>
<property name="returnDate" column="returnDate" type="date" not-null="true"/>
<many-to-one name="movie" column="movieID" class="Movie" not-null="true" fetch="join"/>
</composite-element>
</set>

我从 <many-to-one name="movie" ../> 中删除了级联因为我通过传递 ID 列表来通过 HQL 更新库存计数,而不是更新对象本身的属性,因为后者会导致 N 个更新语句。

关于java - 当这些表的值没有更改时,为什么 hibernate 会执行冗余更新语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16876172/

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