jpa - Hibernate 5 with annotation -
can use jpa annoation perist domain model (classes , relations , heritage) instead of hbm configuration, , use sessionfactory make crud operations. means possible use annotation without using persistence.xml , entitymanager? asked question because in hibernate doc, thay assiciate jpa annotation persistence.xml
yes possible use annotation without using persistence.xml , entity manager.
you can achieve same using traditonal approach using :
- sessionfactory
- transaction
- session
for details please visit 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 inserting record 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 example solves problem
Comments
Post a Comment