Hibernate actual combat (1)

zhaozj2021-02-16  37

Hibernate

Bromon original, please respect the copyright

Object Relative Mapping Abbreviation ORM is a hotspot for object-oriented development, which is used to resolve the complication and inconvenience of manual OR mapping in JDBC development. The entity bean in EJB is very famous in this area - because of its advanced and famous, because of its inefficiency. People with experience in entity bean may have a headache for the low efficiency caused by the remote interface. In a lot of unclear projects, whether the entity bean has to be lost, the debate is very large. A lightweight persistence program may be able to solve some problems, Hibernate should be born.

Hibernate is an intermediate layer that is the purpose of making the relationship in the database into objects through certain rule mappings, allowing Java developers without much consideration of the problem of the underlying database, only need to manage objects as usual. It is considerable to the relational database will continue to occupy the market. In the field of data persistence, even a lightweight solution will be complicated, perhaps, as Jay Chou's music is not known. Before learning it, it is best to recall the problems and inconveniences encountered in the database development. Think about why you need a persistent layer to know what a lot of operations is, and why do you want to do it, on this issue? I don't want to do more narratives, because "long ..." This sentence usually long (sorry, can't play), will cause great wear of my keyboard and enthusiasm. If I let me write a book, then I will be happy to describe what is data persistence, what is the benefit of it. Tony said nonsense.

First, you need to configure the environment, download Hibernate 2.1 (www.hibernate.org), add * .jar under libasspath, your database JDBC driver should also be in ClassPath. Open Hibernate.properties, configure the appropriate information for the database you use, such as MS SQL Server, configured as follows:

## MS SQL Server

hibernate.dialect net.sf.hibernate.dialect.SQLServerDialect hibernate.connection.driver_class com.microsoft.jdbc.sqlserver.SQLServerDriver hibernate.connection.url jdbc: microsoft: sqlserver: // localhost: 1433; DatabaseName = zizz hibernate.connection. Username sa hibernate.connection.password

Among them, it is already written, just need to take the annotation, I just modified the database name, account number, password. Create a database ready for ZIZZ.

Then copy this file to the root of your application.

We talk about many mappings, you should first take a look at how this mapping is done. Assume a simplest application, write a single function of the message board, the design of the data has a message number, the name, message content, and message time. It's simple enough, is it going to do? I guess you want to build a database table first, and the name may be called Guestbook. NO, this is not an object-oriented way, you may wish to consider from the perspective of the object. We certainly hope that each message is presented in the way, each object should have the properties: ID, author, content, time. Stealing a lazy, no painting UML. Below this class should be very easy to understand:

//Guestbook.java package org.bromon.zizz; import java.util. *;

Public Class Guestbook {Private INT ID; Private String Author; Private Calendar Time

Private void setid (int ID) {this.id = ID;} public int getId () {return (ID);

Public void setauthor (string author) {this.author = author;} public string getau () {return (author);

Public void setContent (string content) {this.content = content;}}; {return (content);

Public void settime (calendar time) {this.time = time;} public calendar gettime () {return (time);}}

Basically the simplest bean, if you feel difficult, please go back to Mars first.

It should be noted that the setID method is specified as private because I want to use this field to do the primary key, it is best to generate automatically by the system, so it should not be specified by the user, this method is designed for Hibernate, so it is private.

How to map this class with the database? Take a look at Hibernate's magic, use an XML file to describe, it should be named guestbook.hbm.xml:

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

Although it is a bit unfamiliar, it is easy to read, carefully ponder.

Below to write our app, its function is to insert data:

//Oprate.java package org.bromon.zizz; import net.sf.hibernate. *; Import.sf.hibaRnate.cfg. *; Import net.sf.hibernate.tool.hbm2ddl. *; Import java.util. *;

public class Operate {public static void main (String args []) {try {Configuration cfg = new Configuration () addClass (GuestBook.class);. SessionFactory sessions = cfg.buildSessionFactory (); new SchemaExport (cfg) .create (true , TRUE); session session = sessions.opension (); guestbook gb = new guestbook (); gb.setauthor ("bromon"); gb.setcontent ("message content"); gb.settime (Calendar.GetInstance () Transaction ts = session.begintransaction (); session.save (GB); ts.commit (); session.close ();} catch (exception e) {system.out.println (e);}}} Let's take a look at Javac -d. * .Java: java org.bromon.zizz.Operate

Take a look at the database, the table has been established, and the data has also been saved. If

New SchemaExport (). Create (True, True); Note, then the system does not create a table, but only add new records in existing forms, of course, if the form does not exist, an exception is produced.

You have seen 5% of Hibernate Magic Magic, it is enough complex, you can let you cope with complex applications, but today I still here.

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

New Post(0)