英文:
Spring Data JPA: update a list of Entity using a list of input in a custom Query
问题
我有以下基本对象:
/* 用户类 */
import lombok.Data;
import lombok.AllArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.Id
import javax.persistence.GeneratedValue;
import javax.persistence.ManyToOne;
@Data
@Entity
@AllArgsConstructor
public class User {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String name;
private String surname;
@ManyToOne
private Address address;
}
/* UserComposite 类,用于从用户加载部分数据 */
import lombok.Data;
import lombok.AllArgsConstructor;
@Data
@AllArgsConstructor
public class UserComposite {
private Long userId;
private String name;
private String surname;
}
我的目标是使用 UserComposite 列表作为输入来更新 User 列表。这是我的 DAO 的样子:
/* UserDao 类 */
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;
public interface UserDao extends JpaRepository<User, Long> {
@Transactional
@Modifying
@Query("update User u set u.name = uc.name , "
+ "u.surname = uc.surname "
+ "where u.id = uc.userId "
+ "and UserComposite uc in (?1)")
void updateUserFromCompositeList(List
}
然而,这并不起作用。我很难将输入中的数据与我的数据库中保存的数据匹配,特别是因为 UserComposite 不是一个实体。
在这个问题上是否有解决方法?
英文:
I have the following basic objects :
/* User class */
import lombok.Data;
import lombok.AllArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.Id
import javax.persistence.GeneratedValue;
import javax.persistence.ManyToOne;
@Data
@Entity
@AllArgsConstructor
public class User {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String name;
private String surname;
@ManyToOne
private Address address;
}
/* UserComposite class, used to load partial data from a user */
import lombok.Data;
import lombok.AllArgsConstructor;
@Data
@AllArgsConstructor
public class UserComposite {
private Long userId;
private String name;
private String surname;
}
My goal here, is to update a list of User using a list of UserComposite as input. Here is what my DAO looks like :
/* UserDao class */
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;
public interface UserDao extends JpaRepository<User, Long> {
@Transactional
@Modifying
@Query("update User u set u.name = uc.name , "
+ "u.surname = uc.surname "
+ "where u.id = uc.userId "
+ "and UserComposite uc in (?1)")
void updateUserFromCompositeList(List<UserComposite> userCompositeList);
}
However, this does not work. I have a hard time matching the data from the input with the data saved in my database, especially as UserComposite is not an entity.
Is there a way around this problem ?
答案1
得分: 4
Update multiple rows with different ID
s values is not possible in one query.
Update every user by id
for (UserComposite user : List<UserComposite> userCompositeList) {
userDao.updateUser(user.getUserId(), user.getName(), user.getSurname());
}
Repository query like
@Modifying
@Query("UPDATE User u SET u.name = :name, u.surname = :surname WHERE u.id = :userId")
void updateUser(@Param("userId") int userId, @Param("name") String name, @Param("surname") String surname);
英文:
Update multiple rows with different ID
s values is not possible in one query.
Update every user by id
for (UserComposite user : List<UserComposite> userCompositeList) {
userDao.updateUser(user.getUserId(), user.getName(), user.getSurname());
}
Repository query like
@Modifying
@Query("UPDATE User u SET u.name = :name, u.surname = :surname WHERE u.id = :userId")
void updateUser(@Param("userId") int userId, @Param("name") String name, @Param("surname") String surname);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论