繁体   English   中英

如何使用JPA映射实体?

[英]How to map an entity with JPA?

我有一个称为fund的表,我想使用JPA为其编写自己的查询。 因此,我已经使用IntelliJ根据我的模式而不是基于休眠来生成持久性映射。

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文件中进行了指定。

如果您的@Entity类与persistence.xml文件不在同一类路径中,则不会自动加载它。 例如- 是否有一种方法可以扫描JPA实体,而不在persistence.xml文件中声明持久性类?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM