问题
I need to implement the following request in hibernate:
insert into my_table(....,max_column)
values(...,(select max(id) from special_table where ....))
How to do that in hibernate, using annotations? special_table may be not a child or dependency of my_table, just a subselect.
回答1:
You can use the INSERT INTO ... SELECT ... feature:
int updateCount = session.createQuery("""
insert into MyEntity(
...,
max_column
)
select
...,
max(id)
from SpecialEntity
""")
.executeUpdate();
来源:https://stackoverflow.com/questions/30635715/how-to-insert-using-a-select-in-hibernate