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.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
@Entity
@SuppressWarnings("serial")
@Table(name="TParticipant")
public class Participant implements Serializable {
private Integer pk_part;
private String firstname;
private String surname;
private String chipnumber;
private Contact contact;
private int fk_paid;
private Paid paid;
private int fk_result;
private Result result;
private int fk_club;
private Club club;
private int fk_gender;
private Gender gender;
private int fk_address;
private Address address;
private int fk_born;
private Born born;
private Collection<Event> events;
public Participant() {
events = new ArrayList<Event>();
}
@Id
@GeneratedValue(generator="foreign")
@GenericGenerator(name="foreign", strategy = "foreign", parameters={
@Parameter(name="property", value="contact")
})
public Integer getPk_part() {
return pk_part;
}
public void setPk_part(final Integer pk_part) {
this.pk_part = pk_part;
}
/* Contact 1:1 */
@OneToOne(optional=true)
@PrimaryKeyJoinColumn
public Contact getContact() {
return contact;
}
public void setContact(final Contact contact) {
this.contact = contact;
}
/* Primarykey Generator is only possible once, therefore a separate colomn: 1:1 relation */
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="fk_paid")
public Paid getPaid() {
return paid;
}
public void setPaid(Paid paid) {
this.paid = paid;
}
/* 1:1 relation */
@OneToOne(cascade = CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name="fk_result")
public Result getResult() {
return result;
}
public void setResult(final Result result) {
this.result = result;
}
/* 1:n relation */
@ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER) // CascadeType.ALL muss hier so angegeben werden da ansonsten zuerst der Club gespeichert werden muss um diesen dann zum Participant hinzufügen zu können
@JoinColumn(name="fk_club")
public Club getClub() {
return club;
}
public void setClub(final Club club) {
this.club = club;
}
/* 1:n relation */
@ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER) // CascadeType.ALL muss hier so angegeben werden da ansonsten zuerst der Club gespeichert werden muss um diesen dann zum Participant hinzufügen zu können
@JoinColumn(name="fk_born")
public Born getBorn() {
return born;
}
public void setBorn(final Born born) {
this.born = born;
}
@ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name="fk_gender")
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
@ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name="fk_address")
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@ManyToMany(
fetch=FetchType.EAGER, /* load all objects immediately, standard is lazy */
targetEntity=at.eventtiming.participant.domain.Event.class,
cascade=CascadeType.ALL)
@JoinTable(
name="TEvent_TParticipant",
joinColumns=@JoinColumn(name="id_part"),
inverseJoinColumns=@JoinColumn(name="id_event")
)
public Collection<Event> getEvents() {
return events;
}
public void setEvents(final Collection<Event> events) {
this.events = events;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(final String firstname) {
this.firstname = firstname;
}
public String getSurname() {
return surname;
}
public void setSurname(final String surname) {
this.surname = surname;
}
public String getChipnumber() {
return chipnumber;
}
public void setChipnumber(final String chipnumber) {
this.chipnumber = chipnumber;
}
@Override
public boolean equals(final Object o) {
if (o instanceof Participant) {
final Participant rant = (Participant) o;
return rant.pk_part.equals(this.pk_part);
}
else
return false;
}
@Override
public int hashCode() {
return super.hashCode();
}
@Override
public String toString() {
return "[" + pk_part + ", " + firstname + ", " + surname + ", " + chipnumber + ", Contact: " + contact +
", Paid: " + paid + ", Club: " + club + ", Gender " + gender + ", Address: " + address +
", Event: " + events + ", Result: " + result + "]";
}
}