-->
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: Checking if Values already exist in a separate table
PostPosted: Thu Sep 30, 2010 12:51 pm 
Newbie

Joined: Fri Jul 02, 2010 11:11 am
Posts: 4
Hi,

I was wondering what's the best way to check if a value exist in another table before doing an insert.

For example, I have something like:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class A
{
String name
String lastname
String group
}

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class B
{
@Id
group
}

Table B is fully populated and I would like Table A to throw a foreign key constraints if the value in Table A group does not exist in Table B.

Is there a way to do this with some kind of linking? and how would I link it if possible?

Thanks


Top
 Profile  
 
 Post subject: Re: Checking if Values already exist in a separate table
PostPosted: Thu Sep 30, 2010 10:38 pm 
Beginner
Beginner

Joined: Fri Mar 11, 2005 7:46 am
Posts: 29
ManyToOne or OneToOne associations should help you - have a look at the doco -
http://docs.jboss.org/hibernate/stable/ ... tions.html

and a good explanation here:

http://www.neeraj.name/2005/05/05/hiber ... ction.html


Top
 Profile  
 
 Post subject: Re: Checking if Values already exist in a separate table
PostPosted: Fri Oct 01, 2010 11:46 am 
Newbie

Joined: Fri Jul 02, 2010 11:11 am
Posts: 4
Thanks for the responds! It lead me in the right directions. Guess I was afraid the linking would generate the value in class B also but if you only set it on the class A side and not have the class B link to class A then everything is good.

I was able to do it this way:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class A
{
String name
String lastname
@ManyToOne
@JoinColumn(name="group")
B b//String group
}

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class B
{
@Id
group
@OneToMany(mappedBy="group")
Set<A> a;
}


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.