实体映射中的重复列:BatchJobConfig列:BATCH_JOB_CONFIG_ID(应映射为insert =“ false” update =“ false”)

时间:2019-03-28 21:57:31

标签: java hibernate rest spring-boot

我正在Main方法中调用createSession方法。

private static void createSession() {

        StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
        Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build();

        SessionFactory factory = meta.getSessionFactoryBuilder().build();
        Session session = factory.openSession();
        Transaction t = session.beginTransaction();

        SessionFactory fact = meta.getSessionFactoryBuilder().build();
        Session ss = factory.openSession();
        Transaction tt = session.beginTransaction();

        BatchJobConfig config = new BatchJobConfig();

        BatchJobConfigDetails details = new BatchJobConfigDetails();

        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        System.out.println("timestamp  ->  "+timestamp);

        BatchJobConfigDetails batchJobConfigDtl = new BatchJobConfigDetails(300, 3, "JAVA", date, "JAVA", date, "N",
                "Some Key", "Some value", "FT", "KEY DESCRIPTION", "Y");
        try {
            config.setBatchJobConfigDtl(batchJobConfigDtl);
            config.setAddUserCd("gdfdgdfgdgd");
            config.setBatchJobConfigId(505);
            config.setActiveInd("YYYY");
            config.setAddUserDtm(timestamp);
            config.setDeleteInd("N");
            config.setEndDtm(timestamp);
            config.setJobDesc("DESCRIPTION");
            config.setJobNm("JOB NAME");
            config.setJobType("Job Type");
            config.setLastUpdtDtm(timestamp);
            config.setLastUpdtUserCd("someone");
            config.setStatus("COMPLETED");
            config.setStartDtm(timestamp);
            ss.save(config);
            tt.commit();
            System.out.println("config session saved");

        } catch (HibernateException exception) {
            System.out.println("Problem creating session factory!!!!!!!!!!");
            exception.printStackTrace();
        } finally {
            fact.close();
            ss.close();
        }

        try {
            details.setAddUserCd("fffffffffffffffffffff");
            details.setAddUserDtm(timestamp);           
            details.setBusinessUpdtInd("dssssssss");
            details.setDeleteInd("NNNNNNNNN");
            details.setKeyDataType("ggggggggggggg");
            details.setKeyDesc("Description.......");
            details.setKeyNm("some key name");
            details.setKeyVal("someval");
            details.setLastUpdtDtm(timestamp);
            details.setLastUpdtUserCd("last user");
            BatchJobConfig batchJobConfig = new BatchJobConfig(1L, "JAVA", date, "C#", date, "N", "MissingCK",
                    "JOB DESCRIPTION", "FT", date, date, "COMPLETED", "Y");
            List<BatchJobConfig> b = null;
            b.add(batchJobConfig);
            details.setBatchJobConfigs(b);
            session.save(details);
            t.commit();
            System.out.println("details session saved");

        } catch (HibernateException exception) {
            System.out.println("Problem creating session factory....");
            exception.printStackTrace();
        } finally {
            factory.close();
            session.close();
        }

    }

我有以下两个实体。

BatchJobConfig.java

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;

import javax.persistence.*;


/**
 * The persistent class for the BATCH_JOB_CONFIG database table.
 * 
 */
