您好, 我是hibernate的新手。我有一个类Customer与Order类有一对多的关系,Oder类与Item类有一对多的关系,而且有许多与Customer的一对多。与Item类有一对多的关系产品类别。 我的hbm.xml如下所示
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.xyz.hibernet.dto.Customer" table="CUSTOMER" abstract="true">
<id name="cid" type="java.lang.Integer" column="CUST_ID" >
<generator class="native"/>
</id>
<property name="cname" column="CUST_NAME"/>
<list name="orderList" cascade="all">
<key column="CUST_ID"/>
<index column="idx"/>
<one-to-many class="com.activenet.hibernet.dto.Order"/>
</list>
</class>
<class name="com.xyz.hibernet.dto.Order" table="ORDER">
<id name="oid" type="java.lang.Integer" column="O_ID" >
<generator class="native"/>
</id>
<property name="odate" column="ORDER_DATE"/>
<property name="amt" column="AMOUNT"/>
<set name="itemSet" inverse="true" cascade="all">
<key column="O_ID"/>
<one-to-many class="com.activenet.hibernet.dto.Item"/>
</set>
<many-to-one name="oCustomer" class="com.activenet.hibernet.dto.Customer" column="CUST_ID"/>
</class>
<class name="com.xyz.hibernet.dto.Item" table="ITEM">
<id name="iid" type="java.lang.Integer" column="ITEM_ID" >
<generator class="native"/>
</id>
<property name="quantity" column="ORDER_QTY"/>
<many-to-one name="product" class="com.activenet.hibernet.dto.Product" column="PRODUCT_ID"/>
</class>
<class name="com.xyz.hibernet.dto.Product" table="PRODUCT">
<id name="pid" type="java.lang.Integer" >
<column name="PROD_ID" />
<generator class="native"/>
</id>
<property name="pname" column="PROD_NAME"/>
<property name="price" column="PROD_PRICE"/>
<property name="descr" column="PROD_DESC"/>
</class>
<!-- <query name="findAllCustomers">
<![CDATA[from com.xyz.hibernet.dto.Customer c where c.name=:name]]>
</query>-->
</hibernate-mapping>
并且
客户类如下所示
import java.util.List;
public class Customer {
private int cid;
private String cname;
private List orderList;
public Customer()
{
super();
}
public Customer(int cid,String cname, List orderList)
{
super();
this.cid=cid;
this.cname=cname;
this.orderList=orderList;
}
--setter and getter
}
Product.java
public class Product {
private int pid;
private String pname;
private double price;
private String descr;
public Product() {
super();
}
public Product(int pid,String pname,double price,String descr) {
super();
this.pid=pid;
this.pname=pname;
this.price=price;
this.descr=descr;
}
Setter and getter methods----here
}
Order.java
public class Order {
private int oid;
private Date odate;
private double amt;
private Set itemSet;
private Customer oCustomer;
public Order()
{
super();
}
public Order(int oid,Date odate,double amt, Set itemSet)
{
super();
this.oid=oid;
this.odate=odate;
this.itemSet=itemSet;
this.amt=amt;
}
-- setter and getter
}
Item.java
public class Item {
private int iid;
private Product product;
private int quantity;
public Item ()
{
super();
}
public Item (int iid,Product product,int quantity)
{
super();
this.iid=iid;
this.product=product;
this.quantity=quantity;
}
}
我的测试类如下所示
public class TestCustomer {
public static void main(String args[])
{
SessionFactory oSessionFac=new Configuration().configure().buildSessionFactory();
Session oSession=oSessionFac.openSession();
Transaction oTransaction=oSession.beginTransaction();
//-------------------------->New product Insertion<-------------------------------
Product product1=new Product(0,"Master in EJB",350.00,"Good Luck");
Product product2=new Product(0,"Master in STRUTS",250.00,"Good Luck");
Product product3=new Product(0,"Master in WEBSERVICE",150.00,"Good Luck");
Product product4=new Product(0,"Master in JSF",450.00,"Good Luck");
Product product5=new Product(0,"Master in SPRING",550.00,"Good Luck");
oSession.save(product1);
oSession.save(product2);
oSession.save(product3);
oSession.save(product4);
oSession.save(product5);
oTransaction.commit();
System.out.println("::::::::::::product is inserted::::::::::::::");
//<----------------------Place One Order into new Customer------------------------>
Product prod1=(Product)oSession.load(Product.class, 1);
Product prod2=(Product)oSession.load(Product.class, 2);
Product prod3=(Product)oSession.load(Product.class, 3);
Item oItem1 =new Item(0,prod1,1);
Item oItem2 =new Item(0,prod2,1);
Item oItem3 =new Item(0,prod3,1);
Item oItem4 =new Item(0,prod1,2);
Set<Item> set=new HashSet<Item>();
set.add(oItem1);set.add(oItem2);set.add(oItem3);set.add(oItem4);
Order oOrder=new Order(0,Calendar.getInstance().getTime(),1200.00,set);
List<Order> list=new ArrayList<Order>();
list.add(oOrder);
oSession.persist(oOrder);
Customer customer1=new Customer(0,"ABC",list);
//oOrder=new Order(0,Calendar.getInstance().getTime(),1200.00,set,customer1);
oSession.saveOrUpdate(customer1);
oTransaction.commit();
oSession.flush();
}
}
我使用过org.hibernate.dialect.MySQLMyISAMDialect方言
在尝试执行代码时,它正在说 您的SQL语法中有错误;检查与MySQL服务器版本对应的手册,以便在第1行的'ORDER(ORDER_DATE,AMOUNT,CUST_ID)值('2012-03-07 11:58:55',1200.0,null)附近使用正确的语法< / strong>
我尝试了很多但无法修复它。请任何人帮我解决问题
答案 0 :(得分:0)
由于Customer
(即oCustomer
)中的Order
(即oOrder
)实例未设置(即为NULL),因此hibernate将为列{{}插入NULL 1}}。它绝对会抛出错误,因为此列对客户的FK约束不能接受NULL。
首先,让订单有责任通过将客户内ORDER.CUST_ID
的{{1}}属性设置为true来处理客户与订单之间的关系。
(初学者IMO很难理解PS inverse
属性,但您可以轻松搜索有关此属性的许多资源。通常,在<set>
关系中,我们将其设置为{{1在集合标记中(即inverse
,<one-to-many>
))
然后在保存之前将customer1设置为oOrder的客户,喜欢这个:
true