0

I have a weird problem. I googled many solutions in the internet, but no one was helpful.

I have this simple method to get information from my database:

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.getCurrentSession();

session.beginTransaction();

Query query = session.getNamedQuery("User.findByHash");
query.setString("hash", hash);
query.setDate("hash", new Date());
List<?> list = query.list();
User user = (User) list.get(0);

session.getTransaction().commit();

And this returns me an error:

Exception in thread "main" org.hibernate.MappingException: Named query not known: User.findByHash at org.hibernate.impl.AbstractSessionImpl.getNamedQuery(AbstractSessionImpl.java:93) at org.hibernate.impl.SessionImpl.getNamedQuery(SessionImpl.java:1407) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:345) at com.sun.proxy.$Proxy0.getNamedQuery(Unknown Source) at wroproject.UserConnection._getUserByHash(UserConnection.java:44) at wroproject.UserConnection.(UserConnection.java:26) at wroproject.Main.main(Main.java:19)

The top of the User.java file looks like this:

@Entity
@Table(name = "user", catalog = "wroproject", schema = "public")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "User.findAll", query = "SELECT u FROM User u"),
    @NamedQuery(name = "User.findById", query = "SELECT u FROM User u WHERE u.id = :id"),
    @NamedQuery(name = "User.findByPassword", query = "SELECT u FROM User u WHERE u.password = :password"),
    @NamedQuery(name = "User.findByHash", query = "SELECT \"user\".* FROM \"user\" LEFT JOIN session on \"user\".id = session.user_id WHERE session.hash = :hash AND date_time_terminate < :date")
})
public class User implements Serializable {...}

My fibernate.cfg.xml file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/wroproject</property>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.connection.password">postgres</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
    </session-factory>
</hibernate-configuration>

And persistence.xml file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="WroProjectPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>wrocproject.db.User</class>
    <class>wrocproject.db.Email</class>
    <class>wrocproject.db.Session</class>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/wroproject"/>
      <property name="javax.persistence.jdbc.password" value="postgres"/>
      <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
      <property name="javax.persistence.jdbc.user" value="postgres"/>
      <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
      <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
    </properties>
  </persistence-unit>
</persistence>

What should I do?

5
  • Do you have on line 44 and 26 of class UserConnection Commented Nov 13, 2014 at 18:49
  • In 24-th line I run a method with above code. In 44-th I have this Query query = session.getNamedQuery("User.findByHash"); Commented Nov 13, 2014 at 19:29
  • Even if I change User.findByHash to User.findAll I still have Named query not known error. Commented Nov 13, 2014 at 19:35
  • Can you confirm that User.java imports "org.hibernate.annotations.Entity" and not "javax.persistence.Entity"? Commented Nov 13, 2014 at 20:28
  • Yes, User.java imports "org.hibernate.annotations.Entity" Commented Nov 13, 2014 at 20:42

2 Answers 2

2

It is odd to me that you are using both a hibernate.cfg.xml and a persistence.xml file to configure hibernate. If you are using hibernate-core, then use hibernate.cfg.xml, if you are using hibernate-entitymanager, use persistence.xml.

Since you are using SessionFactory, your persistence.xml is probably being ignored for configuration. Since persistence.xml is where you specify what your entities are, I suspect none of your entities are being mapped. You can test this by just trying to load the entity without using a stored procedure:

session.get(User.class, id);

Also you could turn on logging and see what entities are being registered when you create a SessionFactory.

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

Comments

0

In the query are this parameters:

hash AND date_time_terminate

Change :

Query query = session.getNamedQuery("User.findByHash");
query.setString("hash", hash);
query.setDate("hash", new Date());

By:

Query query = session.getNamedQuery("User.findByHash");
query.setString("hash", hash);
query.setDate("date_time_terminate", new Date());

1 Comment

I'm thinking you have query.setDate("date", new Date()) in your mind. Sadly there is the same error.

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.