休眠状态:[实体]未映射

时间:2019-07-24 20:18:10

标签: hibernate kotlin

我已经在Kotlin-vertx项目中配置了Hibernate,我设法完成了所有设置,但是当我运行HQL查询时,它会输出:

  

严重:org.hibernate.hql.internal.ast.QuerySyntaxException:电影是   未映射[从m AS电影在m.id =:id中选择m]   java.lang.IllegalArgumentException:   org.hibernate.hql.internal.ast.QuerySyntaxException:电影不是   映射[SELECT m FROM电影AS m WHERE m.id =:id]在   org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:138)     在   org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)     在   org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188)     在   org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:729)     在   org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:745)     在   org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:23)     在MonoServer $ main $ 4.handle(MonoServer.kt:46)在   MonoServer $ main $ 4.handle(MonoServer.kt:14)位于   io.vertx.ext.web.impl.RouteImpl.handleContext(RouteImpl.java:231)位于   io.vertx.ext.web.impl.RoutingContextImplBase.iterateNext(RoutingContextImplBase.java:121)     在   io.vertx.ext.web.impl.RoutingContextImpl.next(RoutingContextImpl.java:131)     在io.vertx.ext.web.impl.RouterImpl.handle(RouterImpl.java:80)处   io.vertx.ext.web.impl.RouterImpl.handle(RouterImpl.java:41)位于   io.vertx.ext.web.Router.accept(Router.java:64)在   MonoServer $ main $ 3.handle(MonoServer.kt:39)位于   MonoServer $ main $ 3.handle(MonoServer.kt:14)位于   io.vertx.core.http.impl.WebSocketRequestHandler.handle(WebSocketRequestHandler.java:50)     在   io.vertx.core.http.impl.WebSocketRequestHandler.handle(WebSocketRequestHandler.java:32)     在   io.vertx.core.http.impl.HttpServerRequestImpl.handleBegin(HttpServerRequestImpl.java:149)     在   io.vertx.core.http.impl.Http1xServerConnection.handleMessage(Http1xServerConnection.java:135)     在io.vertx.core.impl.ContextImpl.executeTask(ContextImpl.java:369)     在   io.vertx.core.impl.EventLoopContext.execute(EventLoopContext.java:43)     在io.vertx.core.impl.ContextImpl.executeFromIO(ContextImpl.java:232)     在   io.vertx.core.net.impl.VertxHandler.channelRead(VertxHandler.java:173)     在   io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:359)     在   io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:345)     在   io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:337)     在   io.vertx.core.http.impl.Http1xUpgradeToH2CHandler.channelRead(Http1xUpgradeToH2CHandler.java:99)     在   io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:359)     在   io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:345)     在   io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:337)     在   io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:323)     在   io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:297)     在   io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:359)     在   io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:345)     在   io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:337)     在   io.vertx.core.http.impl.Http1xOrH2CHandler.end(Http1xOrH2CHandler.java:61)     在   io.vertx.core.http.impl.Http1xOrH2CHandler.channelRead(Http1xOrH2CHandler.java:38)     在   io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:359)     在   io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:345)     在   io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:337)     在   io.netty.channel.DefaultChannelPipeline $ HeadContext.channelRead(DefaultChannelPipeline.java:1408)     在   io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:359)     在   io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:345)     在   io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:930)     在   io.netty.channel.nio.AbstractNioByteChannel $ NioByteUnsafe.read(AbstractNioByteChannel.java:163)     在   io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:677)     在   io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:612)     在   io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:529)     在io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:491)处   io.netty.util.concurrent.SingleThreadEventExecutor $ 5.run(SingleThreadEventExecutor.java:905)     在   io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)     在java.base / java.lang.Thread.run(Thread.java:834)造成原因:   org.hibernate.hql.internal.ast.QuerySyntaxException:电影不是   映射[SELECT m FROM电影AS m WHERE m.id =:id]在   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:220)     在   org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:144)     在   org.hibernate.engine.query.spi.HQLQueryPlan。(HQLQueryPlan.java:113)     在   org.hibernate.engine.query.spi.HQLQueryPlan。(HQLQueryPlan.java:73)     在   org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:158)     在   org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:611)     在   org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:720)     ... 49更多原因:   org.hibernate.hql.internal.ast.QuerySyntaxException:电影不是   映射在   org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:169)     在   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:331)     在   org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3695)     在   org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3584)     在   org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:720)     在   org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:576)     在   org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:313)     在   org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:261)     在   org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:272)     在   org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:192)     ...还有55个

我的persistence.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="default">
        <description>Persistence XML</description>
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>

            <!-- Hibernate Config -->
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.generate_statistics" value="false" />
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
            <property name="hibernate.connection.charSet" value="UTF-8"/>
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="false"/>
            <property name="hibernate.use_sql_comments" value="false"/>

            <!-- JDBC Config -->
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/monolith_db" />
            <property name="javax.persistence.jdbc.user" value="monolith_user" />
            <property name="javax.persistence.jdbc.password" value="monolith_user_pwd" />

        </properties>
    </persistence-unit>
</persistence>

我正在这样调用查询:

val query = em.createQuery("SELECT m FROM Movie AS m WHERE m.id=:id", Movie::class.java)
                query.setParameter("id", 2)
                val movie = query.singleResult

我的实体:

@Entity
@Table(name = "movie")
class Movie(
    var title: String = "",
    var summary: String = ""
) : StandardEntity() {

    override fun toString(): String {
        return "Movie[id=$id, title=$title, summary=${summary.substring(0, 30)}"
    }
}

谢谢。

1 个答案:

答案 0 :(得分:0)

我遇到了一个非常相似的问题,并多次遇到此问题,原因是您的错误 看起来与JPA提供程序的配置有关,请尝试使用spring对其进行配置,看看是否有帮助

我假设这是根本原因,因为在另一篇文章中,它告诉我Intellij由于未配置JPA提供程序而无法映射实体,因此,一旦按如下所述固定映射,它就会停止失败< / p>

在我看来,这是更愚蠢的事情,因为我在使用@Entity(name = "nameOfTheTable")时应该使用@Entity(name = "nameOfEntity"), @Table(name = "nameOfTable")

也可以尝试下一个,看看是否有帮助。尝试在@Entity中向您的实体添加自定义名称,然后使用该名称,即:

@Entity(name = "MyMovieEntity")
@Table(name = "movie")

然后尝试在查询中使用它:

val query = em.createQuery("SELECT m FROM MyMovieEntity AS m WHERE m.id=:id", Movie::class.java)
                query.setParameter("id", 2)
                val movie = query.singleResult