@Entity
@Table(name="BATCH_JOB_CONFIG")
@NamedQuery(name="BatchJobConfig.findAll", query="SELECT b FROM BatchJobConfig b")
public class BatchJobConfig implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="BATCH_JOB_CONFIG_ID")
    private long batchJobConfigId;

    @Column(name="ADD_USER_CD")
    private String addUserCd;

    @Column(name="ADD_USER_DTM")
    private Timestamp addUserDtm;

    @Column(name="LAST_UPDT_USER_CD")
    private String lastUpdtUserCd;

    @Column(name="LAST_UPDT_DTM")
    private Timestamp lastUpdtDtm;

    @Column(name="DELETE_IND")
    private String deleteInd;

    @Column(name="JOB_NM")
    private String jobNm;

    @Column(name="JOB_DESC")
    private String jobDesc;

    @Column(name="JOB_TYPE")
    private String jobType;

    @Column(name="START_DTM")
    private Timestamp startDtm;

    @Column(name="END_DTM")
    private Timestamp endDtm;

    @Column(name="STATUS")
    private String status;

    @Column(name="ACTIVE_IND")
    private String activeInd;

    public Timestamp getAddUserDtm() {
        return addUserDtm;
    }

    public void setAddUserDtm(Timestamp addUserDtm) {
        this.addUserDtm = addUserDtm;
    }

    public String getLastUpdtUserCd() {
        return lastUpdtUserCd;
    }

    public void setLastUpdtUserCd(String lastUpdtUserCd) {
        this.lastUpdtUserCd = lastUpdtUserCd;
    }

    public Timestamp getLastUpdtDtm() {
        return lastUpdtDtm;
    }

    public void setLastUpdtDtm(Timestamp lastUpdtDtm) {
        this.lastUpdtDtm = lastUpdtDtm;
    }

    public String getDeleteInd() {
        return deleteInd;
    }

    public void setDeleteInd(String deleteInd) {
        this.deleteInd = deleteInd;
    }

    public String getJobNm() {
        return jobNm;
    }

    public void setJobNm(String jobNm) {
        this.jobNm = jobNm;
    }

    public String getJobDesc() {
        return jobDesc;
    }

    public void setJobDesc(String jobDesc) {
        this.jobDesc = jobDesc;
    }

    public String getJobType() {
        return jobType;
    }

    public void setJobType(String jobType) {
        this.jobType = jobType;
    }

    public Timestamp getStartDtm() {
        return startDtm;
    }

    public void setStartDtm(Timestamp startDtm) {
        this.startDtm = startDtm;
    }

    public Timestamp getEndDtm() {
        return endDtm;
    }

    public void setEndDtm(Timestamp endDtm) {
        this.endDtm = endDtm;
    }

    public String getStatus() {
        return status;
    }

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

    public String getActiveInd() {
        return activeInd;
    }

    public void setActiveInd(String activeInd) {
        this.activeInd = activeInd;
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }

    //bi-directional many-to-one association to BatchJobConfigDtl
    @ManyToOne
    @JoinColumn(name="BATCH_JOB_CONFIG_ID", referencedColumnName="BATCH_JOB_CONFIG_ID")
    private BatchJobConfigDetails batchJobConfigDtl;

    public BatchJobConfig() {
    }

    public BatchJobConfig(long l, String string, Date date, String string2, Date date2, String string3, String string4,
            String string5, String string6, Date date3, Date date4, String string7, String string8) {
        // TODO Auto-generated constructor stub
    }

    public long getBatchJobConfigId() {
        return this.batchJobConfigId;
    }

    public void setBatchJobConfigId(long batchJobConfigId) {
        this.batchJobConfigId = batchJobConfigId;
    }

    public String getAddUserCd() {
        return this.addUserCd;
    }

    public void setAddUserCd(String addUserCd) {
        this.addUserCd = addUserCd;
    }

    public BatchJobConfigDetails getBatchJobConfigDtl() {
        return this.batchJobConfigDtl;
    }

    public void setBatchJobConfigDtl(BatchJobConfigDetails batchJobConfigDtl) {
        this.batchJobConfigDtl = batchJobConfigDtl;
    }

}

BatchJobConfigDetails.java

import java.io.Serializable;
import javax.persistence.*;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * The persistent class for the BATCH_JOB_CONFIG_DTLS database table.
 * 
 */
