Reputation: 86627
I have an @Entity
class to persist using spring-boot
, hibernate
and postgres
.
Now I want to dynamically adjust the table where the entity gets saved to (or read from). Is that possible?
My goal is to create some kind of caching table with large datasets. I want to optimized and refresh the cache once a day (during that procedure, that might last several hours, I want any entity operation to take place on the other table). Then, after the rebuild and optimization finished, I want to switch back to the first table (and use the 2nd table vice versa when rebuilding again).
Is that possible at all? If not, how could I achieve this?
Upvotes: 0
Views: 387
Reputation: 13259
You could extend the class like:
@Entity
@Table("cache_table_name")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
class CachedVersionOfOriginal extends Original {
//nothing here
}
They will have the same structure and TABLE_PER_CLASS insures two different tables.
If you want to pull Original and then insert to the CachedVersionOfOriginal, you could use the Proxy Pattern like:
@Entity
@Table("cache_table_name")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
class CachedVersionOfOriginal extends Original {
private Original original;
public CachedVersionOfOriginal(Original original){
this.original = original;
}
//Then delegate
@Override
public String getPropertyA(){
return orginal.getPropertyA();
}
}
or use Apache Commons to do Property by Property copy
Upvotes: 1