简体   繁体   English

如何将不是实体的类的值提供给Spring数据中的存储库

[英]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.

相关问题 如何从实体类 spring boot 生成 CRUD 存储库类 - How to generate CRUD repository class from entity class spring boot 如何在 Spring Boot 中实现通用 JPA 存储库 - 它可以自动装配到任何实体/类类型的 spring 服务中 - How to implement Generic JPA Repository in Spring Boot - Which can be autowired into spring services for any entity/class type Spring Data JPA存储库返回父类的实体 - Spring data JPA repository returns entity of parent class Spring Data-Spring Repository:为类属性选择不同的值 - Spring Data - Spring Repository: selecting distinct values for a class attribute 对 Spring Data Mongo 存储库提供限制 - Provide limit on Spring Data Mongo repository Spring数据JPA存储库中的解析和查询构建哪个类? - Which class does the parsing and query building in spring data JPA repository? Spring Data - 从 Repository 获取实体名称/类型 - Spring Data - get entity name/type from Repository 如何从 Spring Boot 中的实体访问存储库? - How can I access the repository from the entity in Spring Boot? Spring 超级实体的存储库 class 从@MappedSuperClass 更改为@Inheritance 时未获取所有子类实体类型 - Spring repository for super entity class not fetching all subclass entity types when changed from @MappedSuperClass to @Inheritance 在Spring Data Repository中转义值 - escaping values in Spring Data Repository
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM