0

Can we use JPA annoation to perist domain model (classes , relations and heritage) instead of hbm configuration, and then use Sessionfactory to make CRUD operations. I means that is it possible do use annotation without using persistence.xml and Entitymanager? I am asked this question because in the hibernate doc, thay always assiciate JPA annotation to persistence.xml

Bacteria
  • 8,406
  • 10
  • 50
  • 67
Yosr
  • 3
  • 1
  • 3
  • 2
    Yes, it's possible, and even recommended. I would go further and ditch the proprietary Session/SessionFactory API. Why don't you want to use the standard JPA API? – JB Nizet Sep 21 '15 at 06:23
  • Thank you for your answer.I have an old application and I would like to modify the hbm by the annotations. I don't like to change the cfg file and the DAO layer. – Yosr Sep 21 '15 at 11:07
  • Although it is not hibernate 5 and it is quite old, you can find some JPA annotated and SessionFactory used examples [here](https://github.com/bhdrkn/Hibernate-Examples). I hope it will be useful to you. – bhdrkn Oct 20 '15 at 08:39

1 Answers1

2

Yes it is possible to use annotation without using persistence.xml and entity manager.

You can achieve the same using traditonal approach by using :

  • SessionFactory
  • Transaction
  • Session

For details please visit the post : - http://techpost360.blogspot.se/2015/12/hibernate-5-maven-example.html

package com.hibernate.tutorial.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "employee")
public class Employee {

 @Id
 @Column(name = "id")
 Long id;

 @Column(name="employee_name")
 String employeeName;

 @Column(name="employee_address")
 String employeeAddress;

 public Employee(Long id, String employeeName, String employeeAddress) {
  this.id = id;
  this.employeeName = employeeName;
  this.employeeAddress = employeeAddress;
 }

 public Employee() {

 }

 public Long getId() {
  return id;
 }

 public void setId(Long id) {
  this.id = id;
 }

 public String getEmployeeName() {
  return employeeName;
 }

 public void setEmployeeName(String employeeName) {
  this.employeeName = employeeName;
 }

 public String getEmployeeAddress() {
  return employeeAddress;
 }

 public void setEmployeeAddress(String employeeAddress) {
  this.employeeAddress = employeeAddress;
 }

}

Main class for inserting the record into the Employee table

package com.hibernate.tutorial.mainclass;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.hibernate.tutorial.entity.Employee;

public class Hibernate5InsertTest {

 public static void main(String[] args) {
  SessionFactory sessionFactory;
  sessionFactory = new Configuration().configure().buildSessionFactory();

  Session session = sessionFactory.openSession();

  Transaction tx = session.beginTransaction();

  Employee emp = new Employee();
  emp.setId(new Long(1));
  emp.setEmployeeName("Rahul Wagh");
  emp.setEmployeeAddress("Indore, India");
  session.save(emp);
  tx.commit();
  session.close();
 }
}

I hope this example solves your problem

Rahul Wagh
  • 470
  • 1
  • 6
  • 23