gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:18:02 25 4
gpt4 key购买 nike

我使用的是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>

我有一个实体类:

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>

我在项目设置中定义了 hibernate 模块和构面。

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

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

最佳答案

只是 IDE 配置。您可以运行程序并且不用担心错误

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

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

  3. 选择 JPA -> 默认 JPA 提供程序选择 Hibernate -> 确定

enter image description here

enter image description here

关于java - Hibernate:无法解析实体类中的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51462345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com