Java Hibernate:无法解析实体类中的表

Java Hibernate:无法解析实体类中的表,java,hibernate,maven,intellij-idea,Java,Hibernate,Maven,Intellij Idea,我正在使用Intellij Idea 2018.1.3 我已经开发了一个小型maven应用程序,用于测试hibernate的注释映射,但我对表名及其列有一个问题。它们用红色条纹标记,并说明“无法解析表/列”…” 我认为Idea中的Hibernate设置有问题,但它在哪里呢 我已经创建了hibernate.cfg.xml <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC

我正在使用Intellij Idea 2018.1.3

我已经开发了一个小型maven应用程序,用于测试hibernate的注释映射,但我对表名及其列有一个问题。它们用红色条纹标记,并说明“无法解析表/列”…”

我认为Idea中的Hibernate设置有问题,但它在哪里呢

我已经创建了hibernate.cfg.xml

<?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.connection.driver_class"> org.postgresql.Driver</property>
    <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/cowbull1</property>
    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</property>
    <property name="hibernate.connection.username">cowbull_admin</property>
    <property name="hibernate.connection.password">cowbull</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <property name="show_sql">true</property>
    <mapping class="org.mycode.entities.User"/>
</session-factory>
</hibernate-configuration>
HibernateUtil类:

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            return new Configuration().configure().buildSessionFactory();
    }
        catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}
主要类别:

import org.hibernate.Session;
import org.myapp.entities.User;
import org.myapp.util.HibernateUtil;

public class App {
    public static void main(String[] args) {
        System.out.println("maven + hibernate + postgresql");
        Session session = HibernateUtil.getSessionFactory().openSession();

        session.beginTransaction();
        User user = new User();
        user.setNickname("Dima");
        user.setPassword("123456");
        session.save(user);
        session.getTransaction().commit();
        session.close();
    }
}
pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0     http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.myapp</groupId>
<artifactId>cowbull1</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.3.2.Final</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.0.7.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.0.7.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.2.4</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/postgresql/postgresql -->
    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.1-901-1.jdbc4</version>
    </dependency>
</dependencies>

4.0.0
org.myapp
牛头犬1
1.0-快照
org.hibernate
冬眠核心
5.3.2.最终版本
org.springframework
弹簧网
5.0.7.发布
org.springframework
SpringWebMVC
5.0.7.发布
org.postgresql
postgresql
42.2.4
postgresql
postgresql
9.1-901-1.jdbc4

我已经在项目设置中定义了hibernate模块和方面

应用程序运行良好,并将适当的条目放入表中


Upd:我已经通过idea的编辑器连接到数据库,它工作得很好。

正如您所说,这并不是阻止代码编译的错误。此错误意味着编辑器不知道您的表及其列。从视图/工具窗口/数据库向编辑器添加数据库菜单。然后通过绿色标志指定一个数据源。如果编辑器成功连接到您的数据库,这些错误将消失,您也可以利用代码完成的优势。另外,如果数据库中有任何更改,可以通过刷新数据源通知编辑器。

正如您所说,这不是阻止代码编译的错误。此错误意味着编辑器不知道您的表及其列。从视图/工具窗口/数据库向编辑器添加数据库菜单。然后通过绿色标志指定一个数据源。如果编辑器成功连接到您的数据库,这些错误将消失,您也可以利用代码完成的优势。另外,如果数据库中有任何更改,可以通过刷新数据源通知编辑器。

只需IDE配置。您可以运行程序,不必担心错误

  • Ctrl+All+Shift+S打开项目结构

  • 在项目设置中,单击模块->单击绿色按钮+

  • 选择JPA->默认JPA提供程序选择Hibernate->OK


  • 只需IDE配置。您可以运行程序,不必担心错误

  • Ctrl+All+Shift+S打开项目结构

  • 在项目设置中,单击模块->单击绿色按钮+

  • 选择JPA->默认JPA提供程序选择Hibernate->OK


  • 这个答案可以帮你解决这个问题

    您只需将IDE指向正确的数据源:


    这个答案可以帮你解决这个问题

    您只需将IDE指向正确的数据源:


    谢谢您的回答!我忘了写我已经通过idea的编辑器连接到了数据库,而且运行良好。但我仍然有一个问题,谢谢你们的回答!我忘了写我已经通过idea的编辑器连接到了数据库,而且运行良好。但我仍然有一个话题。
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0     http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>org.myapp</groupId>
    <artifactId>cowbull1</artifactId>
    <version>1.0-SNAPSHOT</version>
    
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.3.2.Final</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.0.7.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.0.7.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/postgresql/postgresql -->
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.1-901-1.jdbc4</version>
        </dependency>
    </dependencies>