无法在两个表

时间:2017-10-05 10:53:21

标签: java spring hibernate jpa informix

我尝试使用Lbg注释将iLbgOneToMany联系起来

package bg.moi.domain.model;

import javax.persistence.*;
import java.util.Date;
import java.util.Set;

@Entity
@Table(name = "l_bg" , catalog = "bds1")//, schema = "admbds",
@IdClass(LBgEntityPK.class)
public class LBgEntity {
    @Id
    @Column(name = "sid_o", nullable = false, precision = 0)
    private int sidO;
    public int getSidO() {
        return sidO;
    }

    public void setSidO(int sidO) {
        this.sidO = sidO;
    }

    @Id
    @Column(name = "pnr_o", nullable = false)
    private short pnrO;
    public short getPnrO() {
        return pnrO;
    }

    public void setPnrO(short pnrO) {
        this.pnrO = pnrO;
    }

    @Basic
    @Column(name = "egn", nullable = false, length = 10)
    private String egn;
    public String getEgn() {
        return egn;
    }

    public void setEgn(String egn) {
        this.egn = egn;
    }

    @Basic
    @Column(name = "ime", nullable = false, length = 25)
    private String ime;
    public String getIme() {
        return ime;
    }

    public void setIme(String ime) {
        this.ime = ime;
    }

    @Basic
    @Column(name = "prezime", nullable = false, length = 25)
    private String prezime;
    public String getPrezime() {
        return prezime;
    }

    public void setPrezime(String prezime) {
        this.prezime = prezime;
    }

    @Basic
    @Column(name = "familno", nullable = false, length = 43)
    private String familno;
    public String getFamilno() {
        return familno;
    }

    public void setFamilno(String familno) {
        this.familno = familno;
    }

    @Basic
    @Column(name = "dat_vav", nullable = false)
    private Date datVav;
    public Date getDatVav() { return datVav; }
    public void setDatVav(Date datVav) { this.datVav = datVav; }

    @Basic
    @Column(name = "mrk_akt", nullable = false, length = 1)
    private String mrkAkt;
    public String getMrkAkt() {
        return mrkAkt;
    }
    public void setMrkAkt(String mrkAkt) {
        this.mrkAkt = mrkAkt;
    }

    @OneToMany(targetEntity = ILbgEntity.class,fetch = FetchType.LAZY)
    @JoinColumns({@JoinColumn(name = "sidO",referencedColumnName = "sidO",insertable = false,updatable = false),
            @JoinColumn(name = "pnrO" ,referencedColumnName = "pnrO",insertable = false,updatable = false)})
    //@JoinColumn(foreignKey = @ForeignKey(name = "r130_1013"))
    private Set<ILbgEntity> iLbgEntity;
    public void setiLbgEntity(Set<ILbgEntity> iLbgEntity) { this.iLbgEntity = iLbgEntity; }
    public Set<ILbgEntity> getiLbgEntity(){ return iLbgEntity; }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        LBgEntity lBgEntity = (LBgEntity) o;

        if (sidO != lBgEntity.sidO) return false;
        if (pnrO != lBgEntity.pnrO) return false;
        if (egn != null ? !egn.equals(lBgEntity.egn) : lBgEntity.egn != null) return false;
        if (ime != null ? !ime.equals(lBgEntity.ime) : lBgEntity.ime != null) return false;
        if (prezime != null ? !prezime.equals(lBgEntity.prezime) : lBgEntity.prezime != null) return false;
        if (familno != null ? !familno.equals(lBgEntity.familno) : lBgEntity.familno != null) return false;
        if (datVav != null ? !datVav.equals(lBgEntity.datVav) : lBgEntity.datVav != null) return false;
        if (mrkAkt != null ? !mrkAkt.equals(lBgEntity.mrkAkt) : lBgEntity.mrkAkt != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = sidO;
        result = 31 * result + (int) pnrO;
        result = 31 * result + (egn != null ? egn.hashCode() : 0);
        result = 31 * result + (ime != null ? ime.hashCode() : 0);
        result = 31 * result + (prezime != null ? prezime.hashCode() : 0);
        result = 31 * result + (familno != null ? familno.hashCode() : 0);
        result = 31 * result + (datVav != null ? datVav.hashCode() : 0);
        result = 31 * result + (mrkAkt != null ? mrkAkt.hashCode() : 0);
        return result;
    }

}

