简体   繁体   English

使用Hibernate自动查询生成未创建表

[英]Table not getting created using Hibernate Automatic Query Generation

Getting Below Error 低于错误

Caused by: org.apache.derby.client.am.SqlException: Table/View 'SO_ITEM_DTLS' does not exist.
    at org.apache.derby.client.am.Statement.completeSqlca(Unknown Source)
    at org.apache.derby.client.net.NetStatementReply.parsePrepareError(Unknown Source)
    at org.apache.derby.client.net.NetStatementReply.parsePRPSQLSTTreply(Unknown Source)
    at org.apache.derby.client.net.NetStatementReply.readPrepareDescribeOutput(Unknown Source)
    at org.apache.derby.client.net.StatementReply.readPrepareDescribeOutput(Unknown Source)
    at org.apache.derby.client.net.NetStatement.readPrepareDescribeOutput_(Unknown Source)
    at org.apache.derby.client.am.Statement.readPrepareDescribeOutput(Unknown Source)
    at org.apache.derby.client.am.PreparedStatement.readPrepareDescribeInputOutput(Unknown Source)
    at org.apache.derby.client.am.PreparedStatement.flowPrepareDescribeInputOutput(Unknown Source)
    at org.apache.derby.client.am.PreparedStatement.prepare(Unknown Source)
    at org.apache.derby.client.am.Connection.prepareStatementX(Unknown Source)
    ... 100 more

I am using derby database and hibernate for automatic schema generation. 我正在使用derby数据库并休眠以自动生成模式。 But Table is not getting created into the database while the SQL is getting generated for all the Entities. 但是,在为所有实体生成SQL时,并未将Table创建到数据库中。 Code is having @OneToMany Relationship among two Entity Classes and Parent table also contains EmbeddedID. 代码在两个实体类之间具有@OneToMany关系,并且父表也包含EmbeddedID。 Hibernate Configuration file and Entities classes are as below. Hibernate配置文件和Entities类如下。

hibernate.cfg.xml 的hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
        <property name="hibernate.connection.url">jdbc:derby://localhost:1527/mcodb;create=true;user=mco;password=mco</property>
        <property name="hibernate.connection.username">mco</property>
        <property name="hibernate.connection.password">mco</property>
        <property name="hibernate.hbm2ddl.auto">create</property>
        <property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
        <property name="show_sql">true</property>
        <mapping class="mco.com.billing.app.model.AdminReportingPanel" />
        <mapping class="mco.com.billing.app.model.SODetails" />
        <mapping class="mco.com.billing.app.model.SOItemDetails" />
        <mapping class="mco.com.billing.app.model.BillCategories" />
        <mapping class="mco.com.billing.app.model.BillHeads" />
        <mapping class="mco.com.billing.app.model.Dealers" />
        <mapping class="mco.com.billing.app.model.FinancialYears" />
    </session-factory>
</hibernate-configuration>

SODetails Class SODetails类

package mco.com.billing.app.model;

import java.util.Date;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="SO_DTLS")
public class SODetails {


    @EmbeddedId
    SODetailsEmbeddable soDetailsEmbeddable;

    @Column(name = "LPR_NO", nullable = false, length = 400)
    String lprNo;

    @Column(name = "DEALER_NAME", nullable = false, length = 200)
    String dealerName;

    @Column(name = "SO_DATE")
    Date soDate;

    @Column(name = "VAT", length = 200)
    String vat;

    @Column(name="NO_OF_QUOTATIONS", nullable = false, length=100)
    String noOfQuotations;

    @Column(name="LINKED_SO", nullable = false, length=100)
    String linkedSO;

    @Column(name="BILL_HEAD", nullable = false, length=100)
    String billHead;

    @Column(name="BILL_CATEGORY", nullable = false, length=100)
    String billCategory;

    @Column(name="NO_OF_CSTS", nullable = false, length=100)
    String noOfCSTs;

    @Column(name="NO_OF_CRVS", nullable = false, length=100)
    String noOFCRVs;

    @Column(name = "SO_GRAND_TOTAL_AMOUNT", nullable = false, length = 100)
    String sOGrandTotalAmount;

    @Column(name = "SO_GRAND_TOTAL_ROUND_OFF_AMOUNT", nullable = false, length = 100)
    String sOGrandTotalRoundOfAmount;

    @Column(name = "BILL_GRAND_TOTAL_AMOUNT", length = 100)
    String billGrandTotalAmount;

    @Column(name = "BILL_GRAND_TOTAL_ROUND_OFF_AMOUNT", length = 100)
    String billGrandTotalRoundOffAmount;

    @Column(name="IS_BILL_GENERATED", length = 100)
    boolean isBillGenerated;

