probleme d'association hibernate spring spring mvc
bjr
je suis entrain de develloper un application web jee( hibernate, spring, spring mvc)
j'ai rencontré un probleme consacratnt l'association:
j'ai 2 table evennement et place
il ya une relation ManytoOne, j'ai dificulté d'associer le place a l'evennement
voila mon code
event.java
package levelup.world.domain;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="EVENT")
public class Event {
@Id
@GeneratedValue
@Column(name="ID")
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "description")
private String description;
@Column(name = "path")
private String path;
@Column(name = "date")
private Date date;
@Column(name = "type")
private String type;
//many to one Event * ----- 1 Place
@ManyToOne(cascade=CascadeType.ALL)
private Place place1;
public Event() {
super();
// TODO Auto-generated constructor stub
}
public Event(Integer id, String name, String description, String path,
Date date, String type) {
super();
this.id = id;
this.name = name;
this.description = description;
this.path = path;
this.date = date;
this.type = type;
}
public boolean isNew() {
return id==null;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Place getPlace1() {
return place1;
}
public void setPlace1(Place place1) {
this.place1 = place1;
}
}
------------------------------------------------------
place.java
package levelup.world.domain;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="PLACE")
public class Place {
@Id
@GeneratedValue
@Column(name="ID")
private Integer id;
@Column(name = "adress")
private String adress;
@Column(name = "city")
private String city;
@Column(name = "longitude")
private double longitude;
@Column(name = "latitude")
private double latitude;
// @one to many Place 1 -------- * seat
@OneToMany(mappedBy="place", cascade=CascadeType.ALL) // pointing Person's address field
private List<Seat> seats;
// @one to many Place 1 -------- * event
@OneToMany(mappedBy="place1", cascade=CascadeType.ALL) // pointing Person's address field
private List<Event> events;
public Place() {
super();
// TODO Auto-generated constructor stub
}
public Place(Integer id, String adress, String city, float longitude,
float latitude) {
super();
this.id = id;
this.adress = adress;
this.city = city;
this.longitude = longitude;
this.latitude = latitude;
}
public boolean isNew() {
return id==null;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public List<Seat> getSeats() {
return seats;
}
public void setSeats(List<Seat> seats) {
this.seats = seats;
}
public List<Event> getEvents() {
return events;
}
public void setEvents(List<Event> events) {
this.events = events;
}
}
-----------------------------------------------------------------
formControleurevent.java
package levelup.world.web;
import levelup.world.domain.Event;
import levelup.world.domain.Place;
import levelup.world.domain.service.IEventService;
import levelup.world.domain.service.IPlaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
@Controller
@RequestMapping("/EventForm.html")
@SessionAttributes("ahmed")
public class EventForm {
@Autowired
private IEventService eventService;
private Place place;
@InitBinder
public void initBinder(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields(new String[] {"id"});
dataBinder.setRequiredFields(new String[] {"name", "description"});
dataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(false));
}
@RequestMapping(method = RequestMethod.GET)
public Event setUpForm(@RequestParam(value="id", required = false) Integer eventId) {
if (eventId == null) {
return new Event();
} else {
return eventService.getEventById(eventId);
}
}
@RequestMapping(params = "create", method = RequestMethod.POST)
public String create(Event event, BindingResult result) {
return update(event, result);
}
@RequestMapping(params = "update", method = RequestMethod.POST)
public String update(Event event,
BindingResult result) {
//customerValidator.validate(customer, result);
if (result.hasErrors()) {
return "eventForm";
} else {
eventService.saveEvent(event);
return "redirectlaceForm.html";
}
}
@RequestMapping(params = "delete", method = RequestMethod.POST)
public String delete(Event event, BindingResult result, SessionStatus status) {
eventService.deleteEvent(event);
System.out.println("ahmed11: "+status);
status.setComplete();
return "redirect:GererClient.html";
}
}
------------------------------------------
placeformcontroleur
package levelup.world.web;
import java.util.List;
import levelup.world.dao.hibernate.EventDao;
import levelup.world.domain.Event;
import levelup.world.domain.Place;
import levelup.world.domain.service.IEventService;
import levelup.world.domain.service.IPlaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
@Controller
@RequestMapping("/PlaceForm.html")
@SessionAttributes("ahmed")
public class PlaceForm {
@Autowired
private IPlaceService placeService;
@Autowired
private IEventService eventService;
private Event event;
private Place place;
@InitBinder
public void initBinder(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields(new String[] {"id"});
dataBinder.setRequiredFields(new String[] {"adress", "longitude"});
dataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(false));
}
@RequestMapping(method = RequestMethod.GET)
public Place setUpForm(@RequestParam(value="id", required = false) Integer placeId) {
if (placeId == null) {
return new Place();
} else {
return placeService.getPlaceById(placeId);
}
}
@RequestMapping(params = "create", method = RequestMethod.POST)
public String create(Place place, BindingResult result, SessionStatus status) {
return update(place, result, status);
}
@RequestMapping(params = "update", method = RequestMethod.POST)
public String update(Place place,
BindingResult result,
SessionStatus status) {
//customerValidator.validate(customer, result);
if (result.hasErrors()) {
return "placeForm";
} else {
//comment recuperer l'evenement que j'ai déja crée pour faire event.setPlace(place)
placeService.savePlace(place);
placeService.getPlaceById(place.getId());
event.setPlace1(place);
System.out.println("ahmed 2: "+status);
status.setComplete();
return "redirect:GererClient.html";
}
}
@RequestMapping(params = "delete", method = RequestMethod.POST)
public String delete(Place place, BindingResult result, SessionStatus status) {
placeService.deletePlace(place);
status.setComplete();
return "redirect:GererClient.html";
}
}
----------------------------------------------------------------