-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: JPA pojo generator - reveng - Legacy DB - FK versus no FK
PostPosted: Mon Sep 25, 2017 11:43 am 
Newbie

Joined: Mon Sep 25, 2017 10:17 am
Posts: 2
Hello everyone,

My name is Chris, I work in a small team that tries to slowly migrate an old c++ db code to java, we plan to use Hibernate's JPA api.
One requirement is that we have to keep the current database structure, which actually targets both Ms SQLServer and Oracle.
Therefore I used the hibernate generator (
Code:
org.hibernate.tool.ant.HibernateToolTask
) to rev-eng the db structure and generate JPA annotated POJOs.

Due to some SQLServer quirks, a table A that should have a FK towards table B is not actually modeled with a FK in the SQlServer db (on SQLServer, the delete cascade that we get for a FK is not recursive, so the db uses other mechanisms for achieving the effect of a recursive delete cascade, while avoiding the FK - sure, it's less than ideal, but it has been working like that in the past).

The same table A has an actual FK towards table B under Oracle (because Oracle's FK comes with a properly recursive delete cascade).

Now, the thing is, we will get two different pojo's based on our databases. The one we get for Oracle has an extra member of type B for the FK relation that is present in Oracle db.
The pojo we get for SQLServer does NOT have the same member, of course, because there is no direct FK linking A and B inside the SQLServer db. All this is normal, I know.

Still, I would like to use one POJO for all databases and since the logical structure is really the 'same', I'd like to find a way to use one mapping only.

So I have naively tried to use the POJO I got for Oracle with SQLServer, hoping that Hibernate will find the extra annotation and will use it to fetch B data inside A, without further checking the presence of the FK in the SQLServer db structure.
Instead, when Hibernate initializes, it checks the mapping info against my SQLServer connection and (correctly) fails with an annotation exception.

I understand why that happens.
Still, I would like to ask if there is a clean or 'almost' clean way to get what I want.
Which is clearly to use a single set of POJOs which can be used with both our databases (without touching the db structures).

I know, I know, this is not a clean approach. That's why I am here, because I want something as clean as possible, but without changing the database structure.

Is there by any chance something that could me, in my ingrate situation?

Thanks,
Chris


Top
 Profile  
 
 Post subject: Re: jpa pojo generator - reveng - legacy db - fk versus no fk
PostPosted: Mon Sep 25, 2017 11:59 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Yes, you can!

As I explained in this article, you can use a single class with the base annotations in place and override the mappings using XML configs for the other database.

You could have the base annotation mappings matching the SQL Server one, and add the extra mapping for Oracle in the orm.xml file which is only provided for the Oracle env.


Top
 Profile  
 
 Post subject: Re: JPA pojo generator - reveng - Legacy DB - FK versus no FK
PostPosted: Tue Sep 26, 2017 4:05 am 
Newbie

Joined: Mon Sep 25, 2017 10:17 am
Posts: 2
Thanks a lot Vlad,

So it's both possible to override and mix mappings at runtime, xml may actually override the loaded annotations, this is great insight for me!!

I actually like the Oracle mapping better, because it has the FK. And my SQLServer db actually has the 'same' structure but its own FK is not explicit. Is there, but not explicit (we disabled the actual FK and used some triggers to get a recursive delete cascade - because Microsoft doesn't support that with an explicit FK).

So I guess in this case, no matter how I use my mappings, I cannot 'enforce' the usage of the Oracle mappings (including the FK) with the SQLServer database (without explicit FK), since Hibernate will check the mappings against the db connections (when Hibernate initializes) and will fail to start.

So, I guess there is no way to use the Oracle pojo with the SQLServer db, I cannot 'fool'/disable Hibernate's checkings, is this correct?
(there is no way to specify some mapping info that doesn't precisely fit the actual db we're connecting to)

Many thanks, Vlad / Multumesc mult :D
Cristian


Top
 Profile  
 
 Post subject: Re: JPA pojo generator - reveng - Legacy DB - FK versus no FK
PostPosted: Tue Sep 26, 2017 4:15 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Quote:
So, I guess there is no way to use the Oracle pojo with the SQLServer db, I cannot 'fool'/disable Hibernate's checkings, is this correct?


Yes, you can even do that too!

Check out the @NotFound annotation in the User Guide. You could use it for SQL Server.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.