Hibernate配置和简单例子
java 代码
- import java.util.Date;
- public class Event {
- private Long id;
- private String title;
- private Date date;
- public Event() {
- }
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public Date getDate() {
- return date;
- }
- public void setDate(Date date) {
- this.date = date;
- }
- }
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>