我在我的项目中使用hibernate-JPA实现(v5.6.1.Final)。
我按以下方式实现了数据访问层:

1.1 Employee.java实体
package com.elephasvacation.tms.web.entity;
import com.elephasvacation.tms.web.entity.enumeration.GenderTypes;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
@Entity
@Table(name = "employee")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Employee implements SuperEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Integer id;
@Column(name = "name", nullable = false, length = 200)
private String name;
@Lob
@Column(name = "address")
private String address;
@Column(name = "date_of_birth")
private LocalDate dateOfBirth;
@Column(name = "nic", length = 45)
private String nic;
@Column(name = "contact", length = 45)
private String contact;
@Column(name = "email", length = 200)
private String email;
@Column(name = "gender", length = 20)
private GenderTypes gender;
@Column(name = "position", length = 45)
private String position;
@Column(name = "status", length = 45)
private String status;
@Column(name = "created")
private LocalDateTime created;
@Column(name = "updated")
private LocalDateTime updated;
/* Constructor with ID attribute. */
public Employee(Integer id,
String name,
String address,
LocalDate dateOfBirth,
String nic,
String contact,
String email,
GenderTypes gender,
String position,
String status) {
this.id = id;
this.name = name;
this.address = address;
this.dateOfBirth = dateOfBirth;
this.nic = nic;
this.contact = contact;
this.email = email;
this.gender = gender;
this.position = position;
this.status = status;
}
/* Constructor without ID attribute. */
public Employee(String name,
String address,
LocalDate dateOfBirth,
String nic,
String contact,
String email,
GenderTypes gender,
String position,
String status) {
this.name = name;
this.address = address;
this.dateOfBirth = dateOfBirth;
this.nic = nic;
this.contact = contact;
this.email = email;
this.gender = gender;
this.position = position;
this.status = status;
}
@PrePersist
public void creationTimeStamps() {
created = LocalDateTime.now();
}
@PreUpdate
public void updateTimeStamps() {
updated = LocalDateTime.now();
}
}当对象被成功地持久化时,我想返回生成的ID。因此,我按照以下方式实现了EmployeeDAOImpl.java:
1.2 EmployeeDAOImpl.java
package com.elephasvacation.tms.web.dal.custom.impl;
import com.elephasvacation.tms.web.dal.custom.EmployeeDAO;
import com.elephasvacation.tms.web.entity.Employee;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;
public class EmployeeDAOImpl implements EmployeeDAO {
private EntityManager entityManager;
@Override
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
@Override
public Integer save(Employee employee) throws Exception {
this.entityManager.persist(employee);
// call the flush method on EntityManager manually, because we need to get the Generated ID
this.entityManager.flush();
return employee.getId(); // here, generated ID will be returned.
}
@Override
public void update(Employee employee) throws Exception {
this.entityManager.merge(employee);
}
@Override
public void delete(Integer key) throws Exception {
this.entityManager.remove(this.entityManager.find(Employee.class, key));
}
@Override
public Employee get(Integer key) throws Exception {
return this.entityManager.find(Employee.class, key);
}
@Override
public List<Employee> getAll() throws Exception {
Query allEmployeesQuery = this.entityManager.createQuery("SELECT e FROM Employee e");
return (List<Employee>) allEmployeesQuery.getResultList();
}
}我正在按以下方式重构代码:
使用CrudDAOImpl.java
创建可视化图的方法

