我正在尝试将数据保存在不同的表中
因为我正在编写主要方法
在这里我想保存BillDetails bd,以及CustomerDetails cd(如果存在更新,否则插入)和BillProductDetails bpd,同时保存它是异常
Exception in thread "main" org.hibernate.id.IdentifierGenerationException: null id generated for:class iland.hbm.BillProductDetails
public static void main(String[] s) {
BillDetails bd = new BillDetails();
bd.setPaymentType("Cash");
DatenTime dnt = new DatenTime();
bd.setAddDate(dnt.getTimeStamp());
bd.setSubTotal(400f);
bd.setVat(20f);
bd.setTotal(420f);
/*Customer details*/
CustomerDetails cd = new CustomerDetails();
cd.setId(107L);
cd.setName("Nita");
cd.setAddress("India");
cd.setDob(new Date(2014, 11, 1));
cd.setAnniversery(new Date(2014, 11, 1));
bd.setCustomerDetails(cd);
/*Bill Product Details*/
BillProductDetails bpd = new BillProductDetails();
bpd.setQty(4);
bpd.setSellingPrice(420.0F);
bpd.setUnitPrice(400.0F);
bpd.setDiscountPercent(10);
ProductDetails pd = new ProductDetails();
pd.setBarcode("111");
pd.setId(1001L);
bpd.setProductDetails(pd);
bpd.setBillDetails(bd);
bd.getBillProductList().add(bpd);
BillDAO dao = new BillDAO();
dao.insert(bd);
}
BillDetails.hbm.xml
<hibernate-mapping>
<class name="iland.hbm.BillDetails" table="bill_details" catalog="retail_shop">
<id name="billNo" type="java.lang.Long">
<column name="bill_no" />
<generator class="identity" />
</id>
<many-to-one name="customerDetails" class="iland.hbm.CustomerDetails" fetch="select" cascade="all">
<column name="customer_id" not-null="true" />
</many-to-one>
<many-to-one name="client" class="iland.hbm.Client" fetch="select" cascade="all">
<column name="cid" not-null="true" />
</many-to-one>
<property name="subTotal" type="big_decimal">
<column name="sub_total" precision="15" />
</property>
<property name="vat" type="big_decimal">
<column name="vat" precision="15" />
</property>
<property name="total" type="big_decimal">
<column name="total" precision="15" />
</property>
<property name="paymentType" type="string">
<column name="payment_type" length="30" not-null="true" />
</property>
<property name="status" type="string">
<column name="status" length="20" />
</property>
<property name="addDate" type="timestamp">
<column name="add_date" length="19" not-null="true" />
</property>
<bag name="billProductList" table="bill_product_details" inverse="true" lazy="false" fetch="join" cascade="all">
<key>
<column name="bill_no" not-null="true" />
</key>
<one-to-many class="iland.hbm.BillProductDetails" />
</bag>
<set name="billPaidDetailses" table="bill_paid_details" inverse="true" lazy="false" fetch="join" cascade="all">
<key>
<column name="bill_no" not-null="true" />
</key>
<one-to-many class="iland.hbm.BillPaidDetails" />
</set>
</class>
</hibernate-mapping>
BillProductDetails.hbm.xml
<hibernate-mapping>
<class name="iland.hbm.BillProductDetails" table="bill_product_details" catalog="retail_shop">
<composite-id name="id" class="iland.hbm.BillProductDetailsId">
<key-property name="productId" type="long">
<column name="product_id" />
</key-property>
<key-property name="billNo" type="long">
<column name="bill_no" />
</key-property>
</composite-id>
<many-to-one name="client" class="iland.hbm.Client" fetch="select">
<column name="cid" not-null="true" />
</many-to-one>
<many-to-one name="billDetails" class="iland.hbm.BillDetails" update="false" insert="false" fetch="select">
<column name="bill_no" not-null="true" />
</many-to-one>
<many-to-one name="productDetails" class="iland.hbm.ProductDetails" update="false" insert="false" fetch="select">
<column name="product_id" not-null="true" />
</many-to-one>
<property name="qty" type="long">
<column name="qty" not-null="true" />
</property>
<property name="unitPrice" type="big_decimal">
<column name="unit_price" precision="15" not-null="true" />
</property>
<property name="sellingPrice" type="big_decimal">
<column name="selling_price" precision="15" not-null="true" />
</property>
<property name="discountPercent" type="java.lang.Integer">
<column name="discount_percent" />
</property>
</class>
</hibernate-mapping>
DAO
public class BillDAO {
public int insert(BillDetails obj) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
session.save(obj);
session.getTransaction().commit();
return 1;
}
例外:
Exception in thread "main" org.hibernate.id.IdentifierGenerationException: null id generated for:class iland.hbm.BillProductDetails
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:123)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:685)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:677)
at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:252)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:392)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:335)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:204)
at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:425)
at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:362)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:338)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:204)
at org.hibernate.engine.Cascade.cascade(Cascade.java:161)
at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:475)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:353)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:713)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:701)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:697)
at iland.bill.BillDAO.insert(BillDAO.java:30)
at iland.bill.BillNewAction.main(BillNewAction.java:76)
如何解决此问题。
编辑:
我的BillDetails
Pojo
public class BillDetails implements java.io.Serializable {
private Long billNo;
private CustomerDetails customerDetails;
private Client client;
private BigDecimal subTotal;
private BigDecimal vat;
private BigDecimal total;
private String paymentType;
private String status;
private Timestamp addDate;
private Set billPaidDetailses = new HashSet(0);
private List<BillProductDetails> billProductList = new ArrayList<BillProductDetails>();
//getter setter
}
public class BillProductDetails implements java.io.Serializable {
private BillProductDetailsId id;
private Client client;
private BillDetails billDetails;
private ProductDetails productDetails;
private long qty;
private BigDecimal unitPrice;
private BigDecimal sellingPrice;
private Integer discountPercent;
//getter and setter
}
public class BillProductDetailsId implements java.io.Serializable {
private long productId;
private long billNo;
//getter and setter
}
答案 0 :(得分:0)
你需要:
session.close();
在insert()方法的末尾。
确保将Cascade.ALL设置为:
确保所有id都有@Id和@GeneratedValue设置
@Id @GeneratedValue
Integer getId() { ... };
您的映射有:
<bag name="billProductList" table="bill_product_details" inverse="true" lazy="false" fetch="join" cascade="all">
<key>
<column name="bill_no" not-null="true" />
</key>
<one-to-many class="iland.hbm.BillProductDetails" />
</bag>
<many-to-one name="billDetails" class="iland.hbm.BillDetails" update="false" insert="false" fetch="select">
<column name="bill_no" not-null="true" />
</many-to-one>
所以,如果你选择你的包是“反向的”,你需要让多对一方控制这种关联。因此,您需要删除:
来自
您也可以控制一对多,这意味着删除“反向”属性,但这是不可取的,因为每当您更改列表时它将始终删除/重新插入内容。
这样,这一端就能控制关联。