-->
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.  [ 3 posts ] 
Author Message
 Post subject: Scalar foreign keys?
PostPosted: Sat May 02, 2009 3:51 pm 
Newbie

Joined: Wed Mar 11, 2009 8:39 pm
Posts: 3
Hello,

I am using JPA\Hibernate and would like to know if it is possible to create a foreign key contraint on a scalar value?

For example, I have a Person entity and an Address entity. Instead of having a relationship and putting Address as a member of Person, I would like Person to have an integer addressId. To preserve referencial integrity, I would like a foreign key constrain from addressId in the Person table, to addressId in the Address table.

Is this possible using JPA or Hibernate annotations? I have tried many different ways using @JoinColumn and other annotations but could not get it working.

Regards


Top
 Profile  
 
 Post subject: Re: Scalar foreign keys?
PostPosted: Sat May 02, 2009 10:32 pm 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
Try this
Code:
[size=150]@Entity
class Person{
.................

.................

@ManyToOne
@JoinColumn(name="address_id")
Address address;
}

@Entity
class Address{
..........
...........
}[/size]


Top
 Profile  
 
 Post subject: Re: Scalar foreign keys?
PostPosted: Mon May 04, 2009 7:45 pm 
Newbie

Joined: Wed Mar 11, 2009 8:39 pm
Posts: 3
Thanks for the response but I am storing an int in Person, not Address. So I need something like below to produce a database constraint on addressId.

Code:
class Person
{
       private int addressId;

       int getAddressId()
       {
              return addressId;
       }
}

class Address
{
       private int id;
}



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