我正在使用 hibernate 的 ORM 和 hibernate-generator 以注释的方式生成 Entity
。我需要经常切换数据库(开发/发布)。所以,我每次都必须更改实体的注释。我想知道是否有办法配置它。
@Entity
@Table(name = "my", catalog = "dev_db")
public class MyEntity {
}
如您所见,我每次都必须更改目录。如何根据一个jdbc.properties
来配置?
最佳答案
您可以使用拦截器来修改 hibernate 生成的 SQL。
public String onPrepareStatement(String sql) {
String superSQL = super.onPrepareStatement(newSQLWithNamespace);
//replace all catalog occurencies with desired value in the superSQL
return superSQL;
}
参见例如Add a column to all MySQL Select Queries in a single shot
您的拦截器可以从配置中读取目录值并更改 SQL。
关于java - 如何根据属性设置注解的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39324960/