Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set annotation's values according to properties?

Tags:

java

hibernate

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?

like image 815
blackdog Avatar asked Dec 02 '25 10:12

blackdog


1 Answers

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.

like image 126
StanislavL Avatar answered Dec 04 '25 22:12

StanislavL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!