[英]How to provide values from a class which is not an entity to a repository in spring data
I have a doubt about Spring Data and Spring Repositories. 我对Spring数据和Spring存储库有疑问。 I need to provide some values to a CrudRepository from another class which is not an Entity.
我需要从不是实体的另一个类向CrudRepository提供一些值。 For example: I Have a class
例如:我上课
@Entity
class Profile {
private String id;
private String name;
private long birthDate;
private String aboutMe;
...
}
and 和
class MyProfile {
private String profileId;
private String accountId;
private String name;
private String aboutMe;
}
and a Repository 和一个仓库
@Transactional
@EnableTransactionManagement
@Repository
public interface ProfileRepository extends CrudRepository<Profile, String>{
@Transactional
Profile findByAccountId(String id);
void updateMyProfile(MyProfile myProfile);
}
and I would like to update only some fields from Profile using data provided in MyProfile. 并且我只想使用MyProfile中提供的数据来更新Profile中的某些字段。 There is a way to do this?
有办法吗?
Thanks!! 谢谢!!
You will have to write the update query manually: 您将必须手动编写更新查询:
@Query("UPDATE Profile p" +
" SET " +
" p.name = ?#{#myprofile.name}, " +
" p.aboutMe = ?#{#myprofile.aboutMe}" +
" p.account.id = ?#{#myprofile.accountId}" + // I assume that account is another entity
" WHERE p.id = ?#{#myprofile.profileId}")
@Transactional
@Modifying
void updateMyProfile(@Param("myprofile") MyProfile myprofile);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.