gpt4 book ai didi

java - 如何用JPA映射实体?

转载 作者:行者123 更新时间:2023-12-01 09:45:15 25 4
gpt4 key购买 nike

我有一个名为 fund 的表,我想使用 JPA 为其编写自己的查询。因此,我使用 IntelliJ 根据我的架构而不是基于 hibernate 生成持久性映射。

import javax.persistence.*;
import java.sql.Timestamp;

@Entity
@Table(name = "fund", schema = "public", catalog = "db")
public class FundEntity {
private long fundId;
private Timestamp createdAt;
private String description;
private Timestamp modTime;
private String modUser;
private String fundName;
private String fundType;

@Id
@Column(name = "fund_id")
public long getFundId() {
return fundId;
}

public void setFundId(long fundId) {
this.fundId = fundId;
}

@Basic
@Column(name = "created_at")
public Timestamp getCreatedAt() {
return createdAt;
}

public void setCreatedAt(Timestamp createdAt) {
this.createdAt = createdAt;
}

@Basic
@Column(name = "description")
public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

@Basic
@Column(name = "mod_time")
public Timestamp getModTime() {
return modTime;
}

public void setModTime(Timestamp modTime) {
this.modTime = modTime;
}

@Basic
@Column(name = "mod_user")
public String getModUser() {
return modUser;
}

public void setModUser(String modUser) {
this.modUser = modUser;
}

@Basic
@Column(name = "fund_name")
public String getFundName() {
return fundName;
}

public void setFundName(String fundName) {
this.fundName = fundName;
}

@Basic
@Column(name = "fund_type")
public String getFundType() {
return fundType;
}

public void setFundType(String fundType) {
this.fundType = fundType;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

FundEntity that = (FundEntity) o;

if (fundId != that.fundId) return false;
if (createdAt != null ? !createdAt.equals(that.createdAt) : that.createdAt != null) return false;
if (description != null ? !description.equals(that.description) : that.description != null) return false;
if (modTime != null ? !modTime.equals(that.modTime) : that.modTime != null) return false;
if (modUser != null ? !modUser.equals(that.modUser) : that.modUser != null) return false;
if (fundName != null ? !fundName.equals(that.fundName) : that.fundName != null) return false;
if (fundType != null ? !fundType.equals(that.fundType) : that.fundType != null) return false;

return true;
}

@Override
public int hashCode() {
int result = (int) (fundId ^ (fundId >>> 32));
result = 31 * result + (createdAt != null ? createdAt.hashCode() : 0);
result = 31 * result + (description != null ? description.hashCode() : 0);
result = 31 * result + (modTime != null ? modTime.hashCode() : 0);
result = 31 * result + (modUser != null ? modUser.hashCode() : 0);
result = 31 * result + (fundName != null ? fundName.hashCode() : 0);
result = 31 * result + (fundType != null ? fundType.hashCode() : 0);
return result;
}
}

这是我的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>

<persistence-unit name="postgres">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5443/db" />
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
<property name="hibernate.connection.username" value="dba" />
<property name="hibernate.connection.password" value="XXX" />
<property name="hibernate.archive.autodetection" value="class" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect" />
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.flushMode" value="FLUSH_AUTO" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>

然后我尝试提取我的资金,但没有结果:

jpa-ql> select f from FundEntity f
[2016-06-29 18:01:11] FundEntity is not mapped [select f from FundEntity f]

我在这里缺少什么?我认为我的实体的发现会自动进行,因为我已经在 persistence.xml 文件中指定了。

最佳答案

如果您的 @Entity 类与 persistence.xml 文件不在同一类路径中,则不会自动加载它。例如 - Is there a way to scan JPA entities not to declare persistent classes in a persistence.xml file?

关于java - 如何用JPA映射实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38104769/

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