春天与春天JPA:按需创建数据库架构和表 [英] Spring & JPA: Create database schema and tables on demand

查看:61
本文介绍了春天与春天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:

假设我有两个实体,DogCat.当用户按下按钮时,将创建一个具有两个表DogCat的新架构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/

这篇关于春天与春天JPA:按需创建数据库架构和表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