@Entity
@Table(name = "BATCH_JOB_CONFIG_DTLS")
@NamedQuery(name = "BatchJobConfigDtl.findAll", query = "SELECT b FROM BatchJobConfigDtl b")
public class BatchJobConfigDetails implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "BATCH_JOB_CONFIG_DTLS_ID")
    private long batchJobConfigDtlsId;

    @Column(name = "ADD_USER_CD")
    private String addUserCd;

    @Column(name = "ADD_USER_DTM")
    private Timestamp addUserDtm;

    @Column(name = "BUSINESS_UPDT_IND")
    private String businessUpdtInd;

    @Column(name = "DELETE_IND")
    private String deleteInd;

    @Column(name = "KEY_DATA_TYPE")
    private String keyDataType;

    @Column(name = "KEY_DESC")
    private String keyDesc;

    @Column(name = "KEY_NM")
    private String keyNm;

    @Column(name = "KEY_VAL")
    private String keyVal;

    @Column(name = "LAST_UPDT_DTM")
    private Timestamp lastUpdtDtm;

    @Column(name = "LAST_UPDT_USER_CD")
    private String lastUpdtUserCd;

    // bi-directional many-to-one association to BatchJobConfig
    @OneToMany(mappedBy = "batchJobConfigDtl")
    private List<BatchJobConfig> batchJobConfigs;

    public BatchJobConfigDetails() {
    }

    public BatchJobConfigDetails(int i, int j, String string, Date date, String string2, Date date2, String string3,
            String string4, String string5, String string6, String string7, String string8) {
        // TODO Auto-generated constructor stub
    }

    public BatchJobConfigDetails(int i, BatchJobConfig config1, String string, Date date, String string2, Date date2,
            String string3, String string4, String string5, String string6, String string7, String string8) {
        // TODO Auto-generated constructor stub
    }

    public long getBatchJobConfigDtlsId() {
        return this.batchJobConfigDtlsId;
    }

    public void setBatchJobConfigDtlsId(long batchJobConfigDtlsId) {
        this.batchJobConfigDtlsId = batchJobConfigDtlsId;
    }

    public String getAddUserCd() {
        return this.addUserCd;
    }

    public void setAddUserCd(String addUserCd) {
        this.addUserCd = addUserCd;
    }

    public Timestamp getAddUserDtm() {
        return this.addUserDtm;
    }

    public void setAddUserDtm(Timestamp addUserDtm) {
        this.addUserDtm = addUserDtm;
    }

    public String getBusinessUpdtInd() {
        return this.businessUpdtInd;
    }

    public void setBusinessUpdtInd(String businessUpdtInd) {
        this.businessUpdtInd = businessUpdtInd;
    }

    public String getDeleteInd() {
        return this.deleteInd;
    }

    public void setDeleteInd(String deleteInd) {
        this.deleteInd = deleteInd;
    }

    public String getKeyDataType() {
        return this.keyDataType;
    }

    public void setKeyDataType(String keyDataType) {
        this.keyDataType = keyDataType;
    }

    public String getKeyDesc() {
        return this.keyDesc;
    }

    public void setKeyDesc(String keyDesc) {
        this.keyDesc = keyDesc;
    }

    public String getKeyNm() {
        return this.keyNm;
    }

    public void setKeyNm(String keyNm) {
        this.keyNm = keyNm;
    }

    public String getKeyVal() {
        return this.keyVal;
    }

    public void setKeyVal(String keyVal) {
        this.keyVal = keyVal;
    }

    public Timestamp getLastUpdtDtm() {
        return this.lastUpdtDtm;
    }

    public void setLastUpdtDtm(Timestamp lastUpdtDtm) {
        this.lastUpdtDtm = lastUpdtDtm;
    }

    public String getLastUpdtUserCd() {
        return this.lastUpdtUserCd;
    }

    public void setLastUpdtUserCd(String lastUpdtUserCd) {
        this.lastUpdtUserCd = lastUpdtUserCd;
    }

    public List<BatchJobConfig> getBatchJobConfigs() {
        return this.batchJobConfigs;
    }

    public void setBatchJobConfigs(List<BatchJobConfig> batchJobConfigs) {
        this.batchJobConfigs = batchJobConfigs;
    }

    public BatchJobConfig addBatchJobConfig(BatchJobConfig batchJobConfig) {
        getBatchJobConfigs().add(batchJobConfig);
        batchJobConfig.setBatchJobConfigDtl(this);

        return batchJobConfig;
    }

    public BatchJobConfig removeBatchJobConfig(BatchJobConfig batchJobConfig) {
        getBatchJobConfigs().remove(batchJobConfig);
        batchJobConfig.setBatchJobConfigDtl(null);

        return batchJobConfig;
    }

}

