BitTigerInst / BitTiger-CS504-FAQ

CS504 后端工程师直通车 FAQ
20 stars 10 forks source link

如何用SpringData JPA去连接table with dynamic name? #47

Open bihjuchiu opened 7 years ago

bihjuchiu commented 7 years ago

当define table 的时候, table name 是define在@Table的name里, 如下例:

====================================================== @Entity @Data @Table(name = "category", indexes = {@Index(name = "index_category_name", columnList = "categoryName", unique = true)}) public class Category { @Id @GeneratedValue private long id;

====================================================== 但是如果table name 是每日更新的话,比如: name = category_mm_dd_yyyy ex: category_01_01_2017: category generated at 01/01/2017 category_01_02_2017: category generated at 01/02/2017 category_01_03_2017: category generated at 01/03/2017

有没有办法设计 Category object 去dynamically更新table name?

icyhins commented 7 years ago

这样设计感觉比较怪异,因为基本数据库结构是设计阶段就固定下来,除非因为业务的需要偶尔做扩展。 动态表结构,不如以表名作为key保存在Redis中,并在SQL DB,使用date字段来作为对应日期的搜索条件。

bihjuchiu commented 7 years ago

嗯嗯,这是一种解决的办法。。。

jygan commented 7 years ago

you can use stored procedure https://stackoverflow.com/questions/10877407/t-sql-how-to-create-tables-dynamically-in-stored-procedures