java 从 POJO 获取带注释的休眠表名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1320864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:07:29  来源:igfitidea点击:

Get annotated hibernate tablename from POJO

javahibernateentitydao

提问by niklassaers

I have an entity which is declared roughly like:

我有一个实体,它的声明大致如下:

@Entity
@Table(name = "myUserTable")
public class User implements Serializable { ... }

I'm making a generic DAO class, and in doing so I'd like to retrieve the "myUserTable" name. Is there any way I can reach this name?

我正在创建一个通用的 DAO 类,这样做时我想检索“myUserTable”名称。有什么办法可以达到这个名字吗?

回答by skaffman

Easy enough using general reflection:

使用一般反射很容易:

import javax.persistence.Table;

.....

Class<?> c = User.class;
Table table = c.getAnnotation(Table.class);
String tableName = table.name();

回答by n002213f

Similar to Get the table name from the model in Hibernate

类似于在 Hibernate 中从模型中获取表名

Table table = Entity.class.getAnnotation(Table.class);
String tableName = table.name();