春天与春天JPA:按需创建数据库架构和表
在JPA/Spring中是否有可能在运行时创建/删除具有自定义名称和相应表(由@Entity批注提供)的数据库模式,例如用户何时按下按钮?
Is there any possibility in JPA/Spring to create/delete a database schema with a custom name and according tables (given by @Entity annotations) at runtime, e.g. when the user presses a button?
我只知道javax.persistence.schema-generation
属性,这些属性在我的应用程序启动时执行,并使用预定义的架构名称.
I only know about the javax.persistence.schema-generation
properties, which are executed at startup of my application and use a predefined schema name.
我正在寻找的简短(简化)示例:
A short (and simplified) example of what I am looking for:
假设我有两个实体,Dog
和Cat
.当用户按下按钮时,将创建一个具有两个表Dog
和Cat
的新架构animals1
.现在,所有查询都转到架构animals1
.当用户第二次按下按钮时,将创建具有两个表的不同架构animals2
,并且所有查询现在都进入animals2
.
Let's say I have two entities, Dog
and Cat
. When the user presses a button, a new schema animals1
with two tables Dog
and Cat
is created. All queries now go to schema animals1
. When the user presses the button a second time, a different schema animals2
with the two tables is created, and all queries now go to animals2
.
我认为我在春季可以做些魔术,但是我不知道从哪里开始.我将不胜感激.
I assume there is some magic I can do in Spring, but I have no clue where to start. I would be grateful for any hints.
JPA中有一个用于"generateSchema"的API:
There is an API for "generateSchema" in JPA:
检查此博客条目 https://antoniogoncalves.org/2014/12/11/generating-database-schemas-with-jpa-2-1/