Hibernate3.0 instance

Source: Internet
Author: User

Hibernate3.0 instance
It is recommended that people who have a little experience or no experience with hibernate should be more appropriate (a little bit of foundation is not recommended for me to introduce some configuration files at least .). note that the JDBC driver COM. inet. TDS. tdsdriver

Let's study hard together.

1. Create a po object
Create Po objects customer, order, and their hibernate configuration files

/*
* Created on 2005-10-12
*
* Todo to change the template for this generated file go
* Window-preferences-Java-code style-code templates
*/
Package testdemo;

Import java. util. hashset;
Import java. util. Set;

/**
* @ Author liuzj
*
* Todo to change the template for this generated type comment go to window-
* Preferences-Java-code style-code templates
*/

Public Class Customer {
Private int ID;

Private string username;

Private string password;

Private set orders = new hashset ();

Public customer (){
}

Public customer (string username, string password, set orders ){
This. Username = username;
This. Password = password;

This. Orders = orders;

}

Public int GETID (){
Return ID;
}

Public String GetPassword (){
Return password;
}

Public String GetUserName (){
Return username;
}

Public set getorders (){
Return orders;
}

Public void setid (int id ){
This. ID = ID;
}

Public void setpassword (string password ){
This. Password = password;
}

Public void setusername (string username ){
This. Username = username;
}

Public void setorders (set orders ){
This. Orders = orders;
}

}

<? 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 = "testdemo. Customer" table = "customer" dynamic-insert = "true" dynamic-update = "true">
<ID name = "ID" column = "ID">
<Generator class = "increment"/>
</ID>
<Property name = "username" column = "username"/>
<Property name = "password" column = "password"/>

<Set
Name = "orders"
Inverse = "true"
Cascade = "Save-Update"
>
<Key column = "customer_id"/>
<One-to-define class = "testdemo. Order"/>
</Set>

</Class>
</Hibernate-mapping>

/*
* Created on 2005-10-13
*
* Todo to change the template for this generated file go
* Window-preferences-Java-code style-code templates
*/
Package testdemo;

/**
* @ Author liuzj
*
* Todo to change the template for this generated type comment go
* Window-preferences-Java-code style-code templates
*/

Import java. Io. serializable;
Public class order implements serializable {

Private long ID;
Private string ordernumber;
Private double price;
Private customer;

Public Order (){
}
Public Order (string ordernumber, double price, customer ){
This. ordernumber = ordernumber;
This. Price = price;
This. Customer = customer;
}
Public long GETID (){
Return this. ID;
}

Public void setid (long ID ){
This. ID = ID;
}

Public String getordernumber (){
Return this. ordernumber;
}

Public void setordernumber (string ordernumber ){
This. ordernumber = ordernumber;
}

Public customer getcustomer (){
Return this. customer;
}

Public void setcustomer (customer ){
This. Customer = customer;
}

Public double getprice (){
Return this. price;
}
Private void setprice (double price ){
This. Price = price;
}
}

<? 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 package = "testdemo">

<Class name = "order" table = "orders">

<ID name = "ID">
<Generator class = "increment"/>
</ID>

<Property name = "ordernumber" column = "order_number"/>
<Property name = "price"/>

<Role-to-one
Name = "customer"
Column = "customer_id"
Class = "customer"
Not-null = "true"
/>

</Class>
</Hibernate-mapping>

The above Po has been created. Below is a test class

/*
* Created on 2005-10-12
*
* Todo to change the template for this generated file go
* Window-preferences-Java-code style-code templates
*/
Package testdemo;

Import java. util. hashset;

Import org. hibernate. Session;
Import org. hibernate. sessionfactory;
Import org. hibernate. transaction;
Import org. hibernate. cfg. configuration;

Import testdemo. customer;

/**
* @ Author liuzj
*
* Todo to change the template for this generated type comment go to window-
* Preferences-Java-code style-code templates
*/

Public class test {

Sessionfactory = new configuration (). Configure (). addclass (
Testdemo. Customer. Class). addclass (order. Class). buildsessionfactory ();
Session session = sessionfactory. opensession ();
Public void savecustomer (customer) throws exception {

Transaction Tx = NULL;
Try {
Tx = session. begintransaction ();
Session. Save (customer );

TX. Commit ();

} Catch (exception e ){
If (TX! = NULL ){
TX. rollback ();
}
Throw E;
} Finally {
Session. Close ();
}
}


Public void testmethod () throws exception
{
Customer customer = new customer ("lzhengj", "001", new hashset ());
Order Order1 = new order ("order", 1000, customer );
Order order2 = new order ("order", 2000, customer );
Customer. getorders (). Add (Order1 );
Customer. getorders (). Add (order2 );
This. savecustomer (customer );

}

Public static void main (string [] ARGs ){

Try {
New Test (). testmethod ();

} Catch (exception E)
{
System. Out. println ("this is the testmethod throw exception .....");
E. printstacktrace ();
}

}
}

OK. The following is a hibernate configuration hibernate. cfg. XML (located under the application directory)

<! Doctype hibernate-configuration public
"-// Hibernate/hibernate configuration DTD 3.0 // en"
Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd>
<Hibernate-configuration>
<Session-factory>
<Property name = "hibernate. Connection. url">
JDBC: inetdae: localhost: 1433? Charset = GBK & database = hibernate_test
</Property>
<Property name = "hibernate. Connection. driver_class">
Com. inet. TDS. tdsdriver
</Property>
<Property name = "hibernate. Connection. username">
SA
</Property>
<Property name = "hibernate. Connection. Password">
AA
</Property>
<Property name = "hibernate. dialect">
Org. hibernate. dialect. sqlserverdialect
</Property>
<Property name = "show_ SQL">
True
</Property>
</Session-factory>
</Hibernate-configuration>

You also need to configure a log file log4j. properties (located under the application directory)

Log4j. rootlogger = debug, stdout
Log4j. appender. stdout = org. Apache. log4j. leleappender
Log4j. appender. stdout. layout = org. Apache. log4j. patternlayout
Log4j. appender. stdout. layout. conversionpattern = % c {1}-% m % N

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.