How to INSERT using a SELECT in Hibernate

丶灬走出姿态 提交于 2020-12-08 08:47:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!