1

I'm developing an application using Hibernate 5.2.1.Final with SQLite (driver: Xerial 3.11.8.2) and Maven. The problem is, that Hibernate can't find my named queries (which are located in an entity provided with @Entity (javax.persistence.Entity) annotation. I've searched everywhere for a solution so far, but I couldn't make it work.

I've recently switched to Hibernate API instead of using a JPA one - I had no issues before and I haven't made any modifications to this named query. As far as I'm concerned, I can still use javax.persistence annotations.

Exception I'm getting:

 Caused by: java.lang.IllegalArgumentException: No query defined for that name [SugerowanaPozycja.getByCategory]
    at org.hibernate.internal.AbstractSharedSessionContract.getNamedQuery(AbstractSharedSessionContract.java:546)
    at org.hibernate.internal.AbstractSharedSessionContract.getNamedQuery(AbstractSharedSessionContract.java:101)
    at pl.nombritech.Menual.dao.SugerowanaPozycjaDAO.getByKategoria(SugerowanaPozycjaDAO.java:90)
    at pl.nombritech.Menual.view.GlowneOknoController.uzupelnijSugerowanePozycje(GlowneOknoController.java:227)
    at pl.nombritech.Menual.view.GlowneOknoController.initialize(GlowneOknoController.java:210)
    ... 22 more

Annotations in my entity class:

import javax.persistence.Entity;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

    @Entity
    @NamedQueries({ 
    @NamedQuery(name = "SugerowanaPozycja.getByCategory",
    query = "SELECT e FROM SugerowanaPozycja e WHERE e.kategoria = :kategoria")
    })

And it is where the exception happens:

public static ObservableList<SugerowanaPozycja> getByKategoria(String kategoria)
{
    Session session = Main.SESSION_FACTORY.openSession();

    Transaction transaction = session.beginTransaction();

    Query<SugerowanaPozycja> query = session.getNamedQuery("SugerowanaPozycja.getByCategory"); // exception here
    query.setParameter("kategoria", kategoria);
    ObservableList<SugerowanaPozycja> list = FXCollections.observableArrayList(query.getResultList());

    transaction.commit();

    session.close();

    return list;
}

My hibernate.cfg.xml:

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

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">org.sqlite.JDBC</property>
        <property name="hibernate.connection.url">jdbc:sqlite:menual_data/bazadb.db</property>
        <property name="hibernate.connection.username"></property>
        <property name="hibernate.connection.password"></property>
        <property name="hibernate.dialect">pl.nombritech.Menual.util.SQLiteDialect</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="hbm2ddl.auto">create</property>
        <property name="hibernate.c3p0.min_size">1</property>
        <property name="hibernate.c3p0.max_size">1</property>
    </session-factory>
</hibernate-configuration>

And finally how I initialize Hibernate:

public static SessionFactory SESSION_FACTORY;

Configuration configuration = new Configuration().configure(Main.class.getResource("/hibernate.cfg.xml"));
StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
serviceRegistryBuilder.applySettings(configuration.getProperties());
ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
SESSION_FACTORY = configuration.buildSessionFactory(serviceRegistry);

I would be very grateful for any hints in this matter. I know there was a lot of such threads here, but it didn't help in my case.

3
  • 2
    I've recently switched to Hibernate API instead of using a JPA one: you shouldn't have done that. But anyway, your class is not listed as an entity in the configuration file. You need a <mapping> element. Read docs.jboss.org/hibernate/orm/5.2/quickstart/html_single/… and download the zip file containing the examples. Commented Aug 6, 2016 at 14:16
  • Thank you, I saw somewhere that pure Hibernate gives more possibilities than JPA and I'd like to stick to Hibernate and don't plan to switch to any other provider, because it's the only one advantage of using JPA I suppose? I thought Hibernate can scan through my classes to find the ones that have @Entity annotation... Commented Aug 6, 2016 at 14:21
  • In my case, the configuration I had done with StandardServiceRegistryBuilder was incorrect. Once fixed, it picked up the mappings just as Hibernate 4.1.x Commented Jan 25, 2021 at 4:27

1 Answer 1

5

The mapping of the class inside which your query resides should be present into the hibernate.cfg.xml. In this case since it it not present that's why you are getting this exception.

In order to resolve this issue, place the following tag inside hibernate.cfg.xml:

<mapping class="your.package.ClassName"/>

and re-run your program.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.