ITeye博客

Hibernate配置和简单例子

Jameslyy · 2007-09-28 16:50 · Database ORM · HSQLDB · JDBC · XML · MySQL · Hibernate · Markdown

java 代码

  1. import java.util.Date;
  2. public class Event {
  3. private Long id;
  4. private String title;
  5. private Date date;
  6. public Event() {
  7. }
  8. public Long getId() {
  9. return id;
  10. }
  11. public void setId(Long id) {
  12. this.id = id;
  13. }
  14. public String getTitle() {
  15. return title;
  16. }
  17. public void setTitle(String title) {
  18. this.title = title;
  19. }
  20. public Date getDate() {
  21. return date;
  22. }
  23. public void setDate(Date date) {
  24. this.date = date;
  25. }
  26. }

Event.hbm.xml

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">              <hibernate-mapping>           <class name="com.james.business.modle.domain.Event" table="EVENTS">               <id name="id" column="id">                   <generator class="native" />               </id>               <property name="date" type="timestamp" column="datet" />               <property name="title" />           </class>       </hibernate-mapping>   

 

java 代码

import org.hibernate.SessionFactory;    import org.hibernate.cfg.Configuration;       public class HibernateUtil {        private static final SessionFactory sessionFactory;           static {            try {                // Create the SessionFactory from hibernate.cfg.xml                sessionFactory = new Configuration().configure()                        .buildSessionFactory();            } catch (Throwable ex) {                // Make sure you log the exception, as it might be swallowed                System.err.println("Initial SessionFactory creation failed." + ex);                throw new ExceptionInInitializerError(ex);            }        }           public static SessionFactory getSessionFactory() {            return sessionFactory;        }       }  

java 代码

import java.util.Date;       import org.hibernate.Session;       import com.james.business.modle.domain.Event;       public class EventManager {        public static void main(String[] args) {            EventManager mgr = new EventManager();               mgr.createAndStoreEvent("My Event", new Date());               HibernateUtil.getSessionFactory().close();        }           private void createAndStoreEvent(String title, Date theDate) {               Session session = HibernateUtil.getSessionFactory().getCurrentSession();               session.beginTransaction();               Event theEvent = new Event();            theEvent.setTitle(title);            theEvent.setDate(theDate);               session.save(theEvent);               session.getTransaction().commit();        }       }  

hibernate.cfg.xm

xml 代码

<?xml version='1.0' encoding='utf-8'?>       <!DOCTYPE hibernate-configuration PUBLIC                "-//Hibernate/Hibernate Configuration DTD 3.0//EN"                "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">              <hibernate-configuration>                  <session-factory>                      <!-- Database connection settings -->               <!-- org.hsqldb.jdbcDriver -->               <property name="connection.driver_class">                   com.mysql.jdbc.Driver                </property>               <!-- jdbc:hsqldb:hsql://localhost -->               <!-- ?useUnicode=true&characterEncoding=utf-8&autoReconnect=true -->               <property name="connection.url">                   jdbc:mysql://10.1.1.2:3306/test                </property>               <property name="connection.username">test</property>               <property name="connection.password">test</property>                      <!-- JDBC connection pool (use the built-in) -->               <property name="connection.pool_size">1</property>                      <!-- SQL dialect -->               <!-- org.hibernate.dialect.HSQLDialect -->               <property name="dialect">                   org.hibernate.dialect.MySQL5InnoDBDialect                </property>                      <!-- Enable Hibernate's automatic session context management -->               <property name="current_session_context_class">thread</property>                      <!-- Disable the second-level cache  -->               <property name="cache.provider_class">                   org.hibernate.cache.NoCacheProvider                </property>                      <!-- Echo all executed SQL to stdout -->               <property name="show_sql">true</property>                      <!-- Drop and re-create the database schema on startup -->               <property name="hbm2ddl.auto">create</property>                      <mapping resource="com/james/business/modle/domain/Event.hbm.xml" />                  </session-factory>              </hibernate-configuration>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。