Intellij how to correctly configure hql with sprin

2019-01-26 19:48发布

So i was trying to use a custom query using hibernate and jpa

@Transactional
public interface EstimateOptionsDao extends JpaRepository<EstimateOptions, Integer> {

    @Query("from estimateOptions options inner join options.company company inner join company.user user where user.name = :userName\n")
    EstimateOptions EstimateOptions(String userName);

}

But the EstimateOptions gives me the following error:

Can't resolve symbol 'EstimateOptions' less... (Ctrl+F1) 
This inspection controls whether the Persistence QL Queries are error-checked

So i found this post Why does Hibernate query have compile error in IntelliJ?.

So i added a facet to test this:

Now i have hibernate.cfg.xml like this:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="connection.url"/>
    <property name="connection.driver_class"/>
    <property name="connection.username"/>
    <property name="connection.password"/>
    <!-- DB schema will be updated if needed -->
    <!-- <property name="hbm2ddl.auto">update</property> -->
  </session-factory>
</hibernate-configuration>

And spring configuration like this :

spring.datasource.url=jdbc:mysql://localhost:3306/testdb
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

But if i got to the Persistence Tool in intellij en do a simple query like this:

hql> From user

it produce the following error:

[2015-12-09 12:59:27] org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class []
[2015-12-09 12:59:27] java.lang.ClassNotFoundException: 
.....

As you can see i'm a bit lost... What is wrong with these settings ?

1条回答
一纸荒年 Trace。
2楼-- · 2019-01-26 20:30
  1. In Project structure settings (ctrl+alt+shift+s), select Facets and Add.
  2. Select JPA
  3. Select the module containing your mappings/annotated entities
  4. In the Modules view, select the module in question and the JPA facet underneath
  5. Add a reference to your descriptor (persistence.xml/orm.xml)
  6. Select your JPA provider from the dropdown

Your JPA-queries should then be validated against your entites.

查看更多
登录 后发表回答