    @Column(name="IS_SHORT_CLOSED_SO", length = 100)
    boolean isShortClosedSO;

    @Column(name="IS_SHORT_CLOSED_GENERATED", length = 100)
    boolean isShortClosedGenerated;

    @Column(name="IS_LD_ATTACHED", length = 100)
    boolean isLDAttached;

    @Column(name="LD_AMOUNT", length = 100)
    String lDAmount;

    @Column(name="ITEM_DUE_DATE", length = 100)
    Date itemDueDate;

    @Column(name="NO_OF_DELAY_WEEKS", length = 100)
    String noOfWeeksDelay;

    @Column(name="LD_PERCENTAGE", length = 100)
    String ldPercentage;

    @Column(name="FINAL_AMOUNT_AFTER_LD", length = 100)
    String finalAmountAfterLD;

    @Column(name="AMOUNT_ON_WHICH_LD_CALCULATED", length = 100)
    String amountOnWhichLDCalculated;

    public String getAmountOnWhichLDCalculated() {
        return amountOnWhichLDCalculated;
    }
    public void setAmountOnWhichLDCalculated(String amountOnWhichLDCalculated) {
        this.amountOnWhichLDCalculated = amountOnWhichLDCalculated;
    }
    public String getLdPercentage() {
        return ldPercentage;
    }
    public void setLdPercentage(String ldPercentage) {
        this.ldPercentage = ldPercentage;
    }
    public String getFinalAmountAfterLD() {
        return finalAmountAfterLD;
    }
    public void setFinalAmountAfterLD(String finalAmountAfterLD) {
        this.finalAmountAfterLD = finalAmountAfterLD;
    }
    public Date getItemDueDate() {
        return itemDueDate;
    }
    public void setItemDueDate(Date itemDueDate) {
        this.itemDueDate = itemDueDate;
    }
    public String getNoOfWeeksDelay() {
        return noOfWeeksDelay;
    }
    public void setNoOfWeeksDelay(String noOfWeeksDelay) {
        this.noOfWeeksDelay = noOfWeeksDelay;
    }
    @OneToMany(fetch=FetchType.LAZY, mappedBy="sODetails", cascade = CascadeType.ALL)
    Set<SOItemDetails> setSOItemDetails;

    public String getVat() {
        return vat;
    }
    public void setVat(String vat) {
        this.vat = vat;
    }
    public String getNoOfQuotations() {
        return noOfQuotations;
    }
    public void setNoOfQuotations(String noOfQuotations) {
        this.noOfQuotations = noOfQuotations;
    }
    public String getLinkedSO() {
        return linkedSO;
    }
    public void setLinkedSO(String linkedSO) {
        this.linkedSO = linkedSO;
    }
    public String getBillHead() {
        return billHead;
    }
    public void setBillHead(String billHead) {
        this.billHead = billHead;
    }
    public String getBillCategory() {
        return billCategory;
    }
    public void setBillCategory(String billCategory) {
        this.billCategory = billCategory;
    }

    public String getNoOfCSTs() {
        return noOfCSTs;
    }
    public void setNoOfCSTs(String noOfCSTs) {
        this.noOfCSTs = noOfCSTs;
    }
    public String getNoOFCRVs() {
        return noOFCRVs;
    }
    public void setNoOFCRVs(String noOFCRVs) {
        this.noOFCRVs = noOFCRVs;
    }
    public boolean isShortClosedSO() {
        return isShortClosedSO;
    }
    public void setShortClosedSO(boolean isShortClosedSO) {
        this.isShortClosedSO = isShortClosedSO;
    }
    public boolean isShortClosedGenerated() {
        return isShortClosedGenerated;
    }
    public void setShortClosedGenerated(boolean isShortClosedGenerated) {
        this.isShortClosedGenerated = isShortClosedGenerated;
    }
    public String getlDAmount() {
        return lDAmount;
    }
    public void setlDAmount(String lDAmount) {
        this.lDAmount = lDAmount;
    }
    public String getLprNo() {
        return lprNo;
    }
    public void setLprNo(String lprNo) {
        this.lprNo = lprNo;
    }

