PYTHON  
 
  Oracle Forms & PL/SQ  
 
  ROR  
 
  Java  
 
  php  
 
  ask us  
 
  home  
 

 

Web Technologies






Hibernate in MyEclipse IDE

Hibernate and Eclipse IDE


1. Preface:

Eclipse is an IDE (Integrated Develepment Environment). It 
contains utilities by which we can develop projects. It comes 
from IBM and remains present in Websphere Studio Workbench (WSW), 
written in Java. The utilities of Eclipse are named plug-ins; 
whereas the main program (kernel) is called Runtime. The plug-ins 
are used to act on the files placed in the Workspace. For the 
related graphics interfaces, Eclipse uses SWT(Standard Widget Toolkit) 
developped in Java language.

Hibernate is a java package open source project that uses the mapping 
technique between an object and its relational database via HQL (Hibernate 
Query Language). Hibernate has its file object of type javaBean 
class. In this article, we will use Hibernate under the Eclipse IDE.

2. How to use Hibernate? here are the steps:

2.1 Download the Eclipse platform:

JBoss Eclipse IDE 2.x includes Hibernate Tools. It contains all we 
need to start working with Hibernate. Download and install JBoss Eclipse IDE from:
  labs.jboss.com
Choose for Windows Platform: JBossIDE-1.6.0.GA-Bundle-win32.zip md5 All Eclipse version 3.1.2

2.2 Unzip and install the JBossIDE-1.6.0 Eclipse:

The unziped package sets the directory eclipse which contains, 
among other things, eclipse.exe and plugings directory. This directory 
includes the plugings we need to run the Hibernate programs under Eclipse.
The subdirectory :\plugins\org.hibernate.eclipse_3.1.0.beta5\lib\hibernate 
contains the .jar libraries we need, but not all. We can find all 
of them In: 
 www.hibernate.org

2.3. Run Eclipse: Double-click on Eclipse.exe

Set the work place:(any directory), Example: C:\Eclipse\Hibernate\
Make here a directory \lib and bring all the possible .jar 
libraries in it:
- from : \plugins\org.hibernate.eclipse_3.1.0.beta5\lib\hibernate , and 
from : 
  www.hibernate.org
Eclipse will make .metadate directory in it the workplace which contains then: .metadata, lib, and the directories of the future projects.

2.4. The main functions to get starting:

In the IDE Eclipse platform: 
File -> New -> Project -> Java project
project -> properties -> add classes or .jar libraries (external 
libraires com from the made \lib directory).

3. Example 1: Regards.java

The steps:
- Lanch Eclipse JBoss
- File -> New -> Project -> Java project
- Browse towards the workplace 
- Project name Hibernate Regards (this is the root directory)
- Eclipse will place this \Hibernate Regards directory in the workplace:
we have: C:\Eclipse\Hibernate\Hibernate Regards. 
- In this directory, Eclipse set two files: .classpath and .project.
- In this directory, Eclipse will place
- hibernate.cfg.xml: the console configuration file

- In this directory, make the package directory: regards.
- In this directory regards, it will be placed:
- Thefile.java
- ItsFile.class
- Regards.hbm.xml: the mapping related file

- In Eclipse: 
Choose File -> New -> Other, 
click the newly available Hibernate category, and create a Java class :
Regards. Place in it:
package regards; public class regards { static String text = "All the best of luck."; public static void main(String[] args) { System.out.println("Regards and" + text); } }
Choose: run -> run as: Java application. The output in the console: Regards and All the best of luck. In the case we want to test the program under the prompt, we have to set classpath first, with hibernate3.jar file: We can use: CLASSPATH = C:\Eclipse\Hibernate\lib\hibernate3.jar We get then: C:\Eclipse\Hibernate\Hibernate Regards\regards> javac -Xlint regards.java C:\Eclipse\Hibernate\Hibernate Regards\regards> java regards Regards and All the best of luck.

4 Second Example: Theboxes.java

In fact to run regards.java programs, we do not need the mapping 
xml following files:
- hibernate.cfg.xml: the console configuration file
- Regards.hbm.xml: the mapping related file
But,for the following example, we do.

4.1. The first file to construct is of
type Javabean, the persistent class:

 TheBoxes.java

4.2. Its file configuration:

It contains:
 TheBoxesXmlConfiguration.xml
Hibernate needs a database driver to access a database. Choose: project -> properties ->Java Build Path -> Add External Jars and add your database driver (here mysqql).

4.3. Its mapping related file:

It contains: 
 TheBoxesXmlMapping.xml

4.4. Connect to MySQL database:

Create Boxes database and TheBoxes table as follows:
 Boxes MySQL database

4.5. First test: rise an instance: TheBox:

The related file is:
 TestTheBoxes.java
Run as an application will output in the Eclipse console:
Hibernate: select max(boxId) from theboxes TheBox object persisted to the database. 14:00:46,843 DEBUG SQL:393 - insert into theboxes (color, side, boxId) values (?, ?, ?) Hibernate: insert into theboxes (color, side, boxId) values (?, ?, ?)
With MySQL prompt
mysql> select * from theboxes; +-------+-------+------+ | boxId | color | side | +-------+-------+------+ | 1 | red | 2 | +-------+-------+------+ 1 row in set (0.00 sec) mysql>
The Hibernate program has inseted an object in the table theboxes where boxId= 1.

4.6. Fill out the table: TheBox:

Fill out the table with the Test2TheBoxes.java
java program by changing its properties: TheBox.setBoxId(integer); TheBox.setColor("color"); TheBox.setSide (double); Running Test2TheBoxes.java gives in the Eclipse console: Hibernate: select max(boxId) from theboxes TheBox object persisted to the database. 14:09:44,515 DEBUG SQL:393 - insert into theboxes (color, side, boxId) values (?, ?, ?) Hibernate: insert into theboxes (color, side, boxId) values (?, ?, ?) With MySQL prompt:
mysql> select * from theboxes; +-------+-------+------+ | boxId | color | side | +-------+-------+------+ | 1 | red | 2 | | 2 | green | 5 | +-------+-------+------+ 2 rows in set (0.00 sec) mysql>
Running once again Test2TheBoxes.java gives: Hibernate: select max(boxId) from theboxes TheBox object persisted to the database. 14:35:47,531 DEBUG SQL:393 - insert into theboxes (color, side, boxId) values (?, ?, ?) Hibernate: insert into theboxes (color, side, boxId) values (?, ?, ?)
mysql> select * from theboxes; +-------+--------+------+ | boxId | color | side | +-------+--------+------+ | 1 | red | 2 | | 2 | green | 5 | | 3 | purple | 7 | +-------+--------+------+ 3 rows in set (0.00 sec) mysql>
Once yet with the properties: TheBox.setBoxId(3); TheBox.setColor("purple"); TheBox.setSide (10.75); gives: Hibernate: update theboxes set color=?, side=? where boxId=? Update successfully!
mysql> select * from theboxes; +-------+--------+------+ | boxId | color | side | +-------+--------+------+ | 1 | red | 2 | | 2 | green | 5 | | 3 | purple | 7 | | 4 | purple | 11 | +-------+--------+------+ 4 rows in set (0.00 sec) mysql>

4.7. Update the table in the database

	
The related file is:
 TheBoxesUpdate.java
Hibernate program changes in theboxes table the object where boxId = 4 to:
mysql> select * from theboxes; +-------+--------+------+ | boxId | color | side | +-------+--------+------+ | 1 | red | 2 | | 2 | green | 5 | | 3 | purple | 7 | | 4 | yellow | 7 | +-------+--------+------+ 4 rows in set (0.00 sec) mysql>

4.8. Deleting an object:

	
The related file is:
 Test3TheBoxes.java
Running Test3TheBoxes.java will ouput:
21:36:16,015 DEBUG SQL:393 - delete from theboxes where boxId=3 Hibernate: delete from theboxes where boxId=2 0in the table TheBoxes is empty
Hibernate has deleted the object where boxId=3 from theboxes database:
mysql> select * from theboxes; +-------+--------+------+ | boxId | color | side | +-------+--------+------+ | 1 | red | 2 | | 2 | green | 5 | | 4 | yellow | 7 | +-------+--------+------+ 3 rows in set (0.00 sec)

5. Remark:

	
In the console, the output starts with: 
log4j:WARN No appenders could be found for logger 
(org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.

To prevent this, put in the root directory (\Hibernate boxes), 
the following file: log4j.properties. Choose wht to debug 
(a comment is #). Its content is:

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} 
%5p %c{1}:%L - %m%n
log4j.rootLogger=debug, stdout
log4j.logger.org.hibernate=info
log4j.logger.org.hibernate.SQL=debug
log4j.logger.org.hibernate.type=info
log4j.logger.org.hibernate.tool.hbm2ddl=info
log4j.logger.org.hibernate.cache=info

  
Google
Web
ScientificSentence
 



chimie labs
|
scientific sentence
|
java
|
php
|
green cat
|
contact
|


© Scientificsentence 2011. All rights reserved.