这是我的第一个Hibernate应用程序。被困在这里2天了。需要帮忙。包视图:小写字母:包大写字母:类
src-> dao-> OPERATIONS.JAVA src-> hbm.xml-> items.hbm.xml
SRC-> hibernate.cfg.xml中
public class Operations {
public static void main(String [] args){
Configuration cfg=new Configuration();
cfg.configure("Hibernate.cfg.xml");
SessionFactory sf=null;
sf=cfg.configure().buildSessionFactory();
Session sess=sf.openSession();
if(sess==null)
System.out.println("Session Object : NULL: "+sess);
Transaction tx=sess.beginTransaction();
ItemTable ob=new ItemTable();
ob.setItemCode(1);
ob.setItemName("ATTA");
sess.save(ob);
sess.flush();//generates SQL queries
tx.commit();
sess.close();
}
}
Hibernate.cfg.xml文件
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:Oracle:thin:@localhost:1521:xe</property>
<property name ="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.connection.username">system</property>
<property name="hibernate.connection.password">admin1234</property>
<property name="show_sql">true</property>
<property name="hibernate.connection.autocommit">false</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<!-- To display the sql generated in console -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
<!-- For JDBC transactions -->
<mapping resource="hbm/xml/Items.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Items.hfm.xml文件
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping auto-import ="true">
<class name="bean.ItemTable" table ="items">
<!-- Every class must have an Id(Primary Key) or Composite_Id -->
<id name ="itemCode" column="ITEMCODE" type="integer">
<generator class="assigned"></generator>
</id>
<property name="itemName" column="ITEMNAME" type="string"/>
</class>
</hibernate-mapping>