本人初学hibernate,就碰到问题,不知道为什么,向大家请教下
public static void main(String[] args) {
// TODO Auto-generated method stub
Configuration conf=new Configuration().configure();
SessionFactory sf=conf.buildSessionFactory();
Session sess=sf.openSession();
Transaction tx=sess.beginTransaction();
NewsTable n=new NewsTable();
n.setTitle("第一条消息");
n.setContent("测试");
sess.save(n);
tx.commit();
}
<hibernate-mapping>
<class name="dao.NewsTable" table="news_table">
<id name="id" type="java.lang.Integer">
<column name="id" ></column>
<generator class="native"></generator>
</id>
<property name="title" type="java.lang.String">
<column name="title" length="20" />
</property>
<property name="content" type="java.lang.String">
<column name="content" length="20" />
</property>
</class>
</hibernate-mapping>
上面的主函数,我运行一次,可以添加进数据库,当我再运行的时候,我的理解应该是在数据库中新增一条数据,可是我怎么运行,数据库始终只有1条数据,改了下 n.setTitle("第2条消息");
n.setContent("测试");
也只有第一条数据会变动,不会增加.
hibernate中的save应该是insert吧,如果写错了,大家帮忙看看,对学HIBERNATE有什么建议,
public static void main(String[] args) {
// TODO Auto-generated method stub
Configuration conf=new Configuration().configure();
SessionFactory sf=conf.buildSessionFactory();
Session sess=sf.openSession();
Transaction tx=sess.beginTransaction();
NewsTable n=new NewsTable();
n.setTitle("第一条消息");
n.setContent("测试");
sess.save(n);
tx.commit();
}
<hibernate-mapping>
<class name="dao.NewsTable" table="news_table">
<id name="id" type="java.lang.Integer">
<column name="id" ></column>
<generator class="native"></generator>
</id>
<property name="title" type="java.lang.String">
<column name="title" length="20" />
</property>
<property name="content" type="java.lang.String">
<column name="content" length="20" />
</property>
</class>
</hibernate-mapping>
上面的主函数,我运行一次,可以添加进数据库,当我再运行的时候,我的理解应该是在数据库中新增一条数据,可是我怎么运行,数据库始终只有1条数据,改了下 n.setTitle("第2条消息");
n.setContent("测试");
也只有第一条数据会变动,不会增加.
hibernate中的save应该是insert吧,如果写错了,大家帮忙看看,对学HIBERNATE有什么建议,
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货