package bg.moi.domain.models;

import javax.persistence.Column;
import javax.persistence.Id;
import java.io.Serializable;

public class LBgEntityPK implements Serializable {
    private int sidO;
    private short pnrO;

    @Column(name = "sid_o", nullable = false, precision = 0)
    @Id
    public int getSidO() {
        return sidO;
    }

    public void setSidO(int sidO) {
        this.sidO = sidO;
    }

    @Column(name = "pnr_o", nullable = false)
    @Id
    public short getPnrO() {
        return pnrO;
    }

    public void setPnrO(short pnrO) {
        this.pnrO = pnrO;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        LBgEntityPK that = (LBgEntityPK) o;

        if (sidO != that.sidO) return false;
        if (pnrO != that.pnrO) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = sidO;
        result = 31 * result + (int) pnrO;
        return result;
    }
}


  package bg.moi.domain.model;

    import javax.persistence.*;

    @Entity
    @Table(name = "i_lbg",  catalog = "bds1") //schema = "admbds",
    @IdClass(ILbgEntityPK.class)
    public class ILbgEntity {

        @Id
        @Column(name = "sid_o", nullable = false, precision = 0)
        private int sidO;
        public int getSidO() {
            return sidO;
        }
        public void setSidO(int sidO) {
            this.sidO = sidO;
        }

        @Id
        @Column(name = "pnr_o", nullable = false)
        private short pnrO;
        public short getPnrO() {
            return pnrO;
        }
        public void setPnrO(short pnrO) {
            this.pnrO = pnrO;
        }

        @Basic
        @Column(name = "ob1", nullable = false)
        private int ob1;
        public int getOb1() {
            return ob1;
        }
        public void setOb1(int ob1) {
            this.ob1 = ob1;
        }

        @Basic
        @Column(name = "ob2", nullable = false)
        private int ob2;
        public int getOb2() {
            return ob2;
        }
        public void setOb2(int ob2) {
            this.ob2 = ob2;
        }

        @Basic
        @Column(name = "ob3", nullable = false)
        private int ob3;
        public int getOb3() {
            return ob3;
        }
        public void setOb3(int ob3) {
            this.ob3 = ob3;
        }

        @Basic
        @Column(name = "dtr", nullable = false)
        private int dtr;
        public int getDtr() {
            return dtr;
        }
        public void setDtr(int dtr) {
            this.dtr = dtr;
        }

        @Basic
        @Column(name = "kpb", nullable = false)
        private short kpb;
        public short getKpb() {
            return kpb;
        }
        public void setKpb(short kpb) {
            this.kpb = kpb;
        }

        @Id
        @Column(name = "pornom", nullable = false)
        private short pornom;
        public short getPornom() {
            return pornom;
        }
        public void setPornom(short pornom) {
            this.pornom = pornom;
        }



        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            ILbgEntity that = (ILbgEntity) o;

            if (sidO != that.sidO) return false;
            if (pnrO != that.pnrO) return false;
            if (ob1 != that.ob1) return false;
            if (ob2 != that.ob2) return false;
            if (ob3 != that.ob3) return false;
            if (dtr != that.dtr) return false;
            if (kpb != that.kpb) return false;
            if (pornom != that.pornom) return false;

            return true;
        }

        @Override
        public int hashCode() {
            int result = sidO;
            result = 31 * result + (int) pnrO;
            result = 31 * result + ob1;
            result = 31 * result + ob2;
            result = 31 * result + ob3;
            result = 31 * result + dtr;
            result = 31 * result + (int) kpb;
            result = 31 * result + (int) pornom;
            return result;
        }
    }
package bg.moi.domain.models;

import javax.persistence.Column;
import javax.persistence.Id;
import java.io.Serializable;

public class ILbgEntityPK implements Serializable {
    private int sidO;
    private short pnrO;
    private short pornom;

    @Column(name = "sid_o", nullable = false, precision = 0)
    @Id
    public int getSidO() {
        return sidO;
    }

    public void setSidO(int sidO) {
        this.sidO = sidO;
    }

    @Column(name = "pnr_o", nullable = false)
    @Id
    public short getPnrO() {
        return pnrO;
    }

    public void setPnrO(short pnrO) {
        this.pnrO = pnrO;
    }

    @Column(name = "pornom", nullable = false)
    @Id
    public short getPornom() { return pornom; }

    public void setPornom(short pornom) { this.pornom = pornom; }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ILbgEntityPK that = (ILbgEntityPK) o;

        if (sidO != that.sidO) return false;
        if (pnrO != that.pnrO) return false;
        if (pornom != that.pornom) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = sidO;
        result = 31 * result + (int) pnrO;
        result = 31 * result + (int) pornom;
        return result;
    }
}

从数据库生成类。

例外是:

  

org.springframework.beans.factory.BeanCreationException:错误   创建名为&entityManagerFactory&#39;的bean在类路径中定义   资源   [组织/ springframework的/引导/自动配置/ ORM / JPA / HibernateJpaAutoConfiguration.class]:   调用init方法失败;嵌套异常是   javax.persistence.PersistenceException:[PersistenceUnit:default]   无法构建Hibernate SessionFactory   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)   〜[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] at   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)   〜[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] at   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)   〜[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] at   org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306)   〜[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] at   org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)   〜[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] at   org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)   〜[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] at   org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)   〜[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] at   org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081)   〜[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE] at   org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:856)   〜[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE] at   org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)   〜[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE] at   org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737)   [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at   org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370)   [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at   org.springframework.boot.SpringApplication.run(SpringApplication.java:314)   [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at   org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)   [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at   org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)   [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at   bg.moi.Applicaion.main(Applicaion.java:13)[classes /:na]引起:   javax.persistence.PersistenceException:[PersistenceUnit:default]   无法构建Hibernate SessionFactory   org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:954)   〜[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final] at   org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:882)   〜[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final] at   org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)   〜[spring-orm-4.3.7.RELEASE.jar:4.3.7.RELEASE] at   org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353)   〜[spring-orm-4.3.7.RELEASE.jar:4.3.7.RELEASE] at   org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:370)   〜[spring-orm-4.3.7.RELEASE.jar:4.3.7.RELEASE] at   org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:359)   〜[spring-orm-4.3.7.RELEASE.jar:4.3.7.RELEASE] at   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)   〜[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] at   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)   〜[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] ... 15个常见帧   省略引起:   org.hibernate.tool.schema.spi.SchemaManagementException:无法   执行模式管理到JDBC目标[alter table i_lbg add   约束外键(sid_o,pnr_o)引用l_bg约束   FK422xjl1722nwcy2wxv5yg6nbx] at   org.hibernate.tool.schema.internal.TargetDatabaseImpl.accept(TargetDatabaseImpl.java:59)   〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at   org.hibernate.tool.schema.internal.SchemaMigratorImpl.applySqlString(SchemaMigratorImpl.java:431)   〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at   org.hibernate.tool.schema.internal.SchemaMigratorImpl.applySqlStrings(SchemaMigratorImpl.java:420)   〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at   org.hibernate.tool.schema.internal.SchemaMigratorImpl.applyForeignKeys(SchemaMigratorImpl.java:386)   〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at   org.hibernate.tool.schema.internal.SchemaMigratorImpl.doMigrationToTargets(SchemaMigratorImpl.java:214)   〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at   org.hibernate.tool.schema.internal.SchemaMigratorImpl.doMigration(SchemaMigratorImpl.java:60)   〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at   org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:134)   〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at   org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:101)   〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at   org.hibernate.internal.SessionFactoryImpl。(SessionFactoryImpl.java:472)   〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at   org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444)   〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at   org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879)   〜[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final] ... 21   常见的帧省略引起的:java.sql.SQLException:一个约束   列集上已存在相同类型的。在   com.informix.util.IfxErrMsg.buildException(IfxErrMsg.java:474)   〜[jdbc-4.10.8.1.jar:4.10.8.1] at   com.informix.util.IfxErrMsg.getSQLException(IfxErrMsg.java:443)   〜[jdbc-4.10.8.1.jar:4.10.8.1] at   com.informix.util.IfxErrMsg.getSQLException(IfxErrMsg.java:394)   〜[jdbc-4.10.8.1.jar:4.10.8.1] at   com.informix.jdbc.IfxSqli.addException(IfxSqli.java:3191)   〜[jdbc-4.10.8.1.jar:4.10.8.1] at   com.informix.jdbc.IfxSqli.receiveError(IfxSqli.java:3472)   〜[jdbc-4.10.8.1.jar:4.10.8.1] at   com.informix.jdbc.IfxSqli.dispatchMsg(IfxSqli.java:2379)   〜[jdbc-4.10.8.1.jar:4.10.8.1] at   com.informix.jdbc.IfxSqli.receiveMessage(IfxSqli.java:2304)   〜[jdbc-4.10.8.1.jar:4.10.8.1] at   com.informix.jdbc.IfxSqli.executeCommand(IfxSqli.java:874)   〜[jdbc-4.10.8.1.jar:4.10.8.1] at   com.informix.jdbc.IfxResultSet.executeUpdate(IfxResultSet.java:294)   〜[jdbc-4.10.8.1.jar:4.10.8.1] at   com.informix.jdbc.IfxStatement.executeUpdateImpl(IfxStatement.java:1094)   〜[jdbc-4.10.8.1.jar:4.10.8.1] at   com.informix.jdbc.IfxStatement.executeUpdateBody(IfxStatement.java:391)   〜[jdbc-4.10.8.1.jar:4.10.8.1] at   com.informix.jdbc.IfxStatement.executeUpdate(IfxStatement.java:279)   〜[jdbc-4.10.8.1.jar:4.10.8.1] at   org.hibernate.tool.schema.internal.TargetDatabaseImpl.accept(TargetDatabaseImpl.java:56)   〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] ... 31个常见帧   省略:java.sql.SQLException:null at   com.informix.util.IfxErrMsg.buildException(IfxErrMsg.java:474)   〜[jdbc-4.10.8.1.jar:4.10.8.1] at   com.informix.util.IfxErrMsg.getSQLException(IfxErrMsg.java:443)   〜[jdbc-4.10.8.1.jar:4.10.8.1] at   com.informix.util.IfxErrMsg.getSQLException(IfxErrMsg.java:394)   〜[jdbc-4.10.8.1.jar:4.10.8.1] at   com.informix.jdbc.IfxSqli.receiveError(IfxSqli.java:3477)   〜[jdbc-4.10.8.1.jar:4.10.8.1] ...省略了39个常用帧

有人有类似的问题吗? 有什么解决方案吗? 这可能是错误吗?!

2 个答案:

答案 0 :(得分:0)

1)您只在实体类上添加<system.serviceModel> <bindings> <basicHttpBinding> <binding maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"> <readerQuotas maxStringContentLength="2147483647" /> </binding> </basicHttpBinding> </bindings> </system.serviceModel> @Id注释。不要将它们放在@Column类中。

2)不要在id类中添加setter ..创建一个接受id字段的构造函数。

3)定义ILbgEntityPK时尝试使用数据库列名,而不是实体字段名:

@JoinColumns

答案 1 :(得分:0)

在@OneToMany

的情况下使用以下代码
@OneToMany(targetEntity = ILbgEntity.class, orphanRemoval = true, cascade = { CascadeType.ALL, CascadeType.REMOVE })
@JoinColumn(name = "sid_o", nullable = false)
@LazyCollection(LazyCollectionOption.FALSE)
@Fetch(FetchMode.SUBSELECT)