I am using JBoss Hibernate Perspective to generate JPA Entities. But all my tables begin with tbl_ prefix.
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.
How can we achieve same with Eclipse and Hibernate perspective since as of now , all classes are being generated with Tbl Prefix.
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. If you don't prefer naming scheme by hibernate, you can change default behavior by extending ImprovedNamingStrategy. As you can see ImprovedNamingStrategy implements 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 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.