0

我们可以使用JPA annoation 来保留领域模型(类、关系和继承)而不是hbm 配置,然后使用Sessionfactory 进行CRUD 操作。我的意思是可以在不使用persistence.xml 和Entitymanager 的情况下使用注释吗?我被问到这个问题是因为在休眠文档中,总是将 JPA 注释关联到 persistence.xml

4

1 回答 1

2

是的,可以在不使用 persistence.xml 和实体管理器的情况下使用注释。

您可以通过使用传统方法来实现相同的目的:

  • 会话工厂
  • 交易
  • 会议

有关详细信息,请访问帖子:- 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;
 }

}

用于将记录插入 Employee 表的主类

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();
 }
}

我希望这个例子能解决你的问题

于 2016-01-19T19:57:47.233 回答