使用JPA工具生成这些Java entrent是否正确?

时间:2017-05-05 21:33:04

标签: java jpa eclipselink

我在PostgreSQL中有以下表格:

CREATE TABLE public.company_subscripcion  ( 
    id_suscription  int4 NOT NULL,
    status          char(1) NOT NULL,
    id_company      int4 NOT NULL,
    PRIMARY KEY(id_suscription,id_company)
)
GO
ALTER TABLE public.company_subscripcion
    ADD CONSTRAINT company_subscripcion_id_suscription_fkey
    FOREIGN KEY(id_suscription)
    REFERENCES public.subscription(id_suscription)
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION 
GO
ALTER TABLE public.company_subscripcion
    ADD CONSTRAINT company_subscripcion_id_company_fkey
    FOREIGN KEY(id_company)
    REFERENCES public.company(id_company)
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION 
GO


CREATE TABLE public.company  ( 
    id_company      int4 NOT NULL,
    id_corporation  int4 NULL,
    tin             varchar(20) NOT NULL,
    short_name      varchar(20) NOT NULL,
    name            varchar(100) NOT NULL,
    commercial_name varchar(100) NOT NULL,
    address         varchar(200) NOT NULL,
    phone_number    varchar(20) NULL,
    logo_url        varchar(500) NULL,
    created_by      int4 NOT NULL,
    created         timestamp NOT NULL,
    updated_by      int4 NULL,
    last_update     timestamp NULL,
    s3_bucket       varchar(20) NULL,
    s3_access       varchar(20) NULL,
    s3_secret       varchar(20) NULL,
    sunat_username  varchar(30) NULL,
    sunat_password  varchar(64) NULL,
    others          text NULL,
    PRIMARY KEY(id_company)
)

CREATE TABLE public.subscription  ( 
    id_suscription      int4 NOT NULL,
    automatic_renewal   char(1) NOT NULL,
    amount              numeric(10,2) NOT NULL,
    status              char(1) NOT NULL,
    start_date          date NULL,
    expiration_date     date NULL,
    id_user             int4 NULL,
    id_plan             int4 NOT NULL,
    created_by          int4 NOT NULL,
    created             timestamp NOT NULL,
    updated_by          int4 NULL,
    last_update         timestamp NULL,
    PRIMARY KEY(id_suscription)
)

使用JPA工具我已经生成了这些实体:

CompanySubscripcion.java

@Entity
@Table(name="company_subscripcion")
@NamedQuery(name="CompanySubscripcion.findAll", query="SELECT c FROM CompanySubscripcion c")
public class CompanySubscripcion implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    private CompanySubscripcionPK id;
    private String status;
    private Company company;
    private Subscription subscription;

    public CompanySubscripcion() {
    }

    public CompanySubscripcionPK getId() {
        return this.id;
    }

    public void setId(CompanySubscripcionPK id) {
        this.id = id;
    }


    @Column(nullable=false, length=1)
    public String getStatus() {
        return this.status;
    }

    public void setStatus(String status) {
        this.status = status;
    }


    //uni-directional many-to-one association to Company
    @ManyToOne
    @JoinColumn(name="id_company", nullable=false, insertable=false, updatable=false)
    public Company getCompany() {
        return this.company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }


    //uni-directional many-to-one association to Subscription
    @ManyToOne
    @JoinColumn(name="id_suscription", nullable=false, insertable=false, updatable=false)
    public Subscription getSubscription() {
        return this.subscription;
    }

    public void setSubscription(Subscription subscription) {
        this.subscription = subscription;
    }

}

CompanySubscripcionPK.java

@Embeddable
public class CompanySubscripcionPK implements Serializable {
    //default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;
    private Integer idSuscription;
    private Integer idCompany;

    public CompanySubscripcionPK() {
    }

    @Column(name="id_suscription", insertable=false, updatable=false, unique=true, nullable=false)
    public Integer getIdSuscription() {
        return this.idSuscription;
    }
    public void setIdSuscription(Integer idSuscription) {
        this.idSuscription = idSuscription;
    }

    @Column(name="id_company", insertable=false, updatable=false, unique=true, nullable=false)
    public Integer getIdCompany() {
        return this.idCompany;
    }
    public void setIdCompany(Integer idCompany) {
        this.idCompany = idCompany;
    }

    public boolean equals(Object other) {
        if (this == other) {
            return true;
        }
        if (!(other instanceof CompanySubscripcionPK)) {
            return false;
        }
        CompanySubscripcionPK castOther = (CompanySubscripcionPK)other;
        return 
            this.idSuscription.equals(castOther.idSuscription)
            && this.idCompany.equals(castOther.idCompany);
    }

    public int hashCode() {
        final int prime = 31;
        int hash = 17;
        hash = hash * prime + this.idSuscription.hashCode();
        hash = hash * prime + this.idCompany.hashCode();

        return hash;
    }
}

我有这个疑问,因为当我使用ReadAllQuery方法(我使用Eclipselink)读取company_subscripcion表的数据时,我收到此错误:

