Bonjour,
En fait mon problème est simple mais je n'arrive pas à le mettre en place.
En fait j'ai des evenements stockés en base de données avec une colonne client qui contient l'id du client concerné correspondant à la table clients.
J'ai une clé étrangère encore la colonne client de la table [events] et id_client de la table [clients]
Et en fait, j'aimerai simplement récupérer un objet client correspondant à l'événement concerné.
Voici mes classes
Code:
@Entity
@Table(name = "events", catalog = "calendar")
public class Events implements java.io.Serializable {
private Integer idEvent;
private int user;
private String nameEvent;
private String description;
private String location;
private Date beginDate;
private Date endDate;
private int client;
private String project;
public Events() {
}
public Events(int user, String nameEvent, String description, String location, Date beginDate, Date endDate, int client, String project) {
this.user = user;
this.nameEvent = nameEvent;
this.description = description;
this.location = location;
this.beginDate = beginDate;
this.endDate = endDate;
this.client = client;
this.project = project;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id_event", unique = true, nullable = false)
public Integer getIdEvent() {
return this.idEvent;
}
public void setIdEvent(Integer idEvent) {
this.idEvent = idEvent;
}
@Column(name = "user", nullable = false)
public int getUser() {
return this.user;
}
public void setUser(int user) {
this.user = user;
}
@Column(name = "name_event", nullable = false, length = 25)
public String getNameEvent() {
return this.nameEvent;
}
public void setNameEvent(String nameEvent) {
this.nameEvent = nameEvent;
}
@Column(name = "description", nullable = false, length = 50)
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
@Column(name = "location", nullable = false, length = 25)
public String getLocation() {
return this.location;
}
public void setLocation(String location) {
this.location = location;
}
@Temporal(TemporalType.DATE)
@Column(name = "begin_date", nullable = false, length = 10)
public Date getBeginDate() {
return this.beginDate;
}
public void setBeginDate(Date beginDate) {
this.beginDate = beginDate;
}
@Temporal(TemporalType.DATE)
@Column(name = "end_date", nullable = false, length = 10)
public Date getEndDate() {
return this.endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
@Column(name = "client", nullable = false)
public int getClient() {
return this.client;
}
public void setClient(int client) {
this.client = client;
}
@Column(name = "project", nullable = false, length = 25)
public String getProject() {
return this.project;
}
public void setProject(String project) {
this.project = project;
}
son fichier de mapping :
Code:
<hibernate-mapping>
<class catalog="calendar" name="entites.Events" table="events">
<id name="idEvent" type="java.lang.Integer">
<column name="id_event"/>
<generator class="identity"/>
</id>
<property name="user" type="int">
<column name="user" not-null="true"/>
</property>
<property name="nameEvent" type="string">
<column length="25" name="name_event" not-null="true"/>
</property>
<property name="description" type="string">
<column length="50" name="description" not-null="true"/>
</property>
<property name="location" type="string">
<column length="25" name="location" not-null="true"/>
</property>
<property name="beginDate" type="date">
<column length="10" name="begin_date" not-null="true"/>
</property>
<property name="endDate" type="date">
<column length="10" name="end_date" not-null="true"/>
</property>
<property name="client" type="int">
<column name="client" not-null="true"/>
</property>
<property name="project" type="string">
<column length="25" name="project" not-null="true"/>
</property>
</class>
</hibernate-mapping>
Et mon bean client :
Code:
@Entity
@Table(name = "clients", catalog = "calendar")
public class Clients implements java.io.Serializable {
private Integer idClient;
private String nameClient;
private String address;
private String phone;
private String contact;
private String color;
public Clients() {
}
public Clients(String nameClient, String address, String phone, String contact, String color) {
this.nameClient = nameClient;
this.address = address;
this.phone = phone;
this.contact = contact;
this.color = color;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id_client", unique = true, nullable = false)
public Integer getIdClient() {
return this.idClient;
}
public void setIdClient(Integer idClient) {
this.idClient = idClient;
}
@Column(name = "name_client", nullable = false, length = 25)
public String getNameClient() {
return this.nameClient;
}
public void setNameClient(String nameClient) {
this.nameClient = nameClient;
}
@Column(name = "address", nullable = false, length = 100)
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
@Column(name = "phone", nullable = false, length = 15)
public String getPhone() {
return this.phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Column(name = "contact", nullable = false, length = 25)
public String getContact() {
return this.contact;
}
public void setContact(String contact) {
this.contact = contact;
}
@Column(name = "color", nullable = false, length = 10)
public String getColor() {
return this.color;
}
public void setColor(String color) {
this.color = color;
}
et son fichier de mapping :
Code:
<hibernate-mapping>
<class catalog="calendar" name="entites.Clients" table="clients">
<id name="idClient" type="java.lang.Integer">
<column name="id_client"/>
<generator class="identity"/>
</id>
<property name="nameClient" type="string">
<column length="25" name="name_client" not-null="true"/>
</property>
<property name="address" type="string">
<column length="100" name="address" not-null="true"/>
</property>
<property name="phone" type="string">
<column length="15" name="phone" not-null="true"/>
</property>
<property name="contact" type="string">
<column length="25" name="contact" not-null="true"/>
</property>
<property name="color" type="string">
<column length="10" name="color" not-null="true"/>
</property>
</class>
</hibernate-mapping>
J'ai cherché et j'ai trouvé des info sur @OneToOne et @ManyToOne mais je n'arrive pas à l'appliquer...
J'ai deux clefs étrangères qui relie un id utilisateur d'un evenement à l'id de la table evenement et une autre qui lit un client a son id.
Merci de votre aide.