    public String getsOGrandTotalAmount() {
        return sOGrandTotalAmount;
    }
    public void setsOGrandTotalAmount(String sOGrandTotalAmount) {
        this.sOGrandTotalAmount = sOGrandTotalAmount;
    }
    public String getsOGrandTotalRoundOfAmount() {
        return sOGrandTotalRoundOfAmount;
    }
    public void setsOGrandTotalRoundOfAmount(String sOGrandTotalRoundOfAmount) {
        this.sOGrandTotalRoundOfAmount = sOGrandTotalRoundOfAmount;
    }
    public String getBillGrandTotalAmount() {
        return billGrandTotalAmount;
    }
    public void setBillGrandTotalAmount(String billGrandTotalAmount) {
        this.billGrandTotalAmount = billGrandTotalAmount;
    }
    public String getBillGrandTotalRoundOffAmount() {
        return billGrandTotalRoundOffAmount;
    }
    public void setBillGrandTotalRoundOffAmount(String billGrandTotalRoundOffAmount) {
        this.billGrandTotalRoundOffAmount = billGrandTotalRoundOffAmount;
    }
    public String getDealerName() {
        return dealerName;
    }
    public void setDealerName(String dealerName) {
        this.dealerName = dealerName;
    }

    public SODetailsEmbeddable getSoDetailsEmbeddable() {
        return soDetailsEmbeddable;
    }
    public void setSoDetailsEmbeddable(SODetailsEmbeddable soDetailsEmbeddable) {
        this.soDetailsEmbeddable = soDetailsEmbeddable;
    }
    public Date getSoDate() {
        return soDate;
    }
    public void setSoDate(Date soDate) {
        this.soDate = soDate;
    }
    public boolean isBillGenerated() {
        return isBillGenerated;
    }
    public void setBillGenerated(boolean isBillGenerated) {
        this.isBillGenerated = isBillGenerated;
    }
    public boolean isLDAttached() {
        return isLDAttached;
    }
    public void setLDAttached(boolean isLDAttached) {
        this.isLDAttached = isLDAttached;
    }
    public Set<SOItemDetails> getSetSOItemDetails() {
        return setSOItemDetails;
    }
    public void setSetSOItemDetails(Set<SOItemDetails> setSOItemDetails) {
        this.setSOItemDetails = setSOItemDetails;
    }
    @Override
    public String toString() {
        return "SODetails [soDetailsEmbeddable=" + soDetailsEmbeddable + ", lprNo=" + lprNo + ", dealerName="
                + dealerName + ", soDate=" + soDate + ", vat=" + vat + ", noOfQuotations=" + noOfQuotations
                + ", linkedSO=" + linkedSO + ", billHead=" + billHead + ", billCategory=" + billCategory + ", noOfCSTs="
                + noOfCSTs + ", noOFCRVs=" + noOFCRVs + ", sOGrandTotalAmount=" + sOGrandTotalAmount
                + ", sOGrandTotalRoundOfAmount=" + sOGrandTotalRoundOfAmount + ", billGrandTotalAmount="
                + billGrandTotalAmount + ", billGrandTotalRoundOffAmount=" + billGrandTotalRoundOffAmount
                + ", isBillGenerated=" + isBillGenerated + ", isShortClosedSO=" + isShortClosedSO
                + ", isShortClosedGenerated=" + isShortClosedGenerated + ", isLDAttached=" + isLDAttached
                + ", lDAmount=" + lDAmount + ", itemDueDate=" + itemDueDate + ", noOfWeeksDelay=" + noOfWeeksDelay
                + ", ldPercentage=" + ldPercentage + ", finalAmountAfterLD=" + finalAmountAfterLD
                + ", amountOnWhichLDCalculated=" + amountOnWhichLDCalculated + ", setSOItemDetails=" + setSOItemDetails
                + "]";
    }
}

SOItemDetails Class SOItemDetails类

package mco.com.billing.app.model;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.MapsId;
import javax.persistence.Table;

