如何在Hibernate中使用SELECT进行INSERT

ava*_*lon 5 java sql orm hibernate hql

我需要在hibernate中实现以下请求:

insert into my_table(....,max_column)
values(...,(select max(id) from special_table where ....))
Run Code Online (Sandbox Code Playgroud)

如何在hibernate中使用注释来做到这一点?special_table可能不是my_table的子项或依赖项,只是一个subselect.

Vla*_*cea 6

您可以使用HQL INSERT INTO语法:

String hqlInsert = "insert into MyEntity(....,max_column) select ..., max(id) from SpecialEntity where ....";
int updateCount = session.createQuery(hqlInsert).executeUpdate();
Run Code Online (Sandbox Code Playgroud)