batchJob.hbm.xml

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="BatchJobConfig" table="BATCH_JOB_CONFIG">

        <meta attribute="class-description">
            This class contains the BATCH_JOB_CONFIG.
        </meta>

        <id name="batchJobConfigId" type="int" column="BATCH_JOB_CONFIG_ID">
            <generator class="native" />
        </id>

        <property name="addUserCd" column="ADD_USER_CD" type="string" />
        <property name="addUserDtm" column="ADD_USER_DTM" type="timestamp" />
        <property name="lastUpdtUserCd" column="LAST_UPDT_USER_CD"
            type="string" />
        <property name="lastUpdtDtm" column="LAST_UPDT_DTM" type="timestamp" />
        <property name="deleteInd" column="DELETE_IND" type="string" />
        <property name="jobNm" column="JOB_NM" type="string" />
        <property name="jobDesc" column="JOB_DESC" type="string" />
        <property name="jobType" column="JOB_TYPE" type="string" />
        <property name="startDtm" column="START_DTM" type="timestamp" />
        <property name="status" column="STATUS" type="string" />
        <property name="endDtm" column="END_DTM" type="timestamp" />
        <property name="activeInd" column="ACTIVE_IND" type="string" />

        <many-to-one name="BatchJobConfigDetails" column="BATCH_JOB_CONFIG_ID"
            class="BatchJobConfigDetails" not-null="true" />

    </class>

    <class name="BatchJobConfigDetails" table="BATCH_JOB_CONFIG_DTLS">

        <meta attribute="class-description">
            This class contains the BATCH_JOB_CONFIG_DETAILS.
        </meta>

        <id name="batchJobConfigDtlsId" type="int" column="BATCH_JOB_CONFIG_DTLS_ID">
            <generator class="native" />
        </id>

        <property name="batchJobConfigId" column="BATCH_JOB_CONFIG_ID"
            type="int" />
        <property name="addUserCd" column="ADD_USER_CD" type="string" />
        <property name="addUserDtm" column="ADD_USER_DTM" type="timestamp" />
        <property name="businessUpdtInd" column="BUSINESS_UPDT_IND"
            type="int" />
        <property name="deleteInd" column="DELETE_IND" type="string" />
        <property name="keyDataType" column="KEY_DATA_TYPE" type="string" />
        <property name="keyDesc" column="KEY_DESC" type="string" />
        <property name="keyNm" column="KEY_NM" type="string" />
        <property name="keyVal" column="KEY_VAL" type="string" />
        <property name="lastUpdtDtm" column="LAST_UPDT_DTM" type="timestamp" />
        <property name="lastUpdtUserCd" column="LAST_UPDT_USER_CD" type="string" />

    </class>

</hibernate-mapping>

hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.DB2Dialect</property>
        <property name="hibernate.connection.driver_class">com.ibm.db2.jcc.DB2Driver</property>
        <property name="hibernate.connection.url">jdbc:db2://sdfsfsd:5000df2/dsf</property>
        <property name="hibernate.connection.username">sfs</property>
        <property name="hibernate.connection.password">sfs</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="show_sql">true</property>
        <mapping resource="batchJob.hbm.xml"/>  
    </session-factory>
</hibernate-configuration>

此错误消息是什么意思,您将如何解决?

Exception in thread "main" org.hibernate.MappingException: Repeated column in mapping for entity: BatchJobConfig column: BATCH_JOB_CONFIG_ID (should be mapped with insert="false" update="false")
    at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:862)
    at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:880)
    at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:902)
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:634)
    at org.hibernate.mapping.RootClass.validate(RootClass.java:267)
    at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:347)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:466)
    at com.hms.api.batch.job.config.StartBatchJobConfigApplication.createSession(StartBatchJobConfigApplication.java:67)
    at com.hms.api.batch.job.config.StartBatchJobConfigApplication.main(StartBatchJobConfigApplication.java:34)

此错误消息是什么意思?

如果是,您是如何解决的?请帮我解决问题。

感谢您的提前答复。谢谢!!!!!!!!!!!!!!

1 个答案:

答案 0 :(得分:0)

在您的Java代码中,将多对多关系的名称更改为除当前以外的任何关系:

@ManyToOne
    @JoinColumn(name="BATCH_JOB_CONFIG_ID", referencedColumnName="BATCH_JOB_CONFIG_ID")
    private BatchJobConfigDetails batchJobConfigDtl;

类似

@ManyToOne
    @JoinColumn(name="MY_NEW_NAME", referencedColumnName="BATCH_JOB_CONFIG_ID")
    private BatchJobConfigDetails batchJobConfigDtl;

,并在您的hbm.xml文件中进行相应的更改。

更新: 另外,更改hbl.xml文件中的列名。一秒钟。

来自

<many-to-one name="BatchJobConfigDetails" column="BATCH_JOB_CONFIG_ID"
            class="BatchJobConfigDetails" not-null="true" />

 <many-to-one name="BatchJobConfigDetails" column="MY_NEW_NAME"
                class="BatchJobConfigDetails" not-null="true" />