Table not getting created using Hibernate Automatic Query Generation
低于错误
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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 |
我正在使用 derby 数据库和 hibernate 来自动生成模式。
但是在为所有实体生成 SQL 时,表并没有被创建到数据库中。代码在两个实体类之间具有@OneToMany 关系,并且父表也包含 EmbeddedID。 Hibernate 配置文件和实体类如下。
hibernate.cfg.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?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 类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | 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 类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | 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 +"]"; } } |
SODetails可嵌入类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | 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 +"]"; } } |
事务逻辑功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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(); } } |
我的错,这只是一个愚蠢的错误。未创建 SOItemDetails 表,因为 @GeneratedValue 不适用于 String 类型,它应该是 int 类型。现在需要在 SOItemDetails 实体类中进行以下更改。
1 2 3 4 | @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name ="SO_ITEM_DTLS_ID", unique = true, nullable = false, length = 100) int soItemDtlsRecordNo; |