2017-05-05 18 views
0

我在PostgreSQL的下表:使用JPA工具生成的这些Java实体是否正确?

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; 
    } 
} 

我的问题已解决。谢谢。

回答

0

的错误可能之前一侧混淆它说,找不到列idcompany

ERROR: no existe la columna «idcompany»

而在另一边说,问题出在列id_company

Hint: Perhaps you meant to reference the column "company_subscripcion.id_company"

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

@Column(name="id_company" 

再次检查数据库。

Un abrazo!

+0

但是列id_company确实存在......我会知道是否使用JPA工具自动生成的实体是正确的 –

+0

自动生成的工具应该可以工作。你可以尝试删除“私人公司公司”吗?并且它是getter和setter方法来查看这是否是问题? –

+0

我已经注意到某些东西有线,查询产生的是这一个:SELECT STATUS,IDCOMPANY,IDSUSCRIPTION,COMPANY_ID_company,SUBSCRIPTION_id_suscription FROM company_subscripcion –