I have the following piece of code:
Code:
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 getSession().merge();
}
catch (Exception e) {
returnCode = -1;
LOGGER.error("Exception during payment - {}", e.getMessage(), e);
}
return returnCode;
}
Now when the above method completes, hibernate performs certain
redundant update queries as described below:
Let's say I add 2 items to cart, so...
Code:
DEBUG PaymentService - Number of items in cart - 2
Next, a row is inserted in
RENTALS table (
expected)
Code:
insert into rentals(bookingDate, userID, addressID, rentalID) values(?, ?, ?, ?)
USER and
ADDRESS table updated once since 1 row inserted in RENTALS (
redundant)
Code:
update users set userName=?, password=?, firstName=?, lastName=?, email=?, addressID=? where userID=?
update addresses set line1=?, line2=?, city=?, state=?, pincode=? where addressID=?
MOVIES table updated as many times as the number of items in cart (2 in this case, again
redundant)
Code:
update movies set movieName=?, releaseYear=?, rating=?, nCopies=?, imageUri=?, plotLine=? where movieID=?
/* And another query with different movieID */
2 records inserted in
RENTED_MOVIE table (
expected)
Code:
insert into rented_movie(rentalID, returnDate, movieID) values(?, ?, ?)
/* And one more with same rentalID and diff. movieID */
And last, but not the least, a
select query to RENTALS table (Why??)
What is the reason for the above mentioned redundant queries?
I am using Hibernate 4.2.1 with MySQL 5.5
My schema diagram is as follows:
User.hbm.xmlCode:
<class name="User" table="users" dynamic-insert="true" dynamic-update="true">
<id column="userID" name="userID" type="string">
<generator class="com.clixflix.generators.EntityIDGenerator" />
</id>
<property name="userName" column="userName" type="string" not-null="true" length="15" />
<property name="encrPass" column="password" type="string" not-null="true" />
<property name="firstName" column="firstName" type="string" not-null="true" length="15" />
<property name="lastName" column="lastName" type="string" length="20" />
<property name="email" column="email" type="string" length="45" />
<!-- 1:1 mapping with Address table. -->
<many-to-one name="address"
column="addressID"
class="Address"
cascade="all"
unique="true"
not-null="false"
not-found="ignore"/> <!-- Address for a User is optional. So ignore if not found. -->
<!-- 1:n mapping with Rental table. -->
<set name="rentals" table="rentals" inverse="true" cascade="all" lazy="true">
<key column="userID" not-null="true"/>
<one-to-many class="Rental"/>
</set>
</class>
Rental.hbm.xmlCode:
<class name="Rental" table="rentals" dynamic-insert="true" dynamic-update="true">
<id name="rentalID" column="rentalID" type="string">
<generator class="assigned"/>
</id>
<property name="bookingDate" column="bookingDate" type="date" not-null="true"/>
<!-- n:1 mapping with User -->
<many-to-one name="user"
column="userID"
class="User"
not-null="true"
fetch="join"
cascade="all"/>
<!-- 1:1 mapping with Address -->
<many-to-one name="address"
column="addressID"
class="Address"
cascade="all"
unique="true"
not-null="true"/>
<!-- m:n mapping with Movie -->
<set name="rentedMovies" table="rented_movie" lazy="true" cascade="all">
<key column="rentalID"/>
<composite-element class="RentedMovie">
<property name="returnDate" column="returnDate" type="date" not-null="true"/>
<many-to-one name="movie" column="movieID" class="Movie" not-null="true" cascade="all"/>
</composite-element>
</set>
</class>