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.
<mapping>element. Read docs.jboss.org/hibernate/orm/5.2/quickstart/html_single/… and download the zip file containing the examples.