我使用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!
答案 0 :(得分:0)
我使用SPEL来调用getter而不是直接依赖于该字段。
public interface ProfileProjection {
@Value("#{target.primaryEmailAddress.emailAddress}")
String getEmailAddress();
String getPrimaryEmailAddress();
String getUsername();
String getDisplayName();
@Value("#{target.getGravatarUrl()}")
String getGravatarUrl();
}