"Java EE Spring" 26, BA Sports Network-integrated HIBERNATE4 (1)

Source: Internet
Author: User

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. Summary

This 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.