Hibernate's Hello Word (written in domestic master)

xiaoxiao2021-03-06  42

Finally a bit of the feeling of introducing Hibernate. Conveniently, the learner outside the door gives a simple entry example.

If you have used other persistent architectures to hibernate is actually very simple. I don't talk about some principles.

Robbin is definitely better than me, go to the essence version yourself.

What I gave just I originally started to get in touch with hibernate and set up a way.

I haven't found it, so I will put it here now, (I only give an intuitive induction you want to get started, huh, huh)

First of all, you must create a new project.

Then in Project Properties-> Paths-> Required libraries-> add-> new define all the JAR packages under the Hibernate Lib in course, there is also hibernate2.jar.

Then you can go all the way.

Another is hibernate.properties

Find below from Hibernate's SRC

Put it to the SRC directory of your project

(What, your project does not have a src directory, newly built a casual class has a src directory)

Such a JB's Hibernate's development environment is good.

Then set your database connection in Hibernate.properties.

By default is HypersonicsQL

Um, the next thing is that what you want to do is actually very simple.

Newly built a class Message.java

code show as below

Code:

Package hello;

Import java.io.serializable;

/ **

* @Author getDown

* @version 1.0

* /

Public Class Message IMPLEments Serializable {

Private long id;

PRIVATE STRING TEXT;

/ / Define a simple chain list points to another message

Private message nextmessage;

Public message () {}

Public Message (long id) {

THIS.ID = ID;

}

Public Message (String text) {

THIS.TEXT = TEXT;

}

Public Message (long ID, string text) {

THIS.ID = ID;

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;

}

}

Next is the corresponding Hibernate configuration file for this class. MSSAGE.HBM.XML

Code:

"- // Hibernate / Hibernate mapping DTD 2.0 // en"

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

Name = "Hello.message"

Table = "Messages"

>

Column = "message_id"

>

Name = "text"

TYPE = "string">

Name = "text"

Length = "100"

NOT-NULL = "True"

/>

Name = "NextMessage"

Cascade = "all"

COLUMN = "next_message_id"

/>

Then the test class

Code:

Package hello;

Import Net.sf.hibiBernate.cfg.configuration;

Import Net.sf.hibActory;

Import net.sf.hibernate.tool.hbm2ddl.schemaExport;

Import net.sf.hibernate.Session;

Import net.sf.hibernate.query;

Import net.sf.hibernate.hibernate;

Import net.sf.hibernate.type.longtype;

Import net.sf.hibernate.transaction;

/ **

* @Author getDown

* @version 1.0

* /

Public class hello {

Public hello () {

}

Public static void main (string [] args) throws exception {

CONFIGURATION CFG = New Configuration (). Addclass (Message.Class);

/ ** As the name suggests build table. . . Run the following statement when running can generate a table in the database

* You can remove the following this sentence

* * /

// new schemaexport (cfg) .create (true, true);

// Mr. SemionFactory

SESSIONFACTORY sessions = cfg.buildsessionFactory ();

// get a session from SessionFactory

SESSION session = sessions.openses;

// Start the operation of the database

/ * ---- Creation of the database -------- * /

Message message = new message ("HelloWorld");

// Create a record

Session.save (Message);

// Deposit record

Session.flush ();

/ * --- Query operation for the database --------------- * /// Message Message = new message ();

// query q = session.createQuery ("from message as message where message.id = 1");

// Message = (Message) q.list (). get (0);

// Message.getNextMessage (). settext ("Hellonext");

// session.flush ();

// session.close ();

// long id = new long (1);

// Message Message = (Message) session.find ("from message as message where message.id =?", Id, hibernate.long) .get (0);

// system.out.println (message.getText ());

// / * ------- Transaction ---------------- /

// Transaction tx = session.begintransaction ();

// try {

// Message message = new message ("hello");

// session.save (Message);

// session.flush ();

// Message = new message ("hello");

// session.save (Message);

// session.flush ();

// tx.commit ();

//}

// catch (hibernateException ex) {

// tx.rollback ();

//}

/ * ------- Add 1000 record time -------------- * /

// Message Message;

// long start = system.currenttimemillis ();

// for (INT i = 0; i <1000; i ) {

// Message = new message ("hello");

// session.save (Message);

// session.flush ();

//}

// long end = system.currenttimemillis ();

// system.out.println ("Add 1000 Record Time ---" (end-start) / 1000 "S");

session.close ();

}

}

OK, run Hello, look out, what?

How to last for the lasting Hibernate of CMP is very lightweight. .

You can also try the performance of Hibernate to take a look at the last paragraph of Hello.java.

Of course, Hibernate is the most important or its principle, there are many very good, very interesting features and O / RM design ideas waiting for you to explore.

Look at its own document, you can learn a lot, its document is really very good.

Oh, I have just been in contact with Hibernate and all Hibernate learners.

MSN: java_xml@msn.com

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

New Post(0)