BA Sports Network-integrated HIBERNATE4 (1) 1, Project diagram
2, first we introduce the corresponding jar package
it's used here. Oracle 11g , so we used a database connection Jar Package is Ojdbc5
3. We configure the corresponding entity objects in the database
Producttype.java
/** * Features: This is the product category * File: Producttype.java * Time: May 12, 2015 10:16:21 * cutter_point */package com.cutter_point.bean.product; publicclassproducttype{ private Integer typeid; Public Integer GetTypeId () { returntypeid; } Publicvoid Settypeid (Integertypeid) { This.typeid = typeid; }}
Product.hbm.xml
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://www.hibernate.org/dtd /hibernate-mapping-3.0.dtd ">
4. We configure Hibernate connection file<?xml version= ' 1.0 ' encoding= ' utf-8 '? ><! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "Http://www.hibern Ate.org/dtd/hibernate-configuration-3.0.dtd ">
5. Next we test if Hibernate is configured successfully/** * Features: This is the unit test for the product category * File: Producttest.java * Time: May 12, 2015 10:27:24 * cutter_point * * Package junit.test; Import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.cfg.configuration;import Org.junit.beforeclass;import org.junit.Test; Import Com.cutter_point.bean.product.ProductType; publicclassproducttest{@BeforeClass publicstaticvoid Setupbeforeclass () throws Exception {} @SuppressWa Rnings ("deprecation") @Test publicvoid Test () {producttypept = Newproducttype ();//new an object Pt.sett Ypeid (78); Set ID Number configurationcfg = newconfiguration (); Get the configuration SESSIONFACTORYSF = Cfg.configure ("/config/hibernate/hibernate.cfg.xml"). Buildsessionfactory (); Get Session Factory Sessionsession = Sf.opensession (); Session.begintransaction (); Session.save (PT); Session.gettransaction (). commit (); Session.close (); Sf.close (); } }
6. SummaryThis configuration hibernate is very simple, but note that if your database does not have a corresponding table, or if you do not write SQL statements, or you think you write the SQL statement is not allowed, then there is a method, the Hibernate configuration file Hibernate.cfg.xml inside.
<property name="Hbm2ddl.auto">update</ update in property> to Create, then Hibernate will automatically help you create the appropriate table
"Java EE Spring" 26, BA Sports Network-integrated HIBERNATE4 (1)