休眠:无法解析实体类中的表

时间:2018-07-22 05:34:17

标签: java hibernate maven intellij-idea

我正在使用Intellij Idea 2018.1.3

我已经完成了一个小Maven应用程序,用于使用注释映射测试休眠状态,但是我对表名和列名有疑问。他们用红色条纹标记为澄清“无法解析表/列'...'”

我认为在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>

我有一个实体类:

import javax.persistence.*;

@Entity
@Table(name = "users")
public class User {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(name = "nickname")
    private String nickname;
    @Column(name = "password")
    private String password;
    //getters, setters, constructors
}

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>

我已经在项目设置中定义了休眠模块和构面。

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

更新:我已经通过Idea的编辑器连接到数据库,并且运行良好。

3 个答案:

答案 0 :(得分:1)

此答案可以帮助您。

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

https://stackoverflow.com/a/60534314/8536903

答案 1 :(得分:0)

正如您所说,这不是阻止您的代码编译的错误。此错误意味着编辑器无法识别您的表及其列。从视图/工具窗口/数据库将数据库菜单添加到编辑器。然后通过绿色符号分配数据源。如果编辑器成功连接到您的数据库,这些错误将消失,您也可以利用代码完成的优势。另一件事,如果您对数据库进行了任何更改,则可以通过刷新数据源来通知编辑器。

答案 2 :(得分:0)

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

  1. Ctrl + All + Shift + S 打开项目结构

  2. 在“项目设置”中,单击模块->单击按钮绿色 +

  3. 选择 JPA ->默认JPA Provider选择休眠-> 确定

enter image description here

enter image description here