play 2.5,hibernate:table not mapped

时间:2016-07-19 07:30:52

标签: hibernate playframework

我在下面得到了一个例外

(例外是: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()));

但是如果这个项目是由&#34; actirvator start&#34; (开发环境),这个例外没有发生。

它意味着......只有在生产环境中我才有这个例外。

我该如何解决它。

请帮帮我。

感谢您的帮助,我的项目信息在下面

  • 系统环境:

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;
    }
    

3 个答案:

答案 0 :(得分:1)

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>

答案 1 :(得分:0)

您是否在调用此查询的控制器中的方法上放置了@Transactional注释?

您还可以将此模型添加到持久性文件

models.User

答案 2 :(得分:0)

看起来Play 2.4中引入了一个错误。您需要将以下行添加到build.sbt作为解决方法。

PlayKeys.externalizeResources := false

它似乎与在prod模式下如何加载类有关。

See here for more info