繁体   English   中英

如何将Spring Data Projection与Transient属性一起使用?

[英]How can I use Spring Data Projection with a Transient property?

我正在将Spring Data与MongoDB和一个存储库之一的投影接口一起使用。
问题是我实体上的瞬态字段不被视为模型上的有效属性,因为它不是查询结果的一部分。 [假设]

有没有解决方法?

范例-

// Entity class fields 
@JsonProperty(access=Access.WRITE_ONLY)
private String password;
@Id
private String username;
private String displayName;
@DBRef
private Set<EmailAddress> emailAddresses = new HashSet<>();
@DBRef
@NotEmpty
private EmailAddress primaryEmailAddress;
private boolean hidePrimaryEmailAddress;
private String firstName;
private String lastName;
@JsonTypeInfo(use = com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NONE)
private Set<Role> authorities;
private boolean accountNonExpired;
private boolean accountNonLocked;
private boolean credentialsNonExpired;
private boolean enabled;
@Transient
private String gravatarUrl;

.....

// Spring Data Projection 
public interface ProfileProjection {
  @Value("#{target.primaryEmailAddress.emailAddress}")
  String getEmailAddress();
  String getUsername();
  String getDisplayName();
  String getGravatarUrl();
}  

// Repository
public interface UserRepository extends ExtendedMongoRepository<SproutUser, String> {
    ProfileProjection findFirstByUsername(String username);
}  

例外-

No property gravatarUrl found on savantly.sprout.domain.SproutUser!

我使用SPEL调用吸气剂,而不是直接依赖于字段。

public interface ProfileProjection {
    @Value("#{target.primaryEmailAddress.emailAddress}")
    String getEmailAddress();
    String getPrimaryEmailAddress();
    String getUsername();
    String getDisplayName();
    @Value("#{target.getGravatarUrl()}")
    String getGravatarUrl();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM