Hibernate For Entity Beans of EJB
Hibernate also support Entity beans and Java Persistence Provider(JPA) of EJB3.0+ specification. In fact many of the concepts of Entity beans has been taken from Hibernate. JBoss application server uses Hibernate as the default persistence provider for Entity beans and JPA. JPA has the notion of persistence unit which represents the configuration.EntityManagerFactory of JPA corresponds to SessionFactory and EntityManager corresponds to Session. Let's do the Hibernate Introduction with Annotation example of persisting Student object using Entity beans and JPA API's
Download the Hibernate Core, Hibernate Annotations and Hibernate EntityManager from https://www.hibernate.org/6.html
From Hibernate Core (<hibernate-distribution-*>/lib/required)
- antlr-2.7.6
- commons-collections-3.1.jar
- dom4j-1.6.1.jar
- hibernate3.jar
- javassist-3.9.0.GA.jar
- jta-1.1.jar
- slf4j-api-1.5.8.jar
- <hibernate-distribution-*>/lib/ -Choose between cglib or javassist
From Hibernate Annotation:
- lib/ejb3-persistence.jar
- lib/hibernate-commons-annotations.jar
- hibernate-annotations.jar
From Hibernate Entity Manager:
- lib/jboss-archive-browsing.jar
- hibernate-entitymanager.jar
Download slf4j from http://www.slf4j.org/
- slf4j-simple-1.5.8.jar
Hypersonic:
- hsqldb.jar - We will be using hypersonic database for this example. you need to download hypersonic jar. For details see Hypersonic.
Now let's write our domain class which is Student class.Note that it the same class as we did in Hibernate Introduction with Annotation
Student
//Entity annotation represents that it is an entity class and is //mapped to a table in database. Table annotation tells about the //table name to which this entity is mapped @Entity @Table(name="Student") public class Student { //Id will correspond the primary key in the database private Long id; protected String name; //Id - Represents that it is a primary key column //GeneratedValue - How the key to be generated //column - Column to which this property is mapped @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name="STUDENT_ID") public Long getId() { return id; } public void setId(Long id) { this.id = id; } //There is annotation here so by default it is mapped to //the column with name NAME. In annotation, the properties are //by default mapped. In XML mapping by default the columns //are not mapped. public String getName() { return name; } public void setName(String name) { this.name = name; } }
Now let's write persistence.xml. This is the configuration file that EJB mandates. It is similar to what hibernate configuration file does. It can support multiple persistence provider other than hibernate also.
persistence.xml (Put in META-INF folder under source folder)
<?xml version="1.0" encoding="UTF-8"?> <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"><!-- This name is used to refer to this persistence context. This will be used while we make the EntityManagerFactory --> <persistence-unit name="hibernateMapping"> <!-- Persistence provider --> <provider>org.hibernate.ejb.HibernatePersistence</provider> <!-- Provide properites of persistence provider. This is specific to each persistence provider. All hibernate related properties which sit in hibernate.cfg.xml can be put here. --> <properties> <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/> <property name="hibernate.connection.url" value="jdbc:hsqldb:hsql://localhost"/> <property name="hibernate.connection.username" value="sa"/> <property name="hibernate.connection.password" value=""/> </properties> </persistence-unit> </persistence>
Now we just have to write our main class and start interacting with database.
public class HibernateMappingJPA { public static void main(String[] args) { //The name hibernateMapping comes from persistence.xml EntityManagerFactory emf = Persistence.createEntityManagerFactory("hibernateMapping"); //EntityManager is similar to Session in hibernate EntityManager em = emf.createEntityManager(); EntityTransaction tx = em.getTransaction(); tx.begin(); Student student = new Student(); student.setName("James Bond"); em.persist(student); //Query API Query query = em.createQuery("Select s from Student s where s.name like ?"); query.setParameter(1, "James%"); List<Student> studentList = query.getResultList(); for(Student studentList){ System.out.println(s.getName()); } tx.commit(); em.close(); } }
Note that we do not have to map the entity class explicitly as these are scanned automatically based on the @Entity annotation. The source code can be downloaded from the attachment section down below.
Back to Hibernate Index
Back To Java Home
Back To Home
| id | Name | desc | uploaded | Size | Downloads | ||
|---|---|---|---|---|---|---|---|
| 1 | 7 |
|
|
Hibernate Entity Beans tutorial | Mon 22 of June, 2009 22:44 IST by lalit | 2.58 Kb | 207 |
Sidebar
Last wiki comments
- Introduction to ORM: ugg boots
- Introduction to ORM: ugg boots
- AOP: Thanks
- Lalit Bhatt: Superb Collection
- Lalit Bhatt: J2EE training
- Introduction to ORM: timberland shoes
- Introduction to ORM: jordan shoes
- Introduction to ORM: nike air max
- Pune Tourist Spots: KONARK PARK CLOSED
- jQuery Form Validations: Jquery Developer
Sidebar
Random Pages
- What markets work on?
- Why projects fail?
- Bharat Band - Jai ho
- The concept of Nation
- Don't hide complexity if it cannot be handled in a robustway
Last blog post comments
-
Prospective MLA for Pune election - 2009: ugg boots
Wed 01 of Sep., 2010 12:58 IST
-
Bharat Band - Jai ho: How do we protest?
Wed 18 of Aug., 2010 13:13 IST
-
Divided by Destiny: Contact
Fri 23 of July, 2010 16:02 IST
-
Future of Java: thesis writing
Sat 17 of July, 2010 01:50 IST
-
Hang till Death Mr. Kasab: some change
Mon 28 of June, 2010 16:03 IST
-
God Religion : Why we are confused?: Re: Is GOD Necessary?
Tue 15 of June, 2010 17:29 IST
-
God Religion : Why we are confused?: Is GOD Necessary?
Tue 15 of June, 2010 13:06 IST
-
The reason in religion: good
Wed 10 of Mar., 2010 18:30 IST
-
The confusion of Design Patterns: I think at macro level you are right...
Tue 23 of Feb., 2010 03:31 IST
-
The Indian Municipality: Comment
Fri 22 of Jan., 2010 13:20 IST
Post new comment