开发者

Tell hibernate hbm2ddl not create individual table

Is there a way to tell hibernate's hbm2ddl to not create specific table but still have the model be recognized by Hibernate.

The thing is that the model map to a view and I want to have an in-mem开发者_C百科ory database (empty on startup and deleted on termination) for testing, hence, having 2 sets of mapping is out of the question.


Okay, this doesn't exactly answer the question (there's probably no way to do it with current version) but it does solve the issue at hand.

So, in the end I let hibernate create the table but later on forcefully drop it and put in my own create view statement. It seems that there are 2 ways to do it.

The first way is by using the <database-object> element, specifically the child element called <create>, like so:

<class table="MY_VIEW"></class>
<database-object>
  <create>
    drop table MY_VIEW;
    create view MY_VIEW etc etc;
  </create>
</database-object>

The other way is by entering the same thing in the import.sql. This thing is undocumented. I don't know why, perhaps it's deprecated. I assume it is so, hence I won't put too much detail here. It's not deprecated, but I find the previous method less painful (the create view is several lines long).


Is there a way to tell hibernate's hbm2ddl to not create specific table

AFAIK, hbm2ddl is "all or nothing", you can't exclude specific tables. But you could use it to output the generated DDL to a file instead of automatically exporting it to the database if you want to alter the DDL. Would this help?

but still have the model be recognized by Hibernate.

I didn't get that part. Do you mean having Hibernate validate the database against the mapping?


I had a similar problem. I'm trying to extend an existing schema, so I only want my "new" tables to be created (dropped/altered/etc). I couldn't find any way to tell the hbm2ddl tool to use these entities in its model for validation, but not to generate SQL for them.

So I wrote a simple Perl script to remove those statements from the generated SQL. It's designed to work in a shell script pipeline, like so:

cat your-sql-file.sql | scrub-schema.pl table1 table2 table3 ... > scrubbed.sql

The code is available here (uses the Apache v2 license):

https://github.com/cobbzilla/sql-tools/blob/master/scrub-schema.pl

I hope this is helpful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