我正在使用 JPA 2.1、Hibernate 4.1.3.Final 和 MySQL 5.5.37。如何将结果集映射到@Transient
集合?在我的实体中,我设置了
@Entity
@Table(name = "category", uniqueConstraints = { @UniqueConstraint(columnNames = { "NAME", "PD_ONLY" }) })
@SqlResultSetMapping(name = "categoryResults", classes = { @ConstructorResult(targetClass = org.mainco.subco.core.domain.Category.class, columns = {
@ColumnResult(name = "id"), @ColumnResult(name = "name"), @ColumnResult(name = "pd_only"),
@ColumnResult(name = "subjects") }) })
public class Category implements Serializable, Comparable<Category>
{
@Id
@Column(name = "ID")
@GeneratedValue(generator = "uuid-strategy")
private String id;
private String name;
@Column(name = "PD_ONLY")
private Boolean pdOnly;
@Transient
private Set<Subject> subjects;
然后我尝试使用本机查询来映射事物……</p>
final Query q = m_entityManager.createNativeQuery(
"select distinct c.id, " +
" c.name, " +
" c.pd_only, " +
" e.subject_id FROM category c, " +
" resource_category rc, " +
" product_resource pr, " +
" product p, " +
" product_ebook pe, " +
" book e where c.pd_only = :pdOnly and c.id = rc.category_id and rc.resource_id = pr.resource_id and pr.product_id = p.id and p.id = pe.product_id and pe.ebook_id = e.id and c.id = 'Curriculum Maps';",
"categoryResults")
.setParameter("pdOnly", pdOnly ? "1" : "0");
return q.getResultList();
但我得到以下异常......</p>
javax.persistence.PersistenceException: org.hibernate.HibernateException: Could not resolve column name in result set [subjects]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1166)
at org.hibernate.loader.custom.JdbcResultMetadata.resolveColumnPosition(JdbcResultMetadata.java:67)
at org.hibernate.loader.custom.ScalarResultColumnProcessor.performDiscovery(ScalarResultColumnProcessor.java:59)
at org.hibernate.loader.custom.ConstructorResultColumnProcessor.performDiscovery(ConstructorResultColumnProcessor.java:58)
at org.hibernate.loader.custom.CustomLoader.autoDiscoverTypes(CustomLoader.java:498)
at org.hibernate.loader.Loader.getResultSet(Loader.java:2073)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1862)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1838)
at org.hibernate.loader.Loader.doQuery(Loader.java:909)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
at org.hibernate.loader.Loader.doList(Loader.java:2553)
at org.hibernate.loader.Loader.doList(Loader.java:2539)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2369)
at org.hibernate.loader.Loader.list(Loader.java:2364)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:353)
at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1873)
at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:311)
at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:141)
at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573)
at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:449)
at org.mainco.subco.core.repo.CategoryDaoImpl.findCategories(CategoryDaoImpl.java:78)
at org.mainco.subco.core.repo.CategoryDaoImpl.findNonPDCategories(CategoryDaoImpl.java:38)
at org.mainco.subco.core.repo.CategoryDaoIT.testFindNonPDCategories(CategoryDaoIT.java:30)
关于如何将从我的本机查询返回的内容映射到结果集中的任何想法?我也愿意不使用本机 SQL 并使用 CriteriaBuilder/JPQL 做一些事情。