Ok the problem is that i have a aplication that make queries to different DB, and some Entities there are missing columns. Is there a way to ignore missing columns without using inheritance?
-
2To improve your question provide details of what you tried (a small example of the mapping/class/table), why it didn't work possibly including an example of the error message.Gus– Gus2013-03-07 20:20:56 +00:00Commented Mar 7, 2013 at 20:20
-
I had the same Question. I resolved the issue by creating a main class annotated as MappedSuperclass which contains all properties that are everywhere available. Then I created two inherating classes. First one was tagged has no own field, the second one contains the fields, that are only in one of the DBs available. This solution uses inheritance. But I hope it helps someone.Jens876– Jens8762015-04-09 09:30:21 +00:00Commented Apr 9, 2015 at 9:30
1 Answer
There are different possibilities depending of your needs:
If you never need these extra columns, which exist only in some databases, then you just do not define them in the mapping (or in the annotations). Then they are never read by Hibernate, and on insertion the columns are set to null (you can define default values or triggers in the database if you want values different than null).
If you need the extra columns when they exist, then you need different mappings for the different databases. Better you use xml mapping files for this case. Anyway you have an individual hibernate.cfg.xml for each database (the connect string is in there). In here for the tables with different columns you specify different mapping files (tag
<mapping resource="..." />). The Java pojo, i. e. the Java class, is the same for all databases, and it contains all columns which exist on any database, but in the database specific mapping files you only map those columns to Java members which really exist in that database. (With annotations this is annoying because then you need different Java classes for different databases and you have to handle the logic in your code.)If the tables are read only, then you can define a database view over that table and let this view providing missing columns with default values. Then in Hibernate you map the view and not the table.