2016-06-29 222 views
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文件中指定了它。

回答