2.1 CrudDAOImpl.java
package com.elephasvacation.tms.web.dal;
import com.elephasvacation.tms.web.entity.SuperEntity;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;
public class CrudDAOImpl<T extends SuperEntity, K extends Serializable> implements CrudDAO<T, K> {
private EntityManager entityManager;
private Class<T> entityClass;
public CrudDAOImpl() {
this.entityClass =
(Class<T>) (((ParameterizedType) (this.getClass().getGenericSuperclass())).getActualTypeArguments()[0]);
}
/** This method is used to pass the EntityManager to the lower level classes that extend the CrudDAOImpl class.
* */
protected EntityManager getEntityManager(){
return this.entityManager;
}
@Override
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
@Override
public Integer save(T entity) throws Exception {
/* If native hibernate is used in my project. I can do something like this;
this will return the generated ID:
* this.session.save(entity);
*
* Since I go with hibernate-JPA implementation I want to do the same thing in this method. */
this.entityManager.persist(entity);
this.entityManager.flush();
return null; // I want to return the generated ID here.
}
@Override
public void update(T entity) throws Exception {
this.entityManager.merge(entity);
}
@Override
public void delete(K key) throws Exception {
this.entityManager.remove(key);
}
@Override
public T get(K key) throws Exception {
return this.entityManager.find(this.entityClass, key);
}
@Override
public List<T> getAll() throws Exception {
TypedQuery<T> query =
this.entityManager.createQuery("SELECT " + (this.entityClass.getName()), this.entityClass);
return query.getResultList();
}
}如果您能向我建议一种在数据库中保存对象时返回生成的ID的方法,我将不胜感激。提前谢谢。
发布于 2021-12-12 09:46:40
如果你感兴趣的话,这就是我所做的。正如@HasnainAliBohra建议的那样,我使用了SuperEntity.java。
SuperEntity.java.中添加getId()方法的
package com.elephasvacation.tms.web.entity;
import java.io.Serializable;
public interface SuperEntity extends Serializable {
<T extends Serializable> T getId();
}大多数情况下,生成的ID将是一个Serializable.
Employee.java中id属性的getter。请注意,我使用过Project Lombok。因此,@Data注释为id属性生成getter .无论如何,getter方法如下所示:
// Employee.java class
public Integer getId() {
return id;
}CrudDAOImpl.java中更改save()方法如下: @Override
public Integer save(T entity) throws Exception {
this.entityManager.persist(entity);
this.entityManager.flush();
return entity.getId(); // Let's return the generated ID here.
}测试我所做的(用JUnit4). )的
package com.elephasvacation.tms.web.dal.custom.impl;
import com.elephasvacation.tms.web.dal.DAOFactory;
import com.elephasvacation.tms.web.dal.DAOTypes;
import com.elephasvacation.tms.web.dal.custom.EmployeeDAO;
import com.elephasvacation.tms.web.entity.Employee;
import com.elephasvacation.tms.web.entity.enumeration.GenderTypes;
import com.elephasvacation.tms.web.util.HibernateUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import java.time.LocalDate;
import static org.junit.Assert.assertNotNull;
public class EmployeeDAOImplTest {
private final EmployeeDAO employeeDAO = DAOFactory.getInstance().getDAO(DAOTypes.EMPLOYEE);
private EntityManagerFactory emf;
private EntityManager em;
@Before
public void setUp() {
try {
/* get EntityManagerFactory. */
this.emf = HibernateUtil.getEntityManagerFactory();
/* creates EntityManager. */
this.em = emf.createEntityManager();
} catch (Exception e) {
e.printStackTrace();
}
}
@After
public void tearDown() {
/* close the EntityManagerFactory and EntityManager. */
if (em != null) {
em.close();
emf.close();
}
}
@Test
public void save() {
try {
/* begins the transaction. */
this.em.getTransaction().begin();
/* set EntityManager. */
this.employeeDAO.setEntityManager(this.em);
/* creates a new Employee object. */
Employee john = new Employee("John Doe",
"New York",
LocalDate.of(1991, 01, 01),
"112233445566",
"03321234567",
"john.test@gmail.com",
GenderTypes.MALE,
"Trainee",
"Active");
/* saving the Employee. */
Integer generatedEmployeeId = this.employeeDAO.save(john);
/* assert */
assertNotNull(generatedEmployeeId);
/* print the generated ID on the terminal. */
System.out.println("Generated Employee ID: " + generatedEmployeeId);
/* committing the transaction. */
this.em.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
}
}
}employee表.中。
5.1终端输出

5.2数据库记录截图。

希望你能找到有用的东西,谢谢。
https://stackoverflow.com/questions/70321110
复制相似问题