【发布时间】:2018-11-04 08:22:50
【问题描述】:
首先很抱歉我的英语不好。我正在尝试从一个表中显示数据库(Postgres)行,并且它总是返回 null。我正在使用 JPA hibernate 在 Java EE 中做项目。
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="mesPU">
<class>pl.mes.model.Users</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/test" />
<property name="javax.persistence.jdbc.user" value="postgres" />
<property name="javax.persistence.jdbc.password" value="xxxxxx" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/test"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect"/>
</properties>
</persistence-unit>
</persistence>
用户实体
package pl.mes.model;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Users {
private Integer userId;
private String firstname;
private String secondname;
private String email;
@Id
@Column(name = "userId", nullable = false)
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
@Basic
@Column(name = "firstname", nullable = true)
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
@Basic
@Column(name = "secondname", nullable = true)
public String getSecondname() {
return secondname;
}
public void setSecondname(String secondname) {
this.secondname = secondname;
}
@Basic
@Column(name = "email", nullable = true)
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Users users = (Users) o;
if (userId != null ? !userId.equals(users.userId) : users.userId != null) return false;
if (firstname != null ? !firstname.equals(users.firstname) : users.firstname != null) return false;
if (secondname != null ? !secondname.equals(users.secondname) : users.secondname != null) return false;
if (email != null ? !email.equals(users.email) : users.email != null) return false;
return true;
}
@Override
public int hashCode() {
int result = userId != null ? userId.hashCode() : 0;
result = 31 * result + (firstname != null ? firstname.hashCode() : 0);
result = 31 * result + (secondname != null ? secondname.hashCode() : 0);
result = 31 * result + (email != null ? email.hashCode() : 0);
return result;
}
}
当我使用这个时
从用户中选择你
在控制台输出看起来像这样: output
DbSOURCE 如果有人可以帮助我,我将不胜感激:)
【问题讨论】:
-
您没有为您的实体指定注释
@Table(name = "<name of table with users>")(有关进一步说明,请参阅this answer)。这意味着必须存在一个名为Users的表。如果表确实存在,您是否连接到正确的数据库? -
首先不需要指定
@Table(所有这些都是默认的,如果没有提供)。其次,通过查看 JPA 提供程序的 LOG 并查看调用的 SQL 来调试您的问题。 -
我指定了表格但没有用。我添加了我的 dbsource 的屏幕。我想我正在连接到正确的数据库
-
另外,当您还提供足以以可移植方式定义数据库的 JPA 标准属性时,为什么还要指定特定于 Hibernate 的连接信息 (hibernate.connection*)。
-
@ltlBeBoy 您不需要指定 Table 注释。实体注解就够了,不区分大小写
标签: java hibernate jpa jakarta-ee