关于hibernate的新增
package event;
import java.util.Date;
import org.hibernate.Session;
public class EventManager {
public static void main(String[] args) {
EventManager mgr = new EventManager();
mgr.createAndStoreEvent("345 ", new Date());
HibernateUtil.getSessionFactory().close();
}
private void createAndStoreEvent(String title, Date theDate) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Events theEvent = new Events();
theEvent.setTitle(title);
theEvent.setDate(theDate);
session.save(theEvent);
session.getTransaction().commit();
}
}
配置文件
<?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="event.Events" table="EVENTS">
<id name="id" column="EVENT_ID">
<generator class="sequence">
<param name="sequence">seq</param>
</generator>
</id>
<property name="date" type="timestamp" column="EVENT_DATE"/>
<property name="title" type="java.lang.String" column="EVENT_TITLE"/>
</class>
</hibernate-mapping>
我要的是新增,但是每次执行main方法都会把之前的数据覆盖了,只保留最后一次的,请问哪边除了问题。
------解决方案--------------------
看 , hibernate.cfg.xml 文件
里面有个属性叫 hbm2ddl.auto 把它的值设置成为 update , 你可能设置成为 create-drop 了