Say I have a table like so:
CREATE TABLE big_table (UUID varchar(32) not null, ... );
I have a query on the table that I can't express as an HQL or Criteria query. I am trying to set up the query as view in Oracle, like so:
CREATE VIEW big_table_view AS SELECT bt.* FROM big_table bt
LEFT OUTER JOIN ...
-- (multicolumn subselect over big_table for some historical stuff)
WHERE ...
I am trying to map the same Java class to both the table and the view. That would be really cool because then I can run the same Criteria queries against both, etc.
My problem is that I can't come up with an HBM mapping file that doesn't wreak havoc with my HSQLDB test code. My test setup is a typical Maven/Spring test setup with hibernate.hbm2ddl.auto
set to create-drop
so that Hibernate creates the HSQLDB schema on the fly for testing.
My mapping file currently looks like this:
<hibernate-mapping>
<class name="com.example.BigPojo" entity-name="bigPojo"
table="big_table">
&commonPropertiesEntity;
</cla开发者_StackOverflow中文版ss>
<class name="com.example.BigPojo" entity-name="bigPojoView"
table="big_table_view">
&commonPropertiesEntity;
</class>
</hibernate-mapping>
...when I run my tests, they blow up all over the place because HSLQDB is trying to create a table called big_table_view
with all the same foreign key constraints, indices, etc. I'm trying to fix the schema after it gets created via a database-object
element like this:
<database-object>
<create>
DROP TABLE big_table_view CASCADE;
CREATE VIEW big_table_view...
</create>
<drop>
DROP VIEW big_table_view IF EXISTS;
</drop>
<dialect-scope name="org.hibernate.dialect.HSQLDialect" />
</database-object>
...but there's still something breaking and I'm still wading through trying to figure it out. Is there a way to tell Hibernate to exclude the bigPojoView
entity from hbm2ddl
? Is there a better way to do this mapping generally? I'm open to any advice...
There's no easy way to exclude a table from hbm2ddl. You can, however, map your view via Hibernate's subselect: see footnote #20 under 5.1.1.
Something like:
<class name="com.example.BigPojo" entity-name="bigPojoView"
<subselect>
... your view definition here ...
</subselect>
<synchronize table="big_table"/>
<id name="UUID"/>
...
</class>
精彩评论