'Spring Data JPA: update a list of Entity using a list of input in a custom Query
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 ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|