Why is IntelliJ not able to find named queries based on Hibernate and persistence.xml?

Answered

I'm using Hibernate and have a corresponding persistence.xml file for configuration (Java SE). Most of things are working fine, except the named queries I defined.

If I'm trying to start the application following exception will be thrown

java.lang.IllegalArgumentException: No query defined for that name [namedQuery]
    at org.hibernate.internal.AbstractSharedSessionContract.buildQueryFromName(AbstractSharedSessionContract.java:892)
    at org.hibernate.internal.AbstractSharedSessionContract.createNamedQuery(AbstractSharedSessionContract.java:990)
    at org.hibernate.internal.AbstractSessionImpl.createNamedQuery(AbstractSessionImpl.java:23)
    at com....

The persistence.xml (placed in src/main/resources/META-INF/persistence.xml) looks like following

<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
             version="2.0">
    <persistence-unit name="com...">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver"/>
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.max_fetch_depth" value="3"/>
            <property name="hibernate.connection.autocommit" value="false"/>
            <property name="hibernate.connection.provider_class" value="org.hibernate.hikaricp.internal.HikariCPConnectionProvider" />            
            <property name="hibernate.transaction.jta.platform" value="JtaPlatform" />
            <property name="hibernate.hikari.connectionTimeout" value="30000" />
            <property name="hibernate.hikari.minimumIdle" value="1" />
            <property name="hibernate.hikari.maximumPoolSize" value="50" />
            <property name="hibernate.hikari.idleTimeout" value="300000" />
            <property name="hibernate.connection.url" value="jdbc:..."/>            
        </properties>
    </persistence-unit>
</persistence>

I already tried to set different properties like

<property name="packagesToScan" value="com.package.**"/>

and

<property name="hibernate.archive.autodetection" value="class, hbm"/>.

All of my entities are annoated with @Entity@Table and the @NamedQueries. JPA is configured an the queries can be resolved by IntelliJ. Only start of the application does not work.

Please note that it is not an option for me, to add the <class> tag to persistence.xml, because I don't want to have that kind of effort every time I add a new entity.

THX!

 

0
1 comment

It is better to ask such questions on StackOverflow, as it is not directly related to IntelliJ IDEA, and more to the project itself. 

See a similar question here: 

https://stackoverflow.com/a/38806002

0

Please sign in to leave a comment.