Home > OS >  ORA-32795: cannot insert into a generated always identity column | Hibernate ORM
ORA-32795: cannot insert into a generated always identity column | Hibernate ORM

Time:02-03

I am trying to insert in a oracle view using JPA. JPA generates below SQL statement-

insert into home_view (id, email, first_name, last_name) values (default, ?, ?, ?)

And when it executes query, Oracle DB throws below error-

ORA-32575: Explicit column default is not supported for modifying views

As per my understanding above query can not work with view because we can not insert into a view if its underlying table column is IDENTITY column.

Do we have any option in hibernate which helps exclusion of identity column while saving object to DB?

Note- I do not want to use raw SQL statement to insert my object.

I used GenerationType.SEQUENCE strategy and it is working fine, but only problem is I have to provide sequence name. I do not want to provide hard coded sequence name in Java Entity.

Thanks for your help!

CodePudding user response:

Use:

@Id @GeneratedValue(strategy=IDENTITY)
Long id;

UPDATE: For a view on Oracle, use:

  • @Id @Generated in Hibernate 6.2 (soon to be released), or
  • the SelectGenerator, in either Hibernate 5 or 6.

But note that you will have to identify a @NaturalId of your entity, since otherwise Hibernate has no way of locating the just-inserted row to retrieve the generated id.

In your case, the natural id is the email address, I suppose.

CodePudding user response:

Oracle only supports identity columns as of version 12. You didn't mention which version you are using, but I guess this is the reason for the Oracle error. Either way, Oracle just recently stopped supporting versions older than 19c, so either you update or you have to use a different generation strategy i.e. sequence, table or uuid.

  • Related