Logo en.artbmxmagazine.com

Hibernate my eclypse software to connect to a database with xml

Anonim

The Intention of this Article within this context, is due to the fact that although my professional activity is not 100% linked to databases, it is important to know how they interact, in order to offer the end user an application of great potential and that it can be useful, when required. That is why Hibérnate initially aroused my curiosity to know what it was about and later as I entered, I found it very interesting more and more, that is why I decided to write this little note. Although practically Hibérnate is very broad so we will only cover the most basic aspects

It turns out that Hibérnate is an Object-Relational Mapping tool for the Java platform (and also available for.Net under the name NHibernate) that facilitates the mapping of attributes between a traditional relational database and the object model of an application, using declarative files (XML) that allow these relationships to be established. Besides being a working environment that aims to facilitate the persistence of Java objects in relational databases and at the same time the query of these databases to obtain objects.

In this, the concept has been used, Persistent Classes which are equivalent to a table in the database, and a record or line is a persistent object of this class. In addition to the word mapping, which is nothing other than to facilitate in a healthy, orderly and normalized way between the different classes.

Before continuing it is necessary to know what kind of problem hybridize seeks to solve and mainly refers to the problem of the difference between the two models used today to organize and manipulate data: The one used in the computer's memory (objects) and the one used in databases (relational model). To achieve this it allows the developer to detail what their data model is like, what relationships exist and what form they have. With this information, Hibérnate allows the application to manipulate the database data by operating on objects, with all the characteristics of OOP. Object-oriented programming expresses a program as a set of these objects, which collaborate with each other to perform tasks. This makes programs and modules easier to write, maintain, and reuse.As we already know, OOP (is a programming paradigm that uses objects and their interactions to design applications and computer programs are based on various techniques, including inheritance, modularity, polymorphism, and encapsulation. Its use became popular in the early 1990s. 1990. Currently there are many programming languages ​​that support object-oriented programming). For the explanations of the following example, as I already mentioned in the beginning, we will handle the creation ofCurrently there are many programming languages ​​that support object-oriented programming). For the explanations of the following example, as I already mentioned in the beginning, we will handle the creation ofCurrently there are many programming languages ​​that support object-oriented programming). For the explanations of the following example, as I already mentioned in the beginning, we will handle the creation of

Now we will explain what process has to be followed to complete with hibérnate, the entire construction of a simple application:

  • Create the name of the project, by pressing CTRL + N (As shown in the figure).

  • Select a profile for the connection to the database, We call the library Web profile. By specifying the username and password (As shown in the following figure).

  • In the following figure it is necessary to make sure that the COPY JDBC DRIVER box is checked.

  • Now we will have to create a descriptive name for the session factory (Hybrid expect that there is a single instance of the hybrid session class for each connection.Reduction of hybrid libraries, by default this version includes a heavy load of libraries, therefore some of them will only be necessary for local development, and some others for special cache implementations. Database creation using the following code.

CREATE TABLE customer

(id serial NOT NULL, name text, lastname text, age int4, CONSTRAINT customer_pk PRIMARY KEY (id));

CREATE TABLE book

(id serial NOT NULL, title text, author text, customer_fk int4, available bool, CONSTRAINT book_pk PRIMARY KEY (id));

ALTER TABLE book

ADD CONSTRAINT book_customer FOREIGN KEY (customer_fk) REFERENCES customer

(id) ON UPDATE RESTRICT ON DELETE RESTRICT;

  • Generate the mapping files as well as the classes. As can be seen in the following image.

  • Open the profile of the connection, which is called libray web, already specified previously.

  • Select the tables created (Book - Users), using the right button and choose “Create Hybrid Mapping).

  • Select as destination the project name (persistent Lybrary),

With these steps you will have created the persistence layers. In addition to that 2 new entries will have been created in our exploration package

(GET HERE.CFG.XML)

BOOK.HBM.XML

MyEclipse created two files per class. The first is an abstract class. (AbstractBook) will be overwritten every time you repeat the import procedure.

In the second class (book) you can adapt any changes you want to make. It is generated only once.

Finally, so that the previous example is even clearer, it will be necessary for every user who starts his first examples as his server, it will be necessary to download the software from the following link, in order that many examples can be generated, it is not a complicated it only requires more time, dedication as well as reading books, manuals as well as the use of the Internet.

References:

1.-

2.-

3.-

4.-

5.-

7.-

Hibernate my eclypse software to connect to a database with xml