“未映射”与Hibernate

时间:2016-05-07 20:58:48

标签: java hibernate hibernate-mapping

我创建了一个简单的Hibernate测试应用程序但由于某种原因,ReciepeModel未映射。我在hibernate.cfg.xml文件中定义了它。我已经检查过我有正确的导入import javax.persistence.*;

尝试运行程序时出现以下异常:

org.hibernate.hql.internal.ast.QuerySyntaxException: ReciepeModel is not mapped [FROM ReciepeModel WHERE idreciepe = :id]
    at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:79)
    at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:218)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:76)
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150)
    at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:302)
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:240)
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1907)
    at com.calicode.connection.Connector$1.process(Connector.java:16)
    at com.calicode.connection.ConnectionTemplate.createSession(ConnectionTemplate.java:24)
    at com.calicode.connection.Connector.connect(Connector.java:20)
    at com.calicode.connection.Connector.main(Connector.java:9)

我创建了以下ReciepeModel,它在配置文件中映射:

package com.calicode.model;

import javax.persistence.*;

@Entity
@Table(name = "reciepe")
public class ReciepeModel {

    @Id
    @GeneratedValue
    @Column(name = "idreciepe")
    private int idreciepe;
}

这是映射了ReciepeModel的hibernate.cfg.xml:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.id.new_generator_mappings">false</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <mapping class="com.calicode.model.ReciepeModel"/>
    </session-factory>
</hibernate-configuration>

这是一个简单的测试:

public static void main(String[] args) {
        connect();
    }

    public static void connect() {
        new ConnectionTemplate() {
            @Override
            public void process(Session session) {
                Query query = session.createQuery("FROM ReciepeModel WHERE idreciepe = :id");
                query.setParameter("id", 1);
            }
        }.createSession();
    }

我已经没想到为什么这不起作用了。在经历了同一主题的其他问题之后,我觉得这里有更大的问题。

编辑1 将包名称添加到ReciepeModel代码段

1 个答案:

答案 0 :(得分:0)

通过将Gradle依赖关系 Microsoft.VisualStudio.Tools.Applications.Runtime.CannotCreateStartupObjectException: Could not create an instance of startup object blaghblagh ---> System.TypeInitializationException: The type initializer for 'foo' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'blah' or one of its dependencies. The system cannot find the file specified. 更改为'org.hibernate:hibernate-core:5.1.0.Final'来解决问题。

这很好,因为我将在这个项目中使用Spring Boot。