Spring 数据 JPA/Hibernate - 使用 EntityManager 进行查询
Spring Data JPA/Hibernate - using EntityManager for queries
我有这些实体,我想从中获取包含来自它们的信息的元组列表:
应该如下所示:
+--------+-----+---------+
|name |login|user_type|
+--------+-----+---------+
| |admin|admin |
|John Doe|john |user |
|Jane Doe|janed|user |
|........|.....|.........|
问题是,我的 JPA 技能很生疏,我忘记了如何使用实体管理器。我知道如何进行基本的 JPQL 查询或内置的东西,但遗憾的是这对我来说还不够(因为我需要使用该列表来填充我的 UI 中的 table)。那么,我应该如何使用实体管理器呢? (如果我真的应该使用它的话;))
编辑:现在我认为使用DTO投影更好;这是我的映射 class
public class PersonUserMap {
private Integer personID;
private String name;
private String login;
private UserType userType;
public Integer getPersonID() {
return personID;
}
public String getName() {
return name;
}
public String getLogin() {
return login;
}
public UserType getUserType() { //custom Enum
return userType;
}
}
我在人物中的注释class:
@SqlResultSetMapping(
name = "PersonUserMapping",
classes = @ConstructorResult(
columns = { @ColumnResult(name = "personID", type=Integer.class),
@ColumnResult(name = "name"),
@ColumnResult(name = "login"),
@ColumnResult(name = "userType",type = UserType.class)},
targetClass = PersonUserMap.class))
并且当使用这样的本机查询时:Query q = entityManager.createNativeQuery("Select p.personid, p.first_name || ' ' || p.last_name as name, u.login, u.user_type from people p join users u on p.user_idusers = u.idusers","PersonUserMapping");
它抛出异常 Could not resolve column name in result set [userType]
感谢@Chris 的帮助,我终于有所收获;我的设置映射如下所示:
@SqlResultSetMapping(
name = "PersonUserMapping",
classes = @ConstructorResult(
columns = { @ColumnResult(name = "personid", type=Integer.class),
@ColumnResult(name = "name"),
@ColumnResult(name = "login"),
@ColumnResult(name = "user_type",type = UserType.class)},
targetClass = PersonUserMap.class))
我的查询如下所示
Query q = entityManager.createNativeQuery("select personid, concat(first_name, ' ', last_name) as 'name', users.login, users.user_type from aspirejestracja.people"
+ " full join aspirejestracja.users on user_idusers = users.idusers ", "PersonUserMapping");
现在我可以显示我需要的所有用户:)
我有这些实体,我想从中获取包含来自它们的信息的元组列表:
应该如下所示:
+--------+-----+---------+
|name |login|user_type|
+--------+-----+---------+
| |admin|admin |
|John Doe|john |user |
|Jane Doe|janed|user |
|........|.....|.........|
问题是,我的 JPA 技能很生疏,我忘记了如何使用实体管理器。我知道如何进行基本的 JPQL 查询或内置的东西,但遗憾的是这对我来说还不够(因为我需要使用该列表来填充我的 UI 中的 table)。那么,我应该如何使用实体管理器呢? (如果我真的应该使用它的话;))
编辑:现在我认为使用DTO投影更好;这是我的映射 class
public class PersonUserMap {
private Integer personID;
private String name;
private String login;
private UserType userType;
public Integer getPersonID() {
return personID;
}
public String getName() {
return name;
}
public String getLogin() {
return login;
}
public UserType getUserType() { //custom Enum
return userType;
}
}
我在人物中的注释class:
@SqlResultSetMapping(
name = "PersonUserMapping",
classes = @ConstructorResult(
columns = { @ColumnResult(name = "personID", type=Integer.class),
@ColumnResult(name = "name"),
@ColumnResult(name = "login"),
@ColumnResult(name = "userType",type = UserType.class)},
targetClass = PersonUserMap.class))
并且当使用这样的本机查询时:Query q = entityManager.createNativeQuery("Select p.personid, p.first_name || ' ' || p.last_name as name, u.login, u.user_type from people p join users u on p.user_idusers = u.idusers","PersonUserMapping");
它抛出异常 Could not resolve column name in result set [userType]
感谢@Chris 的帮助,我终于有所收获;我的设置映射如下所示:
@SqlResultSetMapping(
name = "PersonUserMapping",
classes = @ConstructorResult(
columns = { @ColumnResult(name = "personid", type=Integer.class),
@ColumnResult(name = "name"),
@ColumnResult(name = "login"),
@ColumnResult(name = "user_type",type = UserType.class)},
targetClass = PersonUserMap.class))
我的查询如下所示
Query q = entityManager.createNativeQuery("select personid, concat(first_name, ' ', last_name) as 'name', users.login, users.user_type from aspirejestracja.people"
+ " full join aspirejestracja.users on user_idusers = users.idusers ", "PersonUserMapping");
现在我可以显示我需要的所有用户:)