Vous êtes sur la page 1sur 2

package pack;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;

@Entity
@NamedQuery(name = "tousLesAuteurs", query = "select o FROM Auteur o")
public class Auteur implements Serializable {

private static final long serialVersionUID = 8279536554568120695L;


private long id;
private String nom = null;
private Collection<Livre> livres;

public Auteur() { livres = new ArrayList<Livre>(); }


public Auteur(final String nom) {
this();
setNom(nom);
}

@OneToMany(mappedBy = "auteur", fetch = FetchType.EAGER, cascade =


CascadeType.ALL)
// Defines strategies for fetching data from the database. The EAGER strategy
is a requirement on the persistence provider runtime that data must be eagerly
fetched.
public Collection<Livre> getLivres() {
return livres;
}

public void ajoutLivre(final String titre) {


Livre livre = new Livre();
livre.setTitre(titre);
livre.setAuteur(this);
livres.add(livre);
}

public void setLivres(final Collection<Livre> livres) { this.livres = livres; }

public String getNom() { return nom; }


public void setNom(final String nom) { this.nom = nom; }

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public long getId() { return this.id;}

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


}
package pack;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;

@Entity
@NamedQuery(name = "tousLesLivres", query = "select o FROM Livre o")

public class Livre implements Serializable {

private static final long serialVersionUID = 7870634228111717036L;

private long id;


private Auteur auteur;
private String titre;

public Livre() { }

public Livre(final String titre, final Auteur auteur) {


setTitre(titre);
setAuteur(auteur);
}

@ManyToOne
@JoinColumn(name = "Auteur_id")
public Auteur getAuteur() {
return auteur;
}

public void setAuteur(final Auteur auteur) { this.auteur = auteur;}


public String getTitre() { return titre;}
public void setTitre(final String titre) { this.titre = titre; }

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public long getId() {
return this.id;
}

public void setId(final long id) {


this.id = id;
}
}

Vous aimerez peut-être aussi