0
我有一張名爲fund
的表,我想用JPA來編寫我自己的查詢。所以,我使用IntelliJ來基於我的模式生成持久性映射,而不是基於hibernate。如何使用JPA映射實體?
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>
然後我試着去找我funds
但沒有結果:
jpa-ql> select f from FundEntity f
[2016-06-29 18:01:11] FundEntity is not mapped [select f from FundEntity f]
缺少什麼我在這裏?我認爲我的實體的發現將會自動完成,因爲我已經在persistence.xml文件中指定了它。
它的工作!謝謝你@Shankar! –