我需要使用 hibernate 创建一个临时表,然后将列表中的数据插入到该表中。这就是我的意思:
String hql = "create temporary table temp (id int)";
session.createQuery(hql);
在这个 for 循环中,我将列表中的数据插入到表“temp”中:
for(Example example : examples) {
hql = "insert into temp (" + example.getId() + ")";
query = session.createQuery(hql);
query.executeUpdate();
}
当我用examples
(这是一个列表)中的值填充表时,我可以使用该表进行另一个查询。创建和插入到该表必须在我的代码中自动完成,不能在数据库中手动完成,因为列表 'examples'
是由代码创建的。
这是我对代码的看法,但它不起作用。谁能告诉我如何以正确的方式做到这一点?谢谢。
最佳答案
我不认为 Hibernate 查询语言管理“创建”操作。如果您使用 EntityManager,则可能需要在使用 createNativeQuery 时运行 native 查询;如果您使用 Hibernate Session,则可能需要在 createSQLQuery 中运行 native 查询:
String hql = "create temporary table temp (id int)";
session.createSQLQuery(hql).executeUpdate();
关于java - 如何创建列表中的数据并将其插入到 Hibernate 查询中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29797837/