Social Media

JPA Entity Relationships

This section covers the more common entity mappings between Object Models and Database. Its often easier to use the tools to create these relationships, but its worth knowing the basics –

  • One-to-One (Unidirectional)
  • One-to-One (Bidirectional)
  • Many-to-One (Unidirectional)
  • Many-to-One (Bidirectional)
  • Many-to-Many (Both Directions)

One-to-One(Unidirectional)

User Address

user_id
address_id ———> address_id

Tables
[sourcecode lang=”sql”] CREATE TABLE USER
(
USER_ID NUMBER(38,0),
ADDRESS_ID NUMBER(38,0)
);

CREATE TABLE ADDRESS
(
ADDRESS_ID NUMBER(38,0)
);
[/sourcecode]

Classes

[sourcecode lang=”java”] @Entity
public class User {

@Id
@Column(name="USER_ID")
private int userId;

@OneToOne
@JoinColumn(name="ADDRESS_ID")
private Address address;

}

@Entity
public class Address {

@Id
@Column(name="ADDRESS_ID")
private int addressId;

}
[/sourcecode]

One-to-One (Bidirectional)

We could also make the User-Address relationship bidirectional –

Classes

[sourcecode lang=”java”] @Entity
public class User {

@Id
@Column(name="USER_ID")
private int userId;

@OneToOne
@JoinColumn(name="ADDRESS_ID")
private Address address;

}

@Entity
public class Address {

@Id
@Column(name="ADDRESS_ID")
private int addressId;

@OneToOne(mappedBy="address")
private User user;

}
[/sourcecode]

Many-to-One (Unidirectional)

Role User

user_id
role_id ———> role_id

Tables

[sourcecode lang=”sql”] CREATE TABLE USER
(
ROLE_ID NUMBER(38,0),
USER_ID NUMBER(38,0)
);

CREATE TABLE ROLE
(
ROLE_ID NUMBER(38,0)
);
[/sourcecode]

Classes

[sourcecode lang=”java”] @Entity
public class User {

@Id
private int userId;

@ManyToOne
private Role role;

}

@Entity
public class Role {

@Id
private int roleId;

}
[/sourcecode]

Many-to-One (Bidirectional)

Classes

[sourcecode lang=”java”] @Entity
public class User {

@Id
private int userId;

@ManyToOne
private Role role;

}

@Entity
public class Role {

@Id
private int roleId;

@OneToMany(mappedBy="role")
private Collection users;

}
[/sourcecode]

Many-to-Many (Both Directions)

One user could also have many roles through a JOIN table –

USER USER_ROLE ROLE

user_id ———> user_id
role_id ———-> role_id

Tables

[sourcecode lang=”sql”] CREATE TABLE USER
(
USER_ID NUMBER(38,0)
);

CREATE TABLE USER_ROLE
(
USER_ID NUMBER(38,0),
ROLE_ID NUMBER(38,0)
);

CREATE TABLE ROLE
(
ROLE_ID NUMBER(38,0)
);
[/sourcecode]

Classes

[sourcecode lang=”java”] @Entity
public class User {

@Id
private int userId;

@ManyToMany
@JoinTable(name="USER_ROLE",
joinColumns=@JoinColumn(name="USER_ID"),
inverseJoinColumns=@JoinColumn(name="ROLE_ID"))
private Collection roles;

}

@Entity
public class Role {

@Id
private int roleId;

@ManyToMany(mappedBy="roles")
private Collection user;

}
[/sourcecode]

References

http://en.wikibooks.org/wiki/Java_Persistence/

About the Author Martin Farrell

My name is Martin Farrell. I have almost 20 years Java experience. I specialize inthe Spring Framework and JEE. I’ve consulted to a range of businesses, and have provide Java and Spring mentoring and training. You can learn more at About or on my consultancy website Glendevon Software

follow me on:

Leave a Comment: