-->
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.  [ 2 posts ] 
Author Message
 Post subject: @Id mapping without PK constraint on the column
PostPosted: Thu Jul 21, 2016 12:41 pm 
Beginner
Beginner

Joined: Wed Mar 03, 2010 3:35 pm
Posts: 37
Hi,
we have a requirement to partition some of the existing tables by create timestamp. The Primary key constraint on the PK column has to be dropped now since it is not part of the the partitioning column list (column will be retained with auto increment in MYSQL, but without PK constraint , will add Unique Index on the column instead) .

Questions we have from hibernate perspective are,

1) we have an Entity mapped to this table already, can the <ID> mapping to the above column still be retained after PK constraint has been removed, but with unique index created on it or PK constraint for this column in the table is required?

2) similarly for mapping one to many relationships from parent -> child tables, is having a FK constraint created on the join column is absolutely required for hibernate to load the data or it can work without that too with the join column not having any FK constraint ?

I understand above even if they could work there could be data issues arising from the absence of the constraints and not a good practice, but we would like to know if it is possible in the first place as these tables are more of audit data so not very critical from data integrity perspective.

regards,
aravias


Top
 Profile  
 
 Post subject: Re: <ID> mapping without PK constraint on the column
PostPosted: Fri Jul 22, 2016 9:08 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1638
Location: Romania
Hi,

Hibernate is very lenient in this regard:

1. You don't even need to use a PK for the entity identifier. Any unique column can work just fine. The only requirement is for the column to be unique, and that's it.
2. For any association, Hibernate does not require a FK to actually be present on the database side. It can take any column that has a peer on the other table. SO no worries here as well.

But just like you said, having a PK for the id and a FK for the association join column is so much better for data integrity.

Vlad

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.