Hibernate JPA
In this section, you will learn about JPA with Hibernate.
JPA
JPA is just an specification from Sun, which is released under JEE 5 specification. JPA standardized the ORM persistence technology for Java developers. JPA is not a product and can't be used as it is for persistence. It needs an ORM implementation to work and persist the Java Objects. ORM frameworks that can be used with JPA are Hibernate, Toplink, Open JPA etc.
These days most of the persistence vendors are releasing the JPA implementation of their persistence frameworks. So, developers can choose the best ORM implementation according to the application requirement. For example, production can be started from the free versions of ORM implementation and when the needs arise it can be switched to the commercial version of the ORM framework. You can switch the persistence provides without changing the code. So, ORM framework independence is another another big benefit of JPA.
Benefits of JPA
- Simplified Persistence technology
- ORM frameworks independence: Any ORM framework can be used
- Data can be saved in ORM way
- Supported by industry leaders
ORM Frameworks
Here are the list of ORM frameworks that can be used with JPA specification.
- Hibernate
- Toplink
- iBatis
- OpOpen JPA
JPA Simple Example
In the below example, you will learn the JPA Query API's method setParameter as a named parameter :
Database Table: student
CREATE TABLE `student` ( `id` int(11) NOT NULL auto_increment, `sname` varchar(100) NOT NULL, `sroll` int(11) NOT NULL, `scourse` varchar(10) NOT NULL, PRIMARY KEY (`id`) )
Model Java Class : Student.java
package roseindia; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.persistence.Table; @Entity @Table(name="student") public class Student { @Id @GeneratedValue private int id; /** * @return the id */ public int getId() { return id; } /** * @param id the id to set */ public void setId(int id) { this.id = id; } @Column(name="sname", length=100,nullable=false) private String sname; /** * @return the sname */ public String getSname() { return sname; } /** * @param sname the sname to set */ public void setSname(String sname) { this.sname = sname; } @Column(name="sroll",nullable=false) private int sroll; /** * @return the sroll */ public int getSroll() { return sroll; } /** * @param sroll the sroll to set */ public void setSroll(int sroll) { this.sroll = sroll; } @Column(name="scourse",length=10,nullable=false) private String scourse; /** * @return the scourse */ public String getScourse() { return scourse; } /** * @param scourse the scourse to set */ public void setScourse(String scourse) { this.scourse = scourse; } }
Main Class: JPASetParameterNamedParameter.java
package roseindia; import java.util.Iterator; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; import javax.persistence.Query; public class JPASetParameterNamedParameter { public static void main(String[] args) { // TODO Auto-generated method stub EntityManagerFactory emf=Persistence.createEntityManagerFactory("jpa"); EntityManager em=emf.createEntityManager(); try{ EntityTransaction entr=em.getTransaction(); entr.begin(); Query query=em.createQuery("SELECT st FROM Student st WHERE st.sroll > :roll"); query.setParameter("roll", 100); List results = query.getResultList(); if(results.size()!=0){ Iterator stIterator=results.iterator(); while(stIterator.hasNext()){ Student st=(Student)stIterator.next(); System.out.print("sname:"+st.getSname()); System.out.print(" sroll:"+st.getSroll()); System.out.print(" scourse:"+st.getScourse()); System.out.println(); } } else{ System.out.println("Record not found."); } entr.commit(); } finally{ em.close(); } } }
OUTPUT
log4j:WARN No appenders could be found for
logger (org.hibernate.cfg.annotations.Version). log4j:WARN Please initialize the log4j system properly. Hibernate: select student0_.id as id0_, student0_.scourse as scourse0_, student0_.sname as sname0_, student0_.sroll as sroll0_ from student student0_ where student0_.sroll>? sname:Arindam sroll:101 scourse:MCA sname:Ajay sroll:102 scourse:B.Tech. sname:Rakesh sroll:103 scourse:B.Tech sname:Vikas sroll:104 scourse:BCA |
JPA with Hibernate
For complete example of Hibernate JPA. Click Here.