【发布时间】:2019-10-23 08:53:58
【问题描述】:
是的,我有这个问题,jpa如何使用动态表名运行DDL sql? 通常,我只使用 DQL 和 DML,例如“选择、插入、更新、删除”。 如:
public interface UserRepository extends JpaRepository<User, Integer> {
@Query(value = "select a.* from user a where a.username = ? and a.password = ?", nativeQuery = true)
List<User> loginCheck(String username, String password);
}
但是当我需要在下面运行 DDL sql 时
String sql = "create table " + tableName + " as select * from user where login_flag = '1'";
我找不到使用 Jpa(或 EntityManager)解决此问题的方法。 最后我用JDBC来运行DDL sql,但我觉得它很难看...
Connection conn = null;
PreparedStatement ps = null;
String sql=" create table " + tableName + " as select * from user where login_flag = '1' ";
try {
Class.forName(drive);
conn = DriverManager.getConnection(url, username, password);
ps = conn.prepareStatement(sql);
ps.executeUpdate();
ps.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
那么,jpa 能否用动态表名轻松运行 DDL sql(如 CREATE/DROP/ALTER)?
【问题讨论】:
标签: jpa spring-data-jpa spring-boot-jpa