2
votes

I try to connect Lbg to iLbg with OneToMany annotation

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

Classes generate from database.

Exception is:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory at 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] Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory at 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 common frames omitted Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Unable to execute schema management to JDBC target [alter table i_lbg add constraint foreign key (sid_o, pnr_o) references l_bg constraint 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 common frames omitted Caused by: java.sql.SQLException: A constraint of the same type already exists on the column set. 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.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 common frames omitted Caused by: 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 common frames omitted

Did someone have a similar problem? Is there any solution? Is possible this is bug ?!

2

2 Answers

0
votes

1) You put @Id and @Column annotations on the entity class only. Do not put them in the ILbgEntityPK class.

2) Do not add setters in the id class.. create a constructor accepting the id fields.

3) When defining the @JoinColumns try to use database column names, not entity field names:

@OneToMany(targetEntity = ILbgEntity.class,fetch = FetchType.LAZY)
@JoinColumns({@JoinColumn(name = "sid_o",referencedColumnName = "sid_o",insertable = false,updatable = false),
        @JoinColumn(name = "pnr_o" ,referencedColumnName = "pnr_o",insertable = false,updatable = false)})
0
votes

Use below code in case of @OneToMany

@OneToMany(targetEntity = ILbgEntity.class, orphanRemoval = true, cascade = { CascadeType.ALL, CascadeType.REMOVE })
@JoinColumn(name = "sid_o", nullable = false)
@LazyCollection(LazyCollectionOption.FALSE)
@Fetch(FetchMode.SUBSELECT)