时间:2021-07-01 10:21:17 帮助过:4人阅读
News.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"> <!-- Generated 2015-7-27 16:52:17 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.main.News" table="NEWS"> <id name="id" type="int"> <column name="ID" /> <generator class="assigned" /> </id> <property name="title" type="java.lang.String"> <column name="TITLE" /> </property> <property name="content" type="java.lang.String"> <column name="CONTENT" /> </property> </class> </hibernate-mapping>
一个简单的insert操作:
Configuration conf = new Configuration().configure(); SessionFactory sf = conf.buildSessionFactory(); Session sess = sf.openSession(); Transaction tx = sess.beginTransaction(); News n = new News(); n.setId(11); n.setTitle("Title2"); n.setContent("Content2"); sess.save(n); n.setContent("editedContent"); tx.commit();
Hibernate之通过hibernate.cfg.xml配置文件访问数据库的例子
标签: