我有两个用户和应用程序类。
用户有一对多映射到应用程序。用户有很多领域,有些领域很像描述。当我想用它来查看用户和应用程序之间的关系时,我不想加载用户的所有字段,因此我创建了一个具有用户名和应用程序集合的代理类。我怎么把这两个表放到这个班级?或者最佳做法是什么?
更新:
@Entity
public class UserProfile implements Serializable
{
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;
private String userName;
private String password;
@OneToMany
@JoinTable(name = "userapplicationlink", joinColumns = @JoinColumn(name = "userId"), inverseJoinColumns = @JoinColumn(name = "appId"))
private Collection<Application> applications;
}
@Entity
public class Application
{
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private int id;
private String name;
private String url;
}
main()
{
UserProfile user1 = new UserProfile();
user1.setUserName("sasd");
user1.setPassword("123");
Application app = new Application();
app.setName("User Application");
app.setPriority(1);
app.setUrl("/user.do");
app.setDescription("app");
user1.setApplications(new ArrayList());
user1.getApplications().add(app);
SessionFactory sessionFac = new Configuration().configure().buildSessionFactory();
Session session = sessionFac.openSession();
session.beginTransaction();
session.save(user1);
session.save(app);
session.getTransaction().commit();
System.out.println("End");
session.close();
sessionFac.close();
sessionFac = new Configuration().configure().buildSessionFactory();
session = sessionFac.openSession();
session.beginTransaction();
try
{
Query query = session.createQuery("SELECT NEW empire.erp.server.db.UserNameAndApplications(u.userName, u.applications) FROM UserProfile u JOIN FETCH u.applications");
List result = query.list();
session.getTransaction().commit();
session.close();
}
finally
{
session.close();
sessionFac.close();
}
}
堆栈跟踪
线程中的异常&#34; main&#34; org.hibernate.QueryException:查询指定的连接获取,但是获取的关联的所有者在选择列表中不存在[FromElement {显式,不是集合连接,获取连接,获取非延迟属性,classAlias = null,role = empire.erp.server.db.UserProfile.applications,tableName = Application,tableAlias = applicatio2_,origin = UserProfile userprofil0_,columns = {userprofil0_.id,className = empire.erp.server.db.Application}}] [SELECT NEW empire .erp.server.db.UserNameAndApplications(u.userName,u.applications)FROM empire.erp.server.db.UserProfile u JOIN FETCH u.applications] 在org.hibernate.QueryException.generateQueryException(QueryException.java:120) 在org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103) 在org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:218) 在org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142) 在org.hibernate.engine.query.spi.HQLQueryPlan。(HQLQueryPlan.java:115) 在org.hibernate.engine.query.spi.HQLQueryPlan。(HQLQueryPlan.java:76) 在org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150) at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:302) 在org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:240) 在org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1907) at empire.erp.server.db.UserProfile.main(UserProfile.java:216) 引起:org.hibernate.QueryException:查询指定的连接提取,但是提取的关联的所有者在选择列表中不存在[FromElement {显式,不是集合连接,获取连接,获取非延迟属性,classAlias = null ,role = empire.erp.server.db.UserProfile.applications,tableName = Application,tableAlias = applicatio2_,origin = UserProfile userprofil0_,columns = {userprofil0_.id,className = empire.erp.server.db.Application}}] 在org.hibernate.hql.internal.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:214) 在org.hibernate.hql.internal.ast.HqlSqlWalker.useSelectClause(HqlSqlWalker.java:991) 在org.hibernate.hql.internal.ast.HqlSqlWalker.processQuery(HqlSqlWalker.java:759) 在org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:675) 在org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:311) 在org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:259) 在org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:262) 在org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:190) ......还有8个
更新2:运行不带提取的查询
Hibernate:选择userprofil0_.userName作为col_0_0_ ,.作为col_1_0_来自UserProfile userprofil0_ inner join userapplicationlink applicatio1_ on userprofil0_.id = applicatio1_.userId inner join Application applicatio2_ on applicatio1_.appId = applicatio2_.id inner join userapplicationlink applicatio3_ on userprofil0_.id = applicatio3_.userId inner join Application applicatio4_ on applicatio3_.appId = applicatio4_.id 2016年5月7日下午4:17:21 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions 警告:SQL错误:0,SQLState:42601 2016年5月7日下午4:17:21 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions 错误:错误:语法错误在或附近&#34;。&#34; 位置:43 2016年5月7日下午4:17:21 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop 信息:HHH10001008:清理连接池[jdbc:postgresql:// localhost:5432 / optimalyou] 线程&#34; main&#34;中的例外情况org.hibernate.exception.SQLGrammarException:无法提取ResultSet 在org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106) 在org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42) 在org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97) at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:79) 在org.hibernate.loader.Loader.getResultSet(Loader.java:2115) 在org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1898) 在org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1874) 在org.hibernate.loader.Loader.doQuery(Loader.java:919) 在org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336) 在org.hibernate.loader.Loader.doList(Loader.java:2610) 在org.hibernate.loader.Loader.doList(Loader.java:2593) 在org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2422) 在org.hibernate.loader.Loader.list(Loader.java:2417) 在org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:501) 在org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:371) 在org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:216) 在org.hibernate.internal.SessionImpl.list(SessionImpl.java:1339) 在org.hibernate.internal.QueryImpl.list(QueryImpl.java:87) at empire.erp.server.db.UserProfile.main(UserProfile.java:217) 引起:org.postgresql.util.PSQLException:错误:语法错误在或附近&#34;。&#34; 位置:43 at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2284) at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2003) 在org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:200) 在org.postgresql.jdbc.PgStatement.execute(PgStatement.java:424) at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:161) 在org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:114) at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:70) ......还有15个
答案 0 :(得分:0)
必须为集合使用别名。不知道为什么。适当的HQL是
SELECT NEW empire.erp.server.db.UserNameAndApplications(u.userName, app) FROM UserProfile u JOIN u.applications app