-->
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: Define Foreig Keys in Hibernate Mapping
PostPosted: Mon Jun 23, 2008 8:45 am 
Newbie

Joined: Fri Jun 20, 2008 8:56 am
Posts: 15
Hello.

I´m new to Hibernate and I am a bit confussed.
Ihave two tables Users and Departments.
These are the java classes:

Users
int ID_User;
String Name;

Departments
int ID_Dept;
String Text_Dept;
int User

A Department can have many Users and a User can belong just to one Department.

In my Database the column User in Departments table isn´t declared as a Foreign Key referencing ID_Users

Can I define the foreign keys in the mapping files?
How would be these mapping files?

Thank you all.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 23, 2008 10:35 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
I have a similar scenario that uses a Team and Players in a free tutorial on my website. Just replace Team with User and Players with Departments, and you'll have all of the required associations.


http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=18mappingonetomanyassociations

Image


Code:
import javax.persistence.*;
@Entity
public class Player {
private long id;
private String nickName;
private Team team;

@ManyToOne
@JoinColumn(name="team_id")     
public Team getTeam() {return team;}
public void setTeam(Team t) {this.team = t;}
@Id
@GeneratedValue
public long getId() {return id;}
public void setId(long id) {this.id = id;}
public String getNickName() {return nickName;}
public void setNickName(String n) {nickName = n;}
}



Code:
import java.util.List; import javax.persistence.*;
@Entity
public class Team {
private long id;
private String name;
private List<Player> players;

@OneToMany(mappedBy="team", 
              targetEntity=Player.class,
    fetch=FetchType.EAGER, cascade = CascadeType.ALL)
public List<Player> getPlayers() {return players;}
public void setPlayers(List<Player> p){players=p;}
@Id
@GeneratedValue
public long getId() {return id;}
public void setId(long id) {this.id = id;}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
}





You will need a FK in the database. You can just have Hibernate create the association by calling the create method of the SchemExport class. It's pretty easy to do with Hibernate.


Here's the SQL code I actually used to create the tables. You could use this as a template if you want:


Code:
CREATE TABLE  'examscam'.'player'
('id' bigint(20) NOT NULL auto_increment,
'nickName' varchar(255) default NULL,
'team_id' bigint(20) default NULL,
PRIMARY KEY  ('id'),
KEY 'FK8EA387017289A91D' ('team_id'));


CREATE TABLE  'examscam'.'team'
( 'id' bigint(20) NOT NULL auto_increment,
'name' varchar(255) default NULL,
PRIMARY KEY  ('id'));

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 23, 2008 12:04 pm 
Newbie

Joined: Fri Jun 20, 2008 8:56 am
Posts: 15
Thank you Cameron for your help.
But there´s something it isn´t clear to me yet. If Foreign Keys are not defined in database, hibernate won´t do mapping?

Thank´s.


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.