`
TRAMP_ZZY
  • 浏览: 133848 次
社区版块
存档分类
最新评论

Hibernate 4.3.5 入门实例

阅读更多

Hibernate4 比hibernate3 有很大的改变,学习官网文档入门实例如下:


1. hibernate.cfg.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 -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/docsearch?useUnicode=true&amp;characterEncoding=UTF-8</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="hibernate.connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="hibernate.current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="hibernate.cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="hibernate.show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hibernate.hbm2ddl.auto">update</property>

        <mapping resource="hibernate/hbm/Document.hbm.xml"/>

    </session-factory>

</hibernate-configuration>


2. Document.java

public class Document implements Serializable {

	private Integer docId;
	private String docName;
	private Byte docType;
	private Date addDatetime;
	private Date modifyDatetime;
	private String docLocation;
	private String uploadAuthor;
	private String anthor;
	
	public Document() {}

	public Integer getDocId() {
		return docId;
	}

	public void setDocId(Integer docId) {
		this.docId = docId;
	}

	public String getDocName() {
		return docName;
	}

	public void setDocName(String docName) {
		this.docName = docName;
	}

	public Byte getDocType() {
		return docType;
	}

	public void setDocType(Byte docType) {
		this.docType = docType;
	}

	public Date getAddDatetime() {
		return addDatetime;
	}

	public void setAddDatetime(Date addDatetime) {
		this.addDatetime = addDatetime;
	}

	public Date getModifyDatetime() {
		return modifyDatetime;
	}

	public void setModifyDatetime(Date modifyDatetime) {
		this.modifyDatetime = modifyDatetime;
	}

	public String getDocLocation() {
		return docLocation;
	}

	public void setDocLocation(String docLocation) {
		this.docLocation = docLocation;
	}

	public String getUploadAuthor() {
		return uploadAuthor;
	}

	public void setUploadAuthor(String uploadAuthor) {
		this.uploadAuthor = uploadAuthor;
	}

	public String getAnthor() {
		return anthor;
	}

	public void setAnthor(String anthor) {
		this.anthor = anthor;
	}

	@Override
	public String toString() {
		return "Document [docId=" + docId + ", docName=" + docName
				+ ", docType=" + docType + ", addDatetime=" + addDatetime
				+ ", modifyDatetime=" + modifyDatetime + ", docLocation="
				+ docLocation + ", uploadAuthor=" + uploadAuthor + ", anthor="
				+ anthor + "]";
	}
}


3. 实例

public class HibernateUtil {

	private static final SessionFactory sessionFactory = buildSessionFactory();
	
	private static SessionFactory buildSessionFactory() {
		SessionFactory sessionFactory = null;  
        try {  
            // Create the SessionFactory from hibernate.cfg.xml  
            Configuration configuration = new Configuration().configure("hibernate/hibernate.cfg.xml");  
            StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()  
                    .applySettings(configuration.getProperties());  
            StandardServiceRegistryImpl registry = (StandardServiceRegistryImpl) builder  
                    .build();  
            sessionFactory = configuration.buildSessionFactory(registry);   
        }  
        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);  
        }  
        return sessionFactory;  
	}
	
	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	public static void main(String[] args) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Document document = (Document) session.get(Document.class, new Integer(1));
		System.out.println(document);
		session.getTransaction().commit();
	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics