-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can Spring Data JPA support dynamic table name creation and query support? #2342
Comments
I'm not sure I understand what you are looking for. Could you elaborate? Maybe with some example pseudo code how a solution could look like? |
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed. |
sorry, i am so busy recently. We want a function, similar to the function of multiple data-source, which table can be dynamically created and selected in the program. As you know, there are more and more data now, we hope to dynamically create and select tables based on the month or dynamically create and select tables with the same fields based on the user ID. With the continuous increase of data, we hope to split the data according to users or date, with a table for each user, so that each query will only query the table data of the current user, so that the query data will be faster. I think it is a very important requirement. At present, this operation can only be supported in frameworks such as mybatis, but I don’t know how to implement it in Spring Data JPA, because each repository must know the name of the table in advance, and the table we need to operate is a dynamic table name. Over time, the data will be split according to the month, day, year, etc., and the database will be divided into tables. These tables will have the same fields but different table names Hope this feature will support dynamic creation and selection of tables like this: one ways split talbe based on user Id : t_like_video_record_10001 t_like_video_record_10002 t_like_video_record_10003 the another way split table based on date: t_like_video_record_10001_09_2021 t_like_video_record_10001_08_2020 t_like_video_record_10001_07_2019
define interface example as below:
the LikeVideoRecordEntityTempalte is the template entity table the actualTableName is the actual table name. we can called the method
if t_like_video_record_10001 table is not existed,spring data jpa can create it automatically. if t_like_video_record_10001 is existed,then do that as below: SELECT * FROM ${actualTableName} these filed fetch from LikeVideoRecordTempalteEntity object, but table name use actualTableName. |
JPA doesn't support this, so I don't see much of a chance to support it in Spring Data JPA. I also do think this well covered by the partitioned table feature that many RDBMSs offer. |
your meaning is spring data jpa doesnot'support dynamic create and query table name? Your suggestion is to use Sharding-JDBC, mycat...? |
Correct: Spring Data JPA doesn't support this. If one would want to add support it should be added to JPA and/or JPA implementations, not Spring Data JPA. My suggestion is to look into partitioned tables like: https://docs.oracle.com/cd/B10500_01/server.920/a96524/c12parti.htm |
For Fetech it's not possible to create you can, by extending PhysicalNamingStrategyStandardImpl |
use StatementInspector, refer to https://medium.com/@akshitagarwal97/how-we-used-hibernate-filter-and-inspector-to-mitigate-performance-issues-with-partitioning-in-d10babd1bbd1 public class MyJpaInterceptor implements StatementInspector {
@Override
public String inspect(String s) {
if (s.contains("order")) {
s = s.replace("order", DynamicTableNameDataHelper.getTableName());
}
return s;
}
} public class DynamicTableNameDataHelper {
private static final ThreadLocal<String> REQUEST_DATA = new ThreadLocal<>();
public static void setTableName(String tableName) {
REQUEST_DATA.set(tableName);
}
public static String getTableName() {
return REQUEST_DATA.get();
}
} usage example
|
hi,
team, as the title above, can Spring Data JPA support dynamic table name creation and query support?
we wish to have the function that Spring Data JPA support dynamic table name creation and query support.
The text was updated successfully, but these errors were encountered: