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 javax.persistence.CascadeType; @Entity
@EmbeddedId @Column(name =“LPR_NO”, nullable = false, length = 400) @Column(name =“DEALER_NAME”, nullable = false, length = 200) @Column(name =“SO_DATE”) @Column(name =“VAT”, length = 200) @Column(name=“NO_OF_QUOTATIONS”, nullable = false, length=100) @Column(name=“LINKED_SO”, nullable = false, length=100) @Column(name=“BILL_HEAD”, nullable = false, length=100) @Column(name=“BILL_CATEGORY”, nullable = false, length=100) @Column(name=“NO_OF_CSTS”, nullable = false, length=100) @Column(name=“NO_OF_CRVS”, nullable = false, length=100) @Column(name =“SO_GRAND_TOTAL_AMOUNT”, nullable = false, length = 100) @Column(name =“SO_GRAND_TOTAL_ROUND_OFF_AMOUNT”, nullable = false, length = 100) @Column(name =“BILL_GRAND_TOTAL_AMOUNT”, length = 100) @Column(name =“BILL_GRAND_TOTAL_ROUND_OFF_AMOUNT”, length = 100) @Column(name=“IS_BILL_GENERATED”, length = 100) @Column(name=“IS_SHORT_CLOSED_SO”, length = 100) @Column(name=“IS_SHORT_CLOSED_GENERATED”, length = 100) @Column(name=“IS_LD_ATTACHED”, length = 100) @Column(name=“LD_AMOUNT”, length = 100) @Column(name=“ITEM_DUE_DATE”, length = 100) @Column(name=“NO_OF_DELAY_WEEKS”, length = 100) @Column(name=“LD_PERCENTAGE”, length = 100) @Column(name=“FINAL_AMOUNT_AFTER_LD”, length = 100) @Column(name=“AMOUNT_ON_WHICH_LD_CALCULATED”, length = 100) public String getAmountOnWhichLDCalculated() { public String getVat() { public String getNoOfCSTs() { public String getsOGrandTotalAmount() { public SODetailsEmbeddable getSoDetailsEmbeddable() { |
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; @Entity @Id @Column(name =“S_NO”, nullable = false, length = 100) @Column(name =“ITEM_UNIT_TYPE”, nullable = false, length = 100) @Column(name =“ITEM_NOMENCLATURE”, nullable = false, length = 400) @Column(name =“FOR_QUANTITY”, nullable = false, length = 100) @Column(name =“READ_QUANTITY”, length = 100) @Column(name =“FOR_AMOUNT”, nullable = false, length = 100) @Column(name =“READ_AMOUNT”, length = 100) @Column(name =“SUPPLY_DATE”, length = 100) @Column(name =“PRICE”, nullable = false, length = 100) @Column(name =“IS_LD_ITEM”) @MapsId(“soDetailsEmbeddable”) public String getSoItemDtlsRecordNo() { public boolean isLDItem() {
public void setLDItem(boolean isLDItem) {
public void setSoItemDtlsRecordNo(String soItemDtlsRecordNo) { public String getItemSNo() { public void setItemSNo(String itemSNo) { public String getItemUnitType() { public void setItemUnitType(String itemUnitType) { public String getItemNomenclature() { public void setItemNomenclature(String itemNomenclature) { public String getForQuantity() { public void setForQuantity(String forQuantity) { public String getReadQuantity() { public void setReadQuantity(String readQuantity) { public String getForAmount() { public void setForAmount(String forAmount) { public String getReadAmount() { public void setReadAmount(String readAmount) { public Date getSupplyDate() { public void setSupplyDate(Date supplyDate) { public String getPrice() { public void setPrice(String price) { public SODetails getsODetails() { public void setsODetails(SODetails sODetails) { @Override |
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; @Embeddable private static final long serialVersionUID = 1L; @Column(name =“SO_NO”, nullable = false, length = 100) @Column(name=“FINANCIAL_YEAR”, length=100) public String getSoNo() { public void setSoNo(String soNo) { public String getFinancialYear() { public void setFinancialYear(String financialYear) { @Override |
事务逻辑功能
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(); } } |
- 嗨,Sathish,您正在编写事务逻辑。您能否也粘贴该逻辑。
- <property name=”hibernate.hbm2ddl.auto”>update</property> 因为如果存在它会删除并重新创建。
- @Pradeep 更新了事务逻辑
- @Pradeep 更新不起作用
我的错,这只是一个愚蠢的错误。未创建 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; |
来源:https://www.codenong.com/43610163/