@Entity
@Table(name="SO_ITEM_DTLS")
public class SOItemDetails {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "SO_ITEM_DTLS_ID", unique = true, nullable = false, length = 100)
    String soItemDtlsRecordNo;

    @Column(name = "S_NO", nullable = false, length = 100)
    String itemSNo;

    @Column(name = "ITEM_UNIT_TYPE", nullable = false, length = 100)
    String itemUnitType;

    @Column(name = "ITEM_NOMENCLATURE", nullable = false, length = 400)
    String itemNomenclature;

    @Column(name = "FOR_QUANTITY", nullable = false, length = 100)
    String forQuantity;

    @Column(name = "READ_QUANTITY", length = 100)
    String readQuantity;

    @Column(name = "FOR_AMOUNT", nullable = false, length = 100)
    String forAmount;

    @Column(name = "READ_AMOUNT", length = 100)
    String readAmount;

    @Column(name = "SUPPLY_DATE", length = 100)
    Date supplyDate;

    @Column(name = "PRICE", nullable = false, length = 100)
    String price;

    @Column(name = "IS_LD_ITEM")
    boolean isLDItem;

    @MapsId("soDetailsEmbeddable")
    @JoinColumns({@JoinColumn(name="SO_NO_FK", referencedColumnName="SO_NO"),
        @JoinColumn(name="FIN_YEAR_FK", referencedColumnName="FINANCIAL_YEAR")
    })
    @ManyToOne(fetch=FetchType.LAZY)
    private SODetails sODetails;

    public String getSoItemDtlsRecordNo() {
        return soItemDtlsRecordNo;
    }

    public boolean isLDItem() {
        return isLDItem;
    }


    public void setLDItem(boolean isLDItem) {
        this.isLDItem = isLDItem;
    }


    public void setSoItemDtlsRecordNo(String soItemDtlsRecordNo) {
        this.soItemDtlsRecordNo = soItemDtlsRecordNo;
    }

    public String getItemSNo() {
        return itemSNo;
    }

    public void setItemSNo(String itemSNo) {
        this.itemSNo = itemSNo;
    }

    public String getItemUnitType() {
        return itemUnitType;
    }

    public void setItemUnitType(String itemUnitType) {
        this.itemUnitType = itemUnitType;
    }

    public String getItemNomenclature() {
        return itemNomenclature;
    }

    public void setItemNomenclature(String itemNomenclature) {
        this.itemNomenclature = itemNomenclature;
    }

    public String getForQuantity() {
        return forQuantity;
    }

    public void setForQuantity(String forQuantity) {
        this.forQuantity = forQuantity;
    }

    public String getReadQuantity() {
        return readQuantity;
    }

    public void setReadQuantity(String readQuantity) {
        this.readQuantity = readQuantity;
    }

    public String getForAmount() {
        return forAmount;
    }

    public void setForAmount(String forAmount) {
        this.forAmount = forAmount;
    }

    public String getReadAmount() {
        return readAmount;
    }

    public void setReadAmount(String readAmount) {
        this.readAmount = readAmount;
    }

    public Date getSupplyDate() {
        return supplyDate;
    }

    public void setSupplyDate(Date supplyDate) {
        this.supplyDate = supplyDate;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public SODetails getsODetails() {
        return sODetails;
    }

    public void setsODetails(SODetails sODetails) {
        this.sODetails = sODetails;
    }

    @Override
    public String toString() {
        return "SOItemDetails [soItemDtlsRecordNo=" + soItemDtlsRecordNo + ", itemSNo=" + itemSNo + ", itemUnitType="
                + itemUnitType + ", itemNomenclature=" + itemNomenclature + ", forQuantity=" + forQuantity
                + ", readQuantity=" + readQuantity + ", forAmount=" + forAmount + ", readAmount=" + readAmount
                + ", supplyDate=" + supplyDate + ", price=" + price + ", isLDItem=" + isLDItem + ", sODetails="
                + sODetails + "]";
    }
}

SODetailsEmbeddable Class SODetailsEmbeddableable类

package mco.com.billing.app.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class SODetailsEmbeddable implements Serializable{

    private static final long serialVersionUID = 1L;

    @Column(name = "SO_NO", nullable = false, length = 100)
    String soNo;

    @Column(name="FINANCIAL_YEAR",  length=100)
    String financialYear;

    public String getSoNo() {
        return soNo;
    }

    public void setSoNo(String soNo) {
        this.soNo = soNo;
    }

    public String getFinancialYear() {
        return financialYear;
    }

    public void setFinancialYear(String financialYear) {
        this.financialYear = financialYear;
    }

    @Override
    public String toString() {
        return "SODetailsEmbeddable [soNo=" + soNo + ", financialYear=" + financialYear + "]";
    }
}

Transactional Logic function 事务逻辑功能

public void saveSOWithBillingData(SODetails soDetails) {
        Session session=null;
        Transaction tx=null;
        try{
            SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
            session=sessionFactory.openSession();
            tx=session.beginTransaction();
            session.save(soDetails);
            tx.commit();
        }catch(HibernateException ex){
            try{
                if(null!=tx)
                    tx.rollback();
            }catch(RuntimeException e){
                e.printStackTrace();
            }
            throw ex;
        }catch(RuntimeException ex){
            throw ex;
        }finally{
            if(null!=session)
                session.close();
        }
    }

My bad, it was just a silly mistake. 我的错,那只是一个愚蠢的错误。 SOItemDetails table was not getting created because @GeneratedValue is not applicable for String type, it should be of int type. 由于@GeneratedValue不适用于String类型,因此未创建SOItemDetails表,该表应为int类型。 Now need to do below change in SOItemDetails Entity Class. 现在需要在SOItemDetails实体类中进行以下更改。

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "SO_ITEM_DTLS_ID", unique = true, nullable = false, length = 100)
    int soItemDtlsRecordNo;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM