13

Reading that

how to do hibernate mapping for table or view without a primary key

I am wondering how to add a primary key to my view as it is basically just a stored query...?

PS: oracle 10g

thx

Community
  • 1
  • 1
Hoax
  • 1,004
  • 2
  • 15
  • 31

1 Answers1

35

We can add a disabled primary key constraint to a view. That is, the constraint does not fire if an insert or update is run against the view. The database expects integrity to be maintained through constraints on the underlying tables. So the constraint exists solely for the purposes of documentation.

SQL> create view emp_view as select * from emp
  2  /


View created.

SQL> alter view emp_view add constraint vemp_pk primary key (empno) disable
  2  /

View altered.

SQL> 

Caveat: I have never tried this with Hibernate, so I don't know whether it would work in your scenario. However, I do know sites which use Hibernate exclusively against a layer of views, so I presume it does. Please experiment with the syntax and report back.

APC
  • 144,005
  • 19
  • 170
  • 281
  • 1
    This is really a very nice surprise and something I long was looking for. This just helped me to tame a quey that makes the cost based optimizer go nuts. Exactely what I was looking for while googling... – olippuner Aug 30 '18 at 09:49
  • @olippuner - glad it helped you. Providing Seekers with an answer through Google is the main goal of SO. – APC Aug 30 '18 at 11:22