我在下面得到了一个例外
(例外是:org.hibernate.hql.internal.ast.QuerySyntaxException:未映射用户)
调用此代码时发生.
TypedQuery<User> query = jpaApi.em().createQuery("select u from User u where u.email = :email and u.secretHash = :secretHash", User.class)
.setParameter("email", parameter.getEmail())
.setParameter("secretHash", hashAlgorithm.hash(parameter.getPassword()));
但是如果这个项目是通过“actirvator start”运行(开发env),则不会发生此异常.
它意味着……仅在生产环境中我得到了这个例外.
我该怎么办呢.
请帮我.
感谢您的帮助,我的项目信息如下
>系统环境:
1)比赛:2.5.4
2)休眠:5.2.1.final
>例外消息:
引起:org.hibernate.hql.internal.ast.QuerySyntaxException:用户未映射[从用户u中选择u,其中u.email =:email和u.secretHash =:secretHash]
在org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:79)
在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:77)
在org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:152)
在org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:521)
在org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:623)
……还有52个
引起:org.hibernate.hql.internal.ast.QuerySyntaxException:未映射用户
在org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:171)
at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:91)
在org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:79)
在org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:321)
在org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3690)
在org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3579)
在org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:718)
在org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:574)
在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)
……还有58个
> persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="Hoth-PersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<non-jta-data-source>DefaultDS</non-jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL94Dialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.use_sql_comments" value="true" />
<property name="hibernate.max_fetch_depth" value="5" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.jdbc.batch_size" value="50" />
<property name="hibernate.jdbc.batch_versioned_data" value="true" />
<property name="hibernate.order_inserts" value="true" />
</properties>
</persistence-unit>
>实体类代码
@Entity
@Table (name = "users")
@Getter @Setter @ToString @EqualsAndHashCode (of = "email")
public class User {
@Id
private String email;
@Column(name = "secret_hash")
private String secretHash;
@Column(name = "accessed_at")
private LocalDateTime accessedAt;
private String gender;
@Column(name = "birth_year")
private Integer birthYear;
@Column(name = "picture_name")
private String pictureName;
@Column(name = "picture_url")
private String pictureUrl;
private String nickname;
}
最佳答案 persistence.xml中缺少用户映射.我无法从用户定义的包中看到片段,但我们假设它在包persistence.models中. persistence.xml应该是这样的:
<persistence-unit name="Hoth-PersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<non-jta-data-source>DefaultDS</non-jta-data-source>
<class>persistence.models.User</class>
<properties>
...
</properties>