Hibernate technology training

xiaoxiao2021-03-06  116

-------------------------------------------------- ----------------

/ **

* Copyright: Shutouxiang [E.ALPHA] all;

*

* EMAIL: Ealpha (AT) MSN (Dot) COM;

* MSN: Ealpha (AT) MSN (Dot) COM;

* QQ: 9690501

*

* All reprint, please indicate this information!

* /

-------------------------------------------------- ----------------

Use case

Train

Using a simple case illustrates the respective functions of the JDO, with examples include two forms, respectively: EMAP_USER, EMAP_POST. These two forms are written for user tables and forums, respectively.

The EMAP_USER form is fine, as follows:

Column name

Data type (precision range)

description

UserID

VARCHAR2 (38)

User ID. Primary key.

Loginname

VARCHAR2 (255)

Login name.

Password

VARCHAR2 (255)

password.

Nick

VARCHAR2 (255)

User's Nickname.

The EMAP_POST form is fine, as follows:

Column name

Data type (precision range)

description

POSTID

Number (38)

Post ID. Primary key.

Title

VARCHAR2 (255)

username.

Content

Varchar2 (4000)

User age.

Two categories corresponding to these two forms are: user, post. The two relationships are as follows:

2. Hibernate

2.1. Quick start

Product Home: http://www.hibernate.org/. The latest packages and development materials can be obtained from the connection.

Latest version: 2.1.2 (Recommended version)

Supported data sources include: HypersonicsQL, PostgreSQL, DB2, MySQL, Oracle, Sybase, MCKOI SQL, SAP DB, MS SQL Server, JSQL Driver, JTURBO Driver, WebLogic Driver, pure Java driver.

2.2. Use guide

1. Preparation: Download the Hibernate version 2.1.2. And configure CLASSPATH, which is an engineering file to access.

2. Copy the configuration file in Hibernate hibernate.properties to the application's default package. Modify this configuration file to select the appropriate data source configuration. In this report, select the MySQL database configuration.

3. Write the Java class, the class contains the getter and setter functions of each variable, and the auxiliary function Equals, etc.

4. Write the mapping profile of the Java class: class name .hbm.xml. The rules of the map are all defined.

5, profile introduction:

It is a root node.

The next node next node is

In the simplest case,

The node must have two properties of Name and Table, the former describes the classes belonging to the object, the latter description mapped data table:

...

Class>

The object's properties are divided into primary key classes and non-main keys. Primary key class properties

Name, and need to fill out

, The way to generate primary keys, there are four types: hi / low, uuid, identity, assigned. Non-primary key attribute

Wait for marking.

6, client program typical application:

Configuration cfg = new configuration (). Addclass (user.class) .addclass (post.class); sessionFactory sessions = cfg.buildsessionFactory ();

New SchemaExport (CFG) .create (True, TRUE);

SESSION session = sessions.openses;

User User = New User ("gigix");

User.SetPassword ("password");

Post pos = new post ();

Post.settitle ("Test");

Post.setContent ("Only for Test");

User.getPosts (). add (post);

Session.save (user);

Session.flush ();

session.close ();

Session = sessions.opensession ();

Query Q = session.createQuery ("from post as post where post.title = 'test'");

POST = (post) q.list (). get (0);

System.out.println (post .getowner (). Getpassword ());

Post.getowner (). setPassword ("xiongjie@9cbs.net);

Session.Update (post);

System.out.println (post .getowner (). Getpassword ());

Session.flush ();

session.close ();

7. The relationship between objects and objects includes: one-to-one, one-to-many, multi-one, more.

One-on-one situation:

Lazy = "true"

INVERSE = "false"

Cascade = "all"

Sort = "unsorted"

ORDER-by = "post_id">

Multi-to-one:

Name = "Owner"

Class = "com.mdao.test.hibernate.user"

Cascade = "none"

Outer-join = "auto"

Update = "true"

INSERT = "True"

COLUMN = "Owner_ID" />

2.3. Simple use case

Reference source code, slightly.

3. Develop resources

http://www.hibernate.org.cn:8000/

http://forum.javaeye.com/

http://www.jdocentral.com/

转载请注明原文地址:https://www.9cbs.com/read-126071.html

New Post(0)