admin管理员组文章数量:1314381
In my spring boot application I have a notification service. I have 4 or 5 tables where there are user entries. When user requests to change some data in any of these tables, I trigger a notification and when admin approves this change request this needs to be updated in the corresponding table.
What I did was I added original data in one column, data to change in another column, entity class name in 3rd column. Entity primary key in fourth column, status of the request in 5th column.
create table tbl_change_requests
(
id uuid not null
primary key,
change_data jsonb,
decided_at timestamp(6),
original_data jsonb,
request_type varchar(255)
constraint tbl_change_requests_request_type_check
check ((request_type)::text = ANY
((ARRAY ['DATE_CHANGE'::character varying, 'PRIORITY_CHANGE'::character varying, 'ASSIGNEE_CHANGE'::character varying, 'STATUS_CHANGE'::character varying, 'DESCRIPTION_CHANGE'::character varying])::text[])),
requested_at timestamp(6),
status varchar(255)
constraint tbl_change_requests_status_check
check ((status)::text = ANY
((ARRAY ['PENDING'::character varying, 'APPROVED'::character varying, 'REJECTED'::character varying])::text[])),
task_id uuid,
requested_by uuid
constraint fkc0g49uvky2f4jwjv9i5ytdavv
references users,
target_name varchar(255)
);
Is there any way to write a generic repository to save any valid entity to the corresponding table? (I have the entity class name in DB and I can find the class and I can also find the table it represents)
public Class<?> getEntityClassFromEntityName(String entityName) {
for (EntityType<?> entity : entityManager.getMetamodel().getEntities()) {
if (entityName.equals(entity.getName())) {
return entity.getJavaType();
}
}
return null;
}
public <T> String getTableName(Class<T> entityClass) {
if (entityClass == null)
return null;
Metamodel meta = entityManager.getMetamodel();
EntityType<T> entityType = meta.entity(entityClass);
Table t = entityClass.getAnnotation(Table.class);
return (t == null)
? entityType.getName().toUpperCase()
: t.name();
}
```
本文标签: javaGeneric repository to save different entitiesStack Overflow
版权声明:本文标题:java - Generic repository to save different entities - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741900631a2403848.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论