Need to know how to do an insert with HQL with a select and that select contains a union. Here's my test case
create table simple_author (
id bigint generated by default as identity,
first_name varchar(255),
last_name varchar(255),
primary key (id))
@Entity
public class SimpleAuthor {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
private String firstName;
private String lastName;}
Assume the requirement is to insert 2 authors and if one fails for whatever reason the other is not persisted in the database.
Using just SQL and if Oracle or H2 I can do Insert into simple_author(first_name, last_name) select ‘bob’,’smith’ from dual union select ‘frank’, ‘brown’ from dual
Or if using SQL Server: Insert into simple_author(first_name, last_name) select ‘bob’,’smith’ union select ‘frank’, ‘brown’
Assume Bob Smith and Frank Brown are supplied by a user entering data in a UI.
Looking at https://docs.jboss.org/hibernate/orm/5.3/userguide/html_single/Hibernate_User_Guide.html#hql-insert Seems to support a simple case. Seems there are a couple of issues: Doing a union and the list of values to be added in this case bob smith and frank brown For the union have tried a couple of solutions as seen in threads
and
How to execute query with union in hibernate?
at best they only seem to get close to what is required. So the question is How to write: Insert into simple_author(first_name, last_name) select ‘bob’,’smith’ from dual union select ‘frank’, ‘brown’ from dual In hsql