首先说明<property name="hbm2ddl.auto">validate</propert>木有问题;请各位大虾指教,先贴代码:hibernate.cfg.xml<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>    <session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://199.9.10.56:3306/drpis?useUnicode=true&amp;characterEncoding=UTF-8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">validate</property>
<property name="show_sql">true</property>
<!-- <mapping resource="com/rmis/util/tree/Tree.hbm.xml"/>
<mapping resource="com/rmis/util/tree/TreeNode.hbm.xml"/>  -->
<mapping class="cn.com.postek.drp.model.Orders"/>  
<mapping class="cn.com.postek.drp.model.OrdersProductInfo"/> 
    </session-factory></hibernate-configuration>------------------------------------------------------------------------------Entity  Orders,OrdersProductInfo,一对多Orders:package cn.com.postek.drp.model;import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;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.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.CascadeType;;@Entity
@Table(name="orders")
public class Orders implements Serializable {
private static final long serialVersionUID = -7443110270626105958L;
private int id;//序号
private String orderSerialNumber;//订单编号order_serial_number;20
private String status;//订单状态status;8
private BigDecimal amount;//总计amount;(00.00)
private String creator;//创建者creator;20
private Date creationDate;//创建日期creation_date YYYY-MM-DD hh:mm:ss
private Date updateDate;//修改日期update_date  YYYY-MM-DD hh:mm:ss
private String description;//描述 description;500
private Set<OrdersProductInfo> oderInfos=new HashSet<OrdersProductInfo>();
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", unique = true, nullable = false)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name = "order_serial_number", length = 20,nullable = false)
public String getOrderSerialNumber() {
return orderSerialNumber;
}
public void setOrderSerialNumber(String orderSerialNumber) {
this.orderSerialNumber = orderSerialNumber;
}
@Column(name = "status", length = 8,nullable = false)
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@Column(name = "amount", nullable = false)
public BigDecimal getAmount() {
return amount;
}
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
@Column(name = "creator", length = 20,nullable = false) public String getCreator() {
return creator;
}
public void setCreator(String creator) {
this.creator = creator;
}

@Column(name = "creation_date",nullable = false)
@Temporal(value = TemporalType.TIMESTAMP)  
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
@Temporal(value = TemporalType.TIMESTAMP) //不用set,hibernate会自动把当前时间写入
@Column(name = "update_date",length = 20,nullable = false) 
public Date getUpdateDate() {
return updateDate;
}
public void setUpdateDate(Date updateDate) {
this.updateDate = updateDate;
}
@Column(name = "description",nullable = true)
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
} @OneToMany(mappedBy = "orders", cascade = CascadeType.ALL,fetch = FetchType.EAGER)   
public Set<OrdersProductInfo> getOderInfos() {
return oderInfos;
}
public void setOderInfos(Set<OrdersProductInfo> oderInfos) {
this.oderInfos = oderInfos;
}
}
------------------------------------------------------------------------------------------------------
OrdersProductInfo:package cn.com.postek.drp.model;import java.io.Serializable;
import java.math.BigDecimal;import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;@Entity
@Table(name="orders_productinfo")
public class OrdersProductInfo implements Serializable {

private static final long serialVersionUID = 4863100688813915712L;
private int id;
private String orderSerialNumber;//订单编号order_serial_number;20
private int productInfoId;//产品信息号product_info_id
// private int productSerialNumber;//产品编号product_serial_number;20
private String productModel;//产品型号product_model;100
private int amount;//数量amount
private BigDecimal subtotal;//小计subtotal; Decimal (10,2)
private String description;//备注description;500
private Orders orders;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", unique = true, nullable = false)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name = "order_serial_number", nullable = false,length=20,insertable=false,updatable=false)
public String getOrderSerialNumber() {
return orderSerialNumber;
}
public void setOrderSerialNumber(String orderSerialNumber) {
this.orderSerialNumber = orderSerialNumber;
}
// @Column(name = "product_serial_number", nullable = true,length=20)
// public int getProductSerialNumber() {
// return productSerialNumber;
// }
// public void setProductSerialNumber(int productSerialNumber) {
// this.productSerialNumber = productSerialNumber;
// }
@Column(name = "product_info_id", nullable = false)
public int getProductInfoId() {
return productInfoId;
}
public void setProductInfoId(int productInfoId) {
this.productInfoId = productInfoId;
} @Column(name = "product_model", nullable = false,length=100)
public String getProductModel() {
return productModel;
}
public void setProductModel(String productModel) {
this.productModel = productModel;
}
@Column(name = "amount", nullable = false)
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
@Column(name = "subtotal", nullable = false)
public BigDecimal getSubtotal() {
return subtotal;
}
public void setSubtotal(BigDecimal subtotal) {
this.subtotal = subtotal;
}
@Column(name = "description", nullable = true)
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
    @ManyToOne
    @JoinColumn(name="order_serial_number",referencedColumnName = "order_serial_number")
public Orders getOrders() {
return orders;
}
public void setOrders(Orders orders) {
this.orders = orders;
}}-------------------------------------------------测试---------------------------------
package cn.com.postek.drp.model;import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;import cn.com.postek.drp.common.util.VeDate;import com.rmis.common.dao.HibernateUtil;public class TestHibernate {
public static void main(String[] args) {
saveOrdes();
} public static void saveOrdes() { Session s = null;
Transaction tx = null;
try {
s = HibernateUtil.getSession();
tx = s.beginTransaction();
Orders order = new Orders();
order.setOrderSerialNumber("ODR0000001");
order.setStatus("1");
order.setAmount(BigDecimal.valueOf(1003.123));
order.setCreator("pasalo");
order.setCreationDate(VeDate.getNow());
order.setUpdateDate(VeDate.strToDateLong("2012-05-31 11:00:01"));
order.setDescription("hello");
s.save(order); OrdersProductInfo opInfo = new OrdersProductInfo();
Set<OrdersProductInfo> opiSet = new HashSet<OrdersProductInfo>();
// opInfo.setOrderSerialNumber("ODR0000001");
opInfo.setOrders(order);
opInfo.setAmount(50);
opInfo.setDescription("123");
opInfo.setProductModel("2");
opInfo.setSubtotal(BigDecimal.valueOf(30100.00));
opiSet.add(opInfo);
order.setOderInfos(opiSet);
s.save(opInfo);
tx.commit();
} catch (HibernateException e) {
if (tx != null)
tx.rollback();
throw e;
} finally {
if (s != null)
s.close();
} }
}
----------------------------------------------------------------------------------------------------------
结果是:每次执行增加数据saveOrdes()后;orders,orders_productinfo都只有1条数据,而且ID都是1,应该是表都被drop了,但是我的hbm2ddl.auto=validate,不知道什么情况,求解。

解决方案 »

  1.   

    <property name="hibernate.hbm2ddl.auto" value="none" /> 试试
      

  2.   

    create:表示启动的时候先drop,再create
    create-drop: 也表示创建,只不过再系统关闭前执行一下drop
    update: 这个操作启动的时候会去检查schema是否一致,如果不一致会做scheme更新
    validate: 启动时验证现有schema与你配置的hibernate是否一致,如果不一致就抛出异常,并不做更新
    只有这四个属性
      

  3.   

    <property name="hbm2ddl.auto">validate</property> 
    这条删了就ok了