hibernate基础实例:
Person.java
<script type="text/javascript">render_code();</script>
Person.hbm.xml
<script type="text/javascript">render_code();</script>
hibernate.properties
- package com.javamodel.hibernate;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Properties;
- import net.sf.hibernate.HibernateException;
- import net.sf.hibernate.MappingException;
- import net.sf.hibernate.Session;
- import net.sf.hibernate.SessionFactory;
- import net.sf.hibernate.Transaction;
- import net.sf.hibernate.cfg.Configuration;
- public class Example{
- private static SessionFactory _sessions = null;
- private static Properties pops = new Properties();
- static{
- try {
- InputStream stream = Example.class.getResourceAsStream("hibernate.properties");
- try {
- pops.load(stream);
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- Configuration cfg = new Configuration();
- cfg.addClass(Person.class);
- cfg.setProperties(pops);
- _sessions = cfg.buildSessionFactory();
- } catch (MappingException e) {
- e.printStackTrace();
- } catch (HibernateException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) throws HibernateException {
- Person person = new Person();
- person.setName("smallduzi");
- person.setEmail("smallduzi@sohu.com");
- Session session = _sessions.openSession();
- Transaction tx = null;
- try{
- tx = session.beginTransaction();
- session.save(person);
- tx.commit();
- }catch(HibernateException he){
- if(tx != null) tx.rollback();
- throw he;
- }
- finally{
- session.close();
- }
- }
- }
Person.java
代码
- package com.javamodel.hibernate;
- public class Person {
- private String id = null;
- private String name = null;
- private String email = null;
- public Person(){}
- /**
- * @return
- */
- public String getEmail() {
- return email;
- }
- /**
- * @return
- */
- public String getId() {
- return id;
- }
- /**
- * @return
- */
- public String getName() {
- return name;
- }
- /**
- * @param string
- */
- public void setEmail(String string) {
- email = string;
- }
- /**
- * @param string
- */
- public void setId(String string) {
- id = string;
- }
- /**
- * @param string
- */
- public void setName(String string) {
- name = string;
- }
- }
Person.hbm.xml
代码
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
- <hibernate-mapping>
- <class name="com.javamodel.hibernate.Person" table="person">
- <id name="id">
- <column name="id" length="40"/>
- <generator class="uuid.hex"/>
- </id>
- <property name="name" column="name" />
- <property name="email" column="email" />
- </class>
- </hibernate-mapping>
hibernate.properties
代码
- ## Oracle
- hibernate.dialect net.sf.hibernate.dialect.OracleDialect
- hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver
- hibernate.connection.username XXX
- hibernate.connection.password XXX
- #hibernate.connection.url jdbc:oracle:thin:@192.168.0.28:1521:orcl
- hibernate.connection.url jdbc:oracle:oci8:@XXX