[EL Finest]: query: 2017-05-05 16:29:52.328--UnitOfWork(1108818591)--Thread(Thread[http-nio-8081-exec-8,5,main])--Execute query ReadAllQuery(referenceClass=CompanySubscripcion sql="SELECT STATUS, IDCOMPANY, IDSUSCRIPTION, COMPANY_id_company, SUBSCRIPTION_id_suscription FROM company_subscripcion")
[EL Finest]: connection: 2017-05-05 16:29:52.328--ServerSession(2070565177)--Connection(641062437)--Thread(Thread[http-nio-8081-exec-8,5,main])--Connection acquired from connection pool [default].
[EL Fine]: sql: 2017-05-05 16:29:52.328--ServerSession(2070565177)--Connection(641062437)--Thread(Thread[http-nio-8081-exec-8,5,main])--SELECT STATUS, IDCOMPANY, IDSUSCRIPTION, COMPANY_id_company, SUBSCRIPTION_id_suscription FROM company_subscripcion
[EL Fine]: sql: 2017-05-05 16:29:52.331--ServerSession(2070565177)--Thread(Thread[http-nio-8081-exec-8,5,main])--SELECT 1
[EL Finest]: connection: 2017-05-05 16:29:52.333--ServerSession(2070565177)--Connection(641062437)--Thread(Thread[http-nio-8081-exec-8,5,main])--Connection released to connection pool [default].
[EL Warning]: 2017-05-05 16:29:52.333--UnitOfWork(1108818591)--Thread(Thread[http-nio-8081-exec-8,5,main])--Local Exception Stack: 
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ERROR: no existe la columna «idcompany»
  Hint: Perhaps you meant to reference the column "company_subscripcion.id_company".
  Position: 16
Error Code: 0
Call: SELECT STATUS, IDCOMPANY, IDSUSCRIPTION, COMPANY_id_company, SUBSCRIPTION_id_suscription FROM company_subscripcion
Query: ReadAllQuery(referenceClass=CompanySubscripcion sql="SELECT STATUS, IDCOMPANY, IDSUSCRIPTION, COMPANY_id_company, SUBSCRIPTION_id_suscription FROM company_subscripcion")
    at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:340)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:679)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:558)
    at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(AbstractSession.java:1995)
    at org.eclipse.persistence.sessions.server.ServerSession.executeCall(ServerSession.java:570)

感谢您的帮助:))

更新1

我再次生成实体,现在他们工作了。这个新的权利与以前不同:

@Entity
@Table(name="company_subscripcion")
@NamedQuery(name="CompanySubscripcion.findAll", query="SELECT c FROM CompanySubscripcion c")
public class CompanySubscripcion implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private CompanySubscripcionPK id;

    private String status;

    //bi-directional many-to-one association to Company
    @ManyToOne
    @JoinColumn(name="id_company")
    private Company company;

    //bi-directional many-to-one association to Subscription
    @ManyToOne
    @JoinColumn(name="id_suscription")
    private Subscription subscription;

    public CompanySubscripcion() {
    }

    public CompanySubscripcionPK getId() {
        return this.id;
    }

    public void setId(CompanySubscripcionPK id) {
        this.id = id;
    }

    public String getStatus() {
        return this.status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Company getCompany() {
        return this.company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }

    public Subscription getSubscription() {
        return this.subscription;
    }

    public void setSubscription(Subscription subscription) {
        this.subscription = subscription;
    }

}

@Embeddable
public class CompanySubscripcionPK implements Serializable {
    //default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;

    @Column(name="id_suscription", insertable=false, updatable=false)
    private Integer idSuscription;

    @Column(name="id_company", insertable=false, updatable=false)
    private Integer idCompany;

    public CompanySubscripcionPK() {
    }
    public Integer getIdSuscription() {
        return this.idSuscription;
    }
    public void setIdSuscription(Integer idSuscription) {
        this.idSuscription = idSuscription;
    }
    public Integer getIdCompany() {
        return this.idCompany;
    }
    public void setIdCompany(Integer idCompany) {
        this.idCompany = idCompany;
    }

    public boolean equals(Object other) {
        if (this == other) {
            return true;
        }
        if (!(other instanceof CompanySubscripcionPK)) {
            return false;
        }
        CompanySubscripcionPK castOther = (CompanySubscripcionPK)other;
        return 
            this.idSuscription.equals(castOther.idSuscription)
            && this.idCompany.equals(castOther.idCompany);
    }

    public int hashCode() {
        final int prime = 31;
        int hash = 17;
        hash = hash * prime + this.idSuscription.hashCode();
        hash = hash * prime + this.idCompany.hashCode();

        return hash;
    }
}

我的问题已经解决了。感谢。

1 个答案:

答案 0 :(得分:0)

错误可能令人困惑,一方面说它无法找到列idcompany

  

错误:没有exlas la columna«idcompany»

另一方面说问题出在列id_company

  

提示:也许您打算引用“company_subscripcion.id_company”栏目

问题是您向我们展示的代码指向数据库中的列id_company,该列不存在。

@Column(name="id_company"

再次检查数据库。

Un abrazo!