I am using hibernate's ORM and hibernate-generator to generate the Entity in the annotation way. I need to switch database frequently (dev/release). So, I have to change the entity's annotation every time. I want to know if there is a way to configure it.
@Entity
@Table(name = "my", catalog = "dev_db")
public class MyEntity {
}
As you can see, I've to change the catalog every time. How to configure it according to a jdbc.properties?
You can use Interceptors to modify SQL generated by hibernate.
public String onPrepareStatement(String sql) {
String superSQL = super.onPrepareStatement(newSQLWithNamespace);
//replace all catalog occurencies with desired value in the superSQL
return superSQL;
}
See e.g. Add a column to all MySQL Select Queries in a single shot
Your interceptor can read the catalog value from config and change the SQL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With