Learn Hibernate In Action Reading Notes (1)

xiaoxiao2021-03-06  100

http://dev.9cbs.net/Article/33/33936.shtm

Let us still start with this long-lasting example "Hello World" :)

1. Message.java: a simple lasting class

Package hello;

Public class message {

Private long id; // Description

PRIVATE STRING TEXT;

Private message nextmessage;

Private message () {}

Public Message (String text) {

THIS.TEXT = TEXT;

}

Public long getId () {

Return ID;

}

Private void setid (long id) {

THIS.ID = ID;

}

Public string gettext () {

Return TEXT;

}

Public void setText (string text) {

THIS.TEXT = TEXT;

}

Public message getnextMessage () {

Return nextMessage;

}

Public void setnextMessage (Message nextMessage) {

This.nextMessage = NextMessage;

}

}

Description: Identifier, which allows the application to access the database with a unique identifier, which is the primary key value of the lasting class. If a Message object has two instances, they have the same identifier, then represent the same data they are accessing to the database. Here we have chosen the data type of LONG to do this, but this is not necessary. Because Hibernate allows this Identifier to be any data type.

2. Save data information to the database:

SESSION session = getsessionFactory (). OpenSession ();

Transaction tx = session.begintransaction ();

Message message = new message ("Hello World");

Session.save (Message);

TX.comMit ();

session.close ();

In this code, we reference Hibernate's session and transaction interfaces. The execution of this code is like we have made things doing the following SQL:

INSERT INTO Messages (Message_ID, Message_Text, Next_Message_ID)

VALUES (1, 'Hello World', NULL)

Note: In this example, we assume that there is already a Messages already have this table.

3. Read the information from the database and print them

SESSION NEWSession = GetSessionFactory (). OpenSession ();

Transaction newTransaction = newsession.begintransaction ();

List messages =

NEWSession.Find ("from message as m order by m.text ASC");

System.out.println (Messages.Size () "Message (s) Found:");

it (iTerator iter = messages.ITERATOR (); it.hasnext ();) {

Message message = (message) iter.next (); system.out.println (message.gettext ());

}

NEWTRANSACTION.COMmit ();

Newession.Close ();

In this code, everyone saw "from message as m order by m.text asc", which is the object-oriented query language Hibernate Query Language (HQL) coming with Hibernate. So, the following SQL is below. :

SELECT M.MESSAGE_ID, M.MESSAGE_TEXT, M.NEXT_MESSAGE_ID

From Messages M

Order by M.Message_Text ASC

Output results:

1 Message (s) Found:

Hello World

4. Simple XML mapping file

"- // hibernate / hibernate mapping dtd // en"

"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

Name = "Hello.message"

Table = "Messages">

Name = "id"

Column = "message_id">

Name = "text"

Column = "message_text" />

Name = "NextMessage"

Cascade = "all"

Column = "Next_Message_ID" />

This mapping file tells hibernate, the message is a persistent Messages that corresponds to the Identifier property corresponding to the Message_ID field in the table. The text property corresponds to the Message_Text field, and NextMessage is a multi-to-one association, the corresponding is Next_message_id field.

The above file can be manually prepared or generated by means of a third party tool.

5. Update a message

Let's modify the first information that just saved in the database.

SESSION session = getsessionFactory (). OpenSession ();

Transaction tx = session.begintransaction ();

// 1 is the generated id of the first message

Message message =

(Message) session.load (Message.class, New long (1));

Message.Settext ("Greetings Earthling");

Message nextMessage = New Message ("Take Me To Your Leader);

Message.setNextMessage (NextMessage);

TX.comMit ();

session.close ();

This code is equivalent to the following three SQL statements to achieve:

SELECT M.MESSAGE_ID, M.MESSAGE_TEXT, M.NEXT_MESSAGE_ID

From Messages M

WHERE M.MESSAGE_ID = 1

INSERT INTO Messages (Message_ID, Message_text, Next_Message_ID) Values ​​(2, 'Take Me To Your Leader (please)', NULL

Update Messages

SET Message_Text = 'Greetings Earthling', Next_Message_ID = 2

WHERE message_ID = 1

Run "Hello World" again, enter the information as follows:

2 Message (s) Found:

Greetings Earthling

Take Me To your Leader (please)

* Supplement, the core five interfaces in Hibernate:

Session interface: This is a major interface in Hibernate. The establishment and destruction of the session interface is lightweight and has little resource. This is very important, because in our application, we work all the SESSION CREATE and DISTROY. It is not a thread safe, in principle it should be designed to have only one session only. Session can load an object related to a workflow, which can monitor the change of this object, which is called a persistent object manager. When we want to edit the data object, it is to call it.

SessionFactory interface: We request a session instance, which is obtained from SessionFactory. Of course, SessionFactory is not a lightweight, it is shared by multiple applications. If you access multiple databases through Hibernate, you have to build a sessionFactory for each database. At Hibernate's runtime, SessionFactory caches SQL segments and other intermediary mapping data. It also holds the data that One Unit Of Work is reading and the data that may be used in the future.

Configuration interface: Configuration is used to specify an address of the mapping file used by an app and hibernate specified attribute file, and then create a sessionFactory.

Transaction interface:

Query and criteria interface:

(Fail)

Author Blog:

http://blog.9cbs.net/strawberry79/

related articles

Learn Hibernate In Action Reading Notes (1) Calling the process in the Oracle package in Java

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

New Post(0)