vendredi 5 juin 2015

JPA Set Mapping

package cinema.model;

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

import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@XmlRootElement
@NamedQueries({@NamedQuery(name = "getAllProjections", query = "SELECT b FROM Projection b")})
public class Projection implements Serializable {

private static final long serialVersionUID = -2929008106626811914L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private String movieTitle;

private int hallNumber;

private int freePlaces;

@ElementCollection()
private Set<Boolean> places = new HashSet<Boolean>();

//TODO change var type
private String startTime;

private String posterUrl;

public Projection() {
}

public Projection(String movieTitle, int hallNumber, int freePlaces, String startTime, String posterUrl) {
    super();
    this.movieTitle = movieTitle;
    this.hallNumber = hallNumber;
    this.freePlaces = freePlaces;
    this.startTime = startTime;
    this.posterUrl = posterUrl;
    System.out.println("Constructor");
    initPlaces(freePlaces); 
}

public Long getId() {
    return this.id;
}

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

public String getMovieTitle() {
    return movieTitle;
}

public void setMovieTitle(String movieTitle) {
    this.movieTitle = movieTitle;
}

public int getHallNumber() {
    return hallNumber;
}

public void setHallNumber(int hallNumber) {
    this.hallNumber = hallNumber;
}

public int getFreePlaces() {
    return freePlaces;
}

public void setFreePlaces(int freePlaces) {
    this.freePlaces = freePlaces;
}

public String getStartTime() {
    return startTime;
}

public void setStartTime(String startTime) {
    this.startTime = startTime;
}

public String getPosterUrl() {
    return posterUrl;
}

public void setPosterUrl(String posterUrl) {
    this.posterUrl = posterUrl;
}

public Set<Boolean> getPlaces(){
    return this.places;
}

public void initPlaces(int freePlaces){
    System.out.println("initmethod");
    for(int i = 0; i < freePlaces; i++){
        this.places.add(true);
    }
}


@Override
public String toString() {
    String result = getClass().getSimpleName() + " ";

    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (!(obj instanceof Projection)) {
        return false;
    }
    Projection other = (Projection) obj;
    if (id != null) {
        if (!id.equals(other.id)) {
            return false;
        }
    }
    return true;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((id == null) ? 0 : id.hashCode());
    return result;
}
}

This is a simple entity use to represent a cinema, is there a way when a send a json file to frond-end in this file to have a field for the Set places. The set will be used to represent the free places in a cinema hall, from where the people can reserved their places in the hall. My json now is :

{
"projection":
[

{
"startTime":"16:00",
"movieTitle":"Furious Seven",
"hallNumber":12,
"id":951,
"freePlaces":12,
"posterUrl":"http://ift.tt/1MYwUvZ",
 },

 {
 "startTime":"19:00",
 "movieTitle":"The Theory of Everything",
 "hallNumber":124,
 "id":952,
 "freePlaces":215,
  "posterUrl":"http://ift.tt/1v662wh",
 },

 {
 "startTime":"21:00",
 "movieTitle":"The Imitation Game",
 "hallNumber":13,
 "id":953,
 "freePlaces":52,
 "posterUrl":"http://ift.tt/1GpqMdc",
  },

 {
 "startTime":"11:00",
 "movieTitle":"Taken 3",
 "hallNumber":12,
 "id":954,
 "freePlaces":52,
 "posterUrl":"http://ift.tt/1GpqMKc",
 },

 {
 "startTime":"21:41",
 "movieTitle":"American Sniper",
 "hallNumber":52,
 "id":955,
 "freePlaces":51,
 "posterUrl":"http://ift.tt/1QxwNUv",
 },
 ],

}

Aucun commentaire:

Enregistrer un commentaire