spring - 休眠实体不同的实现
问题描述
我有一个具有多个实现的项目和一个实体类 Person。在每个实现中都有不同的数据库、不同的表和不同的列。在 DAO 层和业务层,代码是相同的。如何仅更改持久层以具有基于配置文件的 Person 实体类的不同实现并保持其余代码不变?
//I would like to change table and columns based on a profile
@Entity
@Table(name = "person")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Integer id;
private String first_name;
private String last_name
//getters,setters
}
//I would like to keep DAO unchanged no matter the profile
public interface PersonDao {
public List<Person> listAll() throws Exception;
}
public class PersonDaoImpl implements PersonDao{
@Override
public List<Person> listAll() throws Exception{
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Person> criteria = criteriaBuilder.createQuery(Person.class);
Root<Person> root = criteria.from(Person.class);
...the rest of the code
}
}
解决方案
有一个通用的抽象 Person 和 PersonDao 将由其他类(例如 MongoPerson、MysqlPersonDao、PersonV2...根据您的要求)扩展/实现。但是在您的服务层中只使用 Person 和 PersonDao。
推荐阅读
- android - Viewholder 可点击区域的边距底部未定义
- typescript - 如何将 json 文件转换为 Deno doc 网站
- regex - Shell - 重构连接数字的字符串正则表达式
- wordpress - 在 wordpress comment_reply_link() 中损坏评论的回复链接
- python - 错误:使用带有通配符的 re.findall 时出现虚假转义(行尾)
- javascript - Ember.js - 运行 ember --version 命令时出错
- python - 是否可以在 tkinter & python 中创建多个表单字段,然后以表格列格式将其提交到输出文本框
- jsp - JSP中注销按钮的会话无效
- javascript - JavaScript - 单击按钮时加载进度条后显示图像
- ios - SwiftUI 中的 animatableData 有什么作用?