[英]ignore table prefixes while code generation in Hibernate perspective
I am using JBoss Hibernate Perspective to generate JPA Entities. 我正在使用JBoss Hibernate Perspective生成JPA实体。 But all my tables begin with tbl_ prefix.
但是我所有的表都以tbl_前缀开头。
I am migrating codes from PHP YII to Java and in YII model generator have an option to specify prefix and then the models that are generated don't have "tbl" prefix. 我正在将代码从PHP YII迁移到Java,并且在YII模型生成器中可以选择指定前缀,然后生成的模型没有“ tbl”前缀。
How can we achieve same with Eclipse and Hibernate perspective since as of now , all classes are being generated with Tbl Prefix. 由于到目前为止,所有类都是使用Tbl Prefix生成的,因此如何使用Eclipse和Hibernate透视图实现相同的目的。
Simplest way to do it is specify table attribute in your configuration: 最简单的方法是在配置中指定表属性:
<class name="User" table="tbl_user" />
If you are using annotation: 如果您使用注释:
@Table(name = "tbl_user")
public class User
By default we keep name of POJO same as table name. 默认情况下,我们使POJO的名称与表名称相同。 If you don't prefer naming scheme by hibernate, you can change default behavior by extending ImprovedNamingStrategy .
如果您不喜欢通过休眠命名方案,则可以通过扩展EnhancedNamingStrategy来更改默认行为。 As you can see ImprovedNamingStrategy implements NamingStrategy .
如您所见, 改良的 NamingStrategy实现了NamingStrategy 。
eg:- 例如:-
public class CustomImprovedNamingStrategy extends ImprovedNamingStrategy
{
@Override
public String columnName(String columnName)
{
return columnName;
}
@Override
public String tableName(String tableName)
{
return tableName.replace("tbl_","");
}
}
CustomImpovedNamingStrategy shall be set while creating SessionFactory:- 创建SessionFactory时应设置CustomImpovedNamingStrategy:
SessionFactory sessionFactory = new Configuration()
.setNamingStrategy(ImprovedNamingStrategy.INSTANCE)
.addFile("Item.hbm.xml")
.addFile("Bid.hbm.xml")
.buildSessionFactory();
refer: Implementing Naming Strategy . 请参阅: 实施命名策略 。
PS: In similar way DefaultNamingStrategy can be used to modify Hibernate's default naming strategy. PS:以类似方式,可以使用DefaultNamingStrategy修改Hibernate的默认命名策略。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.