简体   繁体   中英

How to INSERT using a SELECT in Hibernate

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.

You can use the INSERT INTO ... SELECT ... feature :

int updateCount = session.createQuery("""
    insert into MyEntity(
        ...,
        max_column
    ) 
    select 
        ..., 
        max(id) 
    from SpecialEntity 
    """)
.executeUpdate();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM