Maxim  Bartkov Maxim Bartkov - 4 months ago 20
Java Question

how to send it all one request in Hibernate ? (HQL)

getSession()
.createQuery(
"UPDATE Friend SET status = :status, actionUserId = :userId, lastModifyDate = :lastModifyDate WHERE userId = :userId AND friendId = :idUserFriend")
.setParameter("status", Integer.valueOf(InviteFriendState.CANCEL.getState()))
.setParameter("userId", userId).setParameter("idUserFriend", idUserFriend)
.setParameter("lastModifyDate", new Date()).executeUpdate();
}getSession()
.createQuery(
"UPDATE Friend SET status = :status, actionUserId = :userId, lastModifyDate = :lastModifyDate WHERE userId = :idUserFriend AND friendId = :userId ")
.setParameter("status", Integer.valueOf(InviteFriendState.CANCEL.getState()))
.setParameter("userId", userId).setParameter("idUserFriend", idUserFriend)
.setParameter("lastModifyDate", new Date()).executeUpdate();
}`


how to send it all one request in Hibernate ? (HQL) how to combine a request?

Answer

Why doing two requests ?
You can do the same thing with a single query by adding an OR condition :

getSession()
            .createQuery(
                    "UPDATE Friend SET status = :status, actionUserId = :userId, lastModifyDate = :lastModifyDate 
              WHERE (userId = :userId AND friendId = :idUserFriend)
              ||    (userId = :idUserFriend AND friendId = :userId)  ")
            .setParameter("status", Integer.valueOf(InviteFriendState.CANCEL.getState()))
            .setParameter("userId", userId).setParameter("idUserFriend", idUserFriend)
            .setParameter("lastModifyDate", new Date()).executeUpdate();
}