我有关于在Java Spring上获取一些特定专栏的问题
我使用Java Spring Boot和GraphDB作为我的数据库,我使用Model和Repository访问数据库(例如:我有userRepository,我使用userRepository.FindAll,所有记录都在他们的所有字段中加载)。
对于Instance,我有一个包含列/字段的表:
@NodeEntity
public class User {
@GraphId
Long id;
@NotNull
@Indexed(unique=true)
private String username;
@NotNull
private String password;
@NotNull
private String firstName;
@NotNull
private String lastName;
@NotNull
private String role;
@NotNull
private Boolean active;
public User() {
}
public User(String username, String password, String firstName,
String lastName, String role, Boolean active) {
super();
this.username = username;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.role = role;
this.active = active;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public Boolean getActive() {
return active;
}
public void setActive(Boolean active) {
this.active = active;
}
}
使用这样的存储库:
public interface UserRepository extends GraphRepository<User> {
}
我没有放入UserRepository.FindAll,因为它已经在GraphRepository上实现了它
我不需要加载密码&#34;领域。如何加载记录而不加载&#34;密码&#34;场?
答案 0 :(得分:2)
使用注释为@QueryResult
的班级@QueryResult
public class UserSimple {
String username;
String role;
}
在UserRepository界面中:
@Query("MATCH (u:USER) RETURN u.username as username, u.role as role")
List<UserSimple> findAllSimple();
编辑:(评论中的问题答案)
这些使用@QueryResult注释的类很容易成为内部类,这完全取决于你。
要返回图形ID,请在return子句中使用ID(u) as id
。