IDEA 增加对JPA的支持 执行JPQL语句

 IDEA 可以在控制台console中执行JPQL语句:

1. 在已存在的项目中选择项目结构:

 2. 选择模块-指定实体所在的模块-选择上面的号

2. 选择要添加的模块:我们使用的是JPA,也可能有的同学选择是的Hibernate.

 4. 指定地址和版本,默认就可以

5. 选完是这个样子

 6. 上面配置完成后,就能看到view->tool windows->persistence:

 7. 配置persistence.xml:

如下代码:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" version="2.1">
    <persistence-unit name="john" transaction-type="RESOURCE_LOCAL">
        <properties>
            <!-- 标准配置方法,适用性高 -->

            <!--配置Hibernate方言 -->
            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
            <!--配置数据库驱动 -->
            <property name="hibernate.connection.driver_class" value="net.sf.log4jdbc.DriverSpy" />
            <!--配置数据库用户名 -->
            <property name="hibernate.connection.username" value="@@2" />
            <!--配置数据库密码 -->
            <property name="hibernate.connection.password" value="@@" />
            <!--配置数据库url -->
            <property name="hibernate.connection.url" value="jdbc:log4jdbc:oracle:thin:@##:1521:##" />
            <!--设置外连接抓取树的最大深度 -->
            <property name="hibernate.max_fetch_depth" value="3" />
        </properties>
    </persistence-unit>
</persistence>

配置完成后,最好重启一下,我没重启,以为配置错了,配置了半天。

配置成功后,就能看到上面的john了。

8. 打开john结点:就能看到实体DO了,右击实体->console->jpa console就可以写jpql查询了

参考地址:

https://www.jetbrains.com/help/idea/hibernate-and-jpa-facet-pages.html

https://www.w3cschool.cn/intellij_idea_doc/intellij_idea_doc-ou9n2zly.html

原文地址:https://www.cnblogs.com/hankuikui/p/11465735.html