类型:转载 责任编辑:asp.net 日期:2007/05/23
热门软件下载:
体验(1)
hibernate-cfg.xml
<?xml version=1.0 encoding=UTF-8?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.pool.size">1</property>
<property name="statement_cache.size">25</property>
<property name="jdbc.fetch_size">50</property>
<property name="jdbc.batch_size">30</property>
<property name="show_sql">true</property>
<!-- Mapping files -->
<mapping resource="com.lontu.struts.cat.hbm.xml"/>
</session-factory>
</hibernate-configuration>
cat.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="net.sf.hibernate.examples.quickstart.Cat" table="CAT">
<!-- A 32 hex character is our surrogate key. Its automatically
generated by Hibernate with the UUID pattern. -->
<id name="id" type="string" unsaved-value="null" >
<column name="CAT_ID" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<!-- A cat has to have a name, but it shouldn be too long. -->
<property name="name">
<column name="NAME" sql-type="varchar(16)" not-null="true"/>
</property>
</class>
</hibernate-mapping>
test.jsp
Session ses = HibernateUtil.currentSession();
Transaction tx = ses.beginTransaction();
Cat pricess = new Cat();
pricess.setId("1002");
pricess.setName("Cat");
ses.save(pricess);
tx.commit();
HibernateUtil.closeSession();
网友回答:
你是用strtus和hibernate结合的。HibernateUtil类里的初始化方法有问题吧,HibernateUtil.java32行看看是什么代码贴出来。
hibernate.cfg.xml
Cat.hbm.xml
放的路径对吗?