package aufgabe09.entitaeten;

import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Version;
import javax.validation.constraints.Min;

@Entity
public class Bestellposten implements Serializable {
  @Id @GeneratedValue
  private int pnr;

  @ManyToOne(cascade = {CascadeType.PERSIST,CascadeType.MERGE})
  private Bestellung bestellung;

  @ManyToOne(cascade = {CascadeType.PERSIST,CascadeType.MERGE})
  private Produkt produkt;

  @Min(value=1, message="Mindestens ein Produkt")
  private int anzahl;

  public Bestellposten(){}

  public Bestellposten(Bestellung bestellung, Produkt produkt, int anzahl) {
    this.bestellung = bestellung;
    this.produkt = produkt;
    this.anzahl = anzahl;
  }

  public int getAnzahl() {
    return anzahl;
  }

  public void setAnzahl(int anzahl) {
    this.anzahl = anzahl;
  }

  public Bestellung getBestellung() {
    return bestellung;
  }

  public void setBestellung(Bestellung bestellung) {
    this.bestellung = bestellung;
  }

  public int getPnr() {
    return pnr;
  }

  public void setPnr(int pnr) {
    this.pnr = pnr;
  }

  public Produkt getProdukt() {
    return produkt;
  }

  public void setProdukt(Produkt produkt) {
    this.produkt = produkt;
  }

  public int getVersion() {
    return version;
  }

  public void setVersion(int version) {
    this.version = version;
  }

  @Version
  private int version;



  @Override
  public String toString(){
    return "ID:"+pnr+" "+produkt.getName()+" Anz.:"+anzahl+" in "+bestellung.getBestnr();
  }

}
