Friday, January 15, 2010

simple EJB3 JPA mappings+code example

Consider the following DB schema:


Now let's create the following entities: Movie, User and Screening. Figure out the mappings from the upper diagram


package com.cinema.entities;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.*;

@Entity
@Table(name="Movie")
public class Movie implements Serializable{

@Id
@Column(name="ID")
private int id;

@Column(name="title")
private String title;

@Column(name="genre")
private String genre;

@Column(name="duration")
private int duration;

@Column(name="director")
private String director;

@OneToMany(mappedBy="movie")
private Set screenings;

public Movie() {}



public Movie(String title, String genre, int duration, String director) {
super();
this.title = title;
this.genre = genre;
this.duration = duration;
this.director = director;
}



public Set getScreenings() {
return screenings;
}



public void setScreenings(Set screenings) {
this.screenings = screenings;
}



public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getGenre() {
return genre;
}

public void setGenre(String genre) {
this.genre = genre;
}

public int getDuration() {
return duration;
}

public void setDuration(int duration) {
this.duration = duration;
}

public String getDirector() {
return director;
}

public void setDirector(String director) {
this.director = director;
}



}




package com.cinema.entities;

import javax.persistence.*;

import java.io.Serializable;
import java.util.*;

@Entity
@Table(name="Screening")
public class Screening implements Serializable{

@Id
@Column(name="ID")
private int id;

@ManyToOne
@JoinColumn(name="Movie_ID", referencedColumnName="ID")
private Movie movie;

@Temporal(TemporalType.TIMESTAMP)
private Date time;

@ManyToMany(mappedBy="screenings")
private Set users;

public Screening(){}

public Screening(Movie movie, Date time) {
super();
this.movie = movie;
this.time = time;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public Movie getMovie() {
return movie;
}

public void setMovie(Movie movie) {
this.movie = movie;
}

public Date getTime() {
return time;
}

public void setTime(Date time) {
this.time = time;
}

public Set getUsers() {
return users;
}

public void setUsers(Set users) {
this.users = users;
}



}



package com.cinema.entities;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.*;



@Entity
@Table(name="User")
public class User implements Serializable{


@Id
@Column(name="username")
private String username;

@Column(name="password")
private String password;

@Column(name="role")
private String role;


@ManyToMany
@JoinTable(name="Bookings",
joinColumns=
@JoinColumn(name="username", referencedColumnName="username"),
inverseJoinColumns=
@JoinColumn(name="Screening_ID", referencedColumnName="ID"))
private Set screenings;


public String getUsername() {
return username;
}

public User() {}



public User(String username, String password, String role) {
super();
this.username = username;
this.password = password;
this.role = role;
}

public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}



public Set getScreenings() {
return screenings;
}

public void setScreenings(Set screenings) {
this.screenings = screenings;
}




}


There you go.... now edit persistence.xml and you're pretty much done:



xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">


org.hibernate.ejb.HibernatePersistence












Peace, out



No comments: