Search code examples
javahibernatehibernate-mapping

"Is not mapped" expection with Hibernate


I have created a simple Hibernate test app but for some reason the ReciepeModel is not mapped. I have defined it in the hibernate.cfg.xml file. I have checked that I have the proper imports import javax.persistence.*;

Here's the exception when trying to run the program:

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)

I have created the following ReciepeModel which is mapped in the configuration file:

package com.calicode.model;

import javax.persistence.*;

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

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

Here's hibernate.cfg.xml with ReciepeModel mapped:

<?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>

Here's a simple test:

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

I have run out of ideas as to why this is not working. After going through other questions of the same topic, I feel like there's something bigger broken here.

EDIT 1 Added the package name to ReciepeModel code snippet


Solution

  • The problem was fixed by changing the Gradle dependency 'org.hibernate:hibernate-core:5.1.0.Final' to 'org.springframework.boot:spring-boot-starter-data-jpa:1.3.3.RELEASE'.

    This is fine because I will be using Spring Boot in this project anways.