通过JPA EntityManager执行创建表查询 [英] Execute a Create Table Query through the JPA EntityManager

查看:2383
本文介绍了通过JPA EntityManager执行创建表查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在通过JPA EntityManager访问的数据库中创建一个新表。 JPA NativeQueries支持选择或更新以外的查询吗?还是有另一种先进的方式来执行一个复杂的SQL查询在JPA上下文中?

解决方案

jpa 本机查询只能用于DML语句(数据操作语言)。要发出任何DDL,例如创建表,您需要从EntityManager获取底层连接。



如何从EM中提取连接取决于你使用的JPA实现,但肯定会包含调用EntityManager.getDelegate()。另外(我认为这是一个更好的方法),注入DataSource或一个JDBCTemplate,如果您使用spring来尝试创建表的对象,并使用该类来创建表。


I need to create a new Table in a Database I access through a JPA EntityManager. Do JPA NativeQueries support Queries other than "Select" or "Update"? Or is there another state-of-the-art way to execute a complex SQL query in a JPA Context?

解决方案

The jpa "native queries" can only be used for DML statements (Data Manipulation Language). To issue any DDL, such as a create table, you'll need to get the underlying connection from the EntityManager.

How to extract the connection from the EM will depend on which JPA implementation you're using, but will definitely include invoking EntityManager.getDelegate().

Alternatively (and I think it's a better approach), inject the DataSource or a JDBCTemplate if you're using spring to the object that tries to create the table, and use that class to create the table.

这篇关于通过JPA EntityManager执行创建表查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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