I currently started to learn objectDB and have some questions but before I would like to share my code. I develop a hotel system using Spring and ObjectDB. I have two classes Guest and Hotel they are located in the model directory. I also have the controller IndexController which is located in the controller directory and two dao classes GuestDAO and HotelDAO.
This is Guest.java:
@Entity
public class Guest implements Serializable {
private static final long serialVersionUID = 1L;
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id;
private String FirstName;
private String LastName;
private Date BirthDate;
private String Email;
private String Country;
private String Address;
private Integer ZipCode;
private String PhoneNumber;
private String gender;
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="HOTEL_ID", nullable=false)
Hotel hotel;
public Guest() {}
public Guest(String FirstName, String LastName, LocalDate BirthDate, String Email, String Country, String Address, Integer ZipCode, String PhoneNumber, String gender, Hotel hotel) {
this.FirstName = FirstName;
this.LastName = LastName;
this.BirthDate = Date.from(BirthDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
this.Email = Email;
this.Country = Country;
this.Address = Address;
this.ZipCode = ZipCode;
this.PhoneNumber = PhoneNumber;
this.gender = gender;
this.hotel = hotel;
}
public void setId(Long id) {
this.id = id;
}
public void setFirstName(String firstName) {
FirstName = firstName;
}
public void setLastName(String lastName) {
LastName = lastName;
}
public void setBirthDate(Date birthDate) {
BirthDate = birthDate;
}
public void setEmail(String email) {
Email = email;
}
public void setCountry(String country) {
Country = country;
}
public void setAddress(String address) {
Address = address;
}
public void setZipCode(Integer zipCode) {
ZipCode = zipCode;
}
public void setPhoneNumber(String phoneNumber) {
PhoneNumber = phoneNumber;
}
public void setGender(String gender) {
this.gender = gender;
}
public Long getId() {
return id;
}
public String getFirstName() {
return FirstName;
}
public String getLastName() {
return LastName;
}
public Date getBirthDate() {
return BirthDate;
}
public String getEmail() {
return Email;
}
public String getCountry() {
return Country;
}
public String getAddress() {
return Address;
}
public Integer getZipCode() {
return ZipCode;
}
public String getPhoneNumber() {
return PhoneNumber;
}
public String getGender() {
return gender;
}
public void addHotel(Hotel hotel) {
this.hotel = hotel;
}
public Hotel getHotel() {
return hotel;
}
@Override
public String toString() {
return "{ " id " " FirstName " " LastName " " BirthDate " " Email " " Country
" " Address " " ZipCode " " PhoneNumber " " gender " };";
}
}
This is class Hotel:
@Entity
public class Hotel implements Serializable {
private static final long serialVersionUID = 1L;
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id;
private String name;
@OneToMany(cascade=CascadeType.REMOVE, mappedBy="hotel", fetch=FetchType.EAGER)
Set<Guest> guests;
public Hotel() {}
public Hotel(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setId(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public void addGuest(Guest guest) {
guests.add(guest);
}
public Set<Guest> getGuests() {
return guests;
}
@Override
public String toString() {
return "{ name = " name ", " "id = " id " };";
}
}
This is my GuestDAO
public class GuestDAO {
@Override
public void insert(Guest guest) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("$objectdb/db/test.odb");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(guest);
em.getTransaction().commit();
TypedQuery<Guest> query = em.createQuery("SELECT b FROM Guest b", Guest.class);
List<Guest> results = query.getResultList();
for (Guest bb : results) {
System.out.println(bb);
}
em.close();
emf.close();
}
@Override
public List<String> search(String name) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("$objectdb/db/test.odb");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
TypedQuery<Guest> query = em.createQuery("SELECT FROM Guest g WHERE g.FirstName = :FirstName ", Guest.class);
List res = query.setParameter("FirstName", name).getResultList();
em.getTransaction().commit();
em.close();
emf.close();
return res;
}
@Override
public void delete(String name) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("$objectdb/db/test.odb");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
TypedQuery<Guest> query = em.createQuery("DELETE FROM Guest g WHERE g.FirstName = :FirstName", Guest.class);
query.setParameter("FirstName", name).executeUpdate();
em.getTransaction().commit();
em.close();
emf.close();
}
@Override
public List<Guest> showAllGuests() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("$objectdb/db/test.odb");
EntityManager em = emf.createEntityManager();
TypedQuery<Guest> query = em.createQuery("SELECT g FROM Guest g ORDER BY g.id", Guest.class);
return query.getResultList();
}
@Override
public void updateByName(String name) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("$objectdb/db/test.odb");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Guest updateGuest = em.find(Guest.class, name);
if(updateGuest != null) {
updateGuest.setFirstName(name);
}
em.getTransaction().commit();
em.close();
emf.close();
}
}
When I add a new user (guest) to the system, then everything works, in the table a new guest appears. But when I try to add a new hotel to the Hotel table, it is added to the Guests' table and not to the Hotel table. I would like to know why the Hotel table is always empty but the Guest table is always added new queries. Also to join them don't work as well because one of the tables is empty.
This is my controller
@Controller
public class IndexController {
//private GuestDAO dao;
// Guest Form
@GetMapping("/")
public String index() {
return "index";
}
@RequestMapping(value="/guestform",method = RequestMethod.GET)
public ModelAndView showform(ModelAndView m) {
Guest guest = new Guest();
m.addObject("register", guest);
m.setViewName("guestform");
return m;
}
@RequestMapping(value="/save",method = RequestMethod.POST)
public String save(@ModelAttribute("register") Guest guest) {
System.out.println(guest);
GuestDAO dao = new GuestDAO();
dao.insert(guest);
return "redirect:/";
//return "redirect:/viewemp";//will redirect to viewemp request mapping
}
@RequestMapping(value="/deleteguestform",method = RequestMethod.GET)
public ModelAndView showform_guest(ModelAndView m) {
Guest guest = new Guest();
m.addObject("deleteguest", guest);
m.setViewName("deleteguestform");
return m;
}
@RequestMapping(value="/delete",method = RequestMethod.POST)
public String delete(@ModelAttribute("deleteguest") Guest guest) {
GuestDAO dao = new GuestDAO();
System.out.println(guest.getFirstName());
dao.delete(guest.getFirstName());
return "redirect:/";
}
@RequestMapping(value="/editguestform",method = RequestMethod.GET)
public ModelAndView edit_form(ModelAndView m) {
Guest guest = new Guest();
m.addObject("editform", guest);
m.setViewName("editguestform");
return m;
}
@RequestMapping(value="/edit",method = RequestMethod.POST)
public String edit(@ModelAttribute("editform") Guest guest) {
GuestDAO dao = new GuestDAO();
System.out.println(guest.getFirstName());
dao.updateByName(guest.getFirstName());
return "redirect:/";
}
@RequestMapping(value="/showguestform", method = RequestMethod.GET)
public ModelAndView listGuests(ModelAndView m) {
GuestDAO dao = new GuestDAO();
List<Guest> listGuest = dao.showAllGuests();
for (int i = 0; i < listGuest.size(); i ) {
System.out.println(listGuest.get(i).getFirstName());
}
m.addObject("listGuest", listGuest);
return m;
}
@RequestMapping(value = "/searchguestform", method = RequestMethod.GET)
public ModelAndView search_form(ModelAndView m) {
Guest guest = new Guest();
m.addObject("searchguest", guest);
m.setViewName("searchguestform");
return m;
}
@RequestMapping(value="/search",method = RequestMethod.POST)
public ModelAndView search(@ModelAttribute("searchguest") Guest guest) {
GuestDAO dao = new GuestDAO();
System.out.println(guest.getFirstName());
List<String> listGuest = dao.search(guest.getFirstName());
ModelAndView m = new ModelAndView("showguestform");
m.addObject("listGuest", listGuest);
return m;
}
// Hotel Form
@RequestMapping(value="/hotelform",method = RequestMethod.GET)
public ModelAndView hotel_form(ModelAndView m) {
Hotel hotel = new Hotel();
m.addObject("hotelregistration", hotel);
m.setViewName("hotelform");
return m;
}
@RequestMapping(value="/hoteladd",method = RequestMethod.POST)
public String hotel_add(@ModelAttribute("hotelregistration") Hotel hotel) {
System.out.println(hotel);
HotelDAO hdao = new HotelDAO();
hdao.insert_hotel(hotel);
return "redirect:/";
}
@RequestMapping(value="/showhotelform", method = RequestMethod.GET)
public ModelAndView listHotels(ModelAndView m) {
HotelDAO dao = new HotelDAO();
List<Hotel> listHotel = dao.showAllHotels();
for (int i = 0; i < listHotel.size(); i ) {
System.out.println("->_" listHotel.get(i).getName());
}
m.addObject("listHotel", listHotel);
return m;
}
}
And HotelDAO
public class HotelDAO {
public void insert_hotel(Hotel hotel) {
System.out.println("Start");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("$objectdb/db/test.odb");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(hotel);
em.getTransaction().commit();
System.out.println("Commit");
TypedQuery<Hotel> query = em.createQuery("SELECT h FROM Hotel h", Hotel.class);
List<Hotel> results = query.getResultList();
System.out.println("Print");
for (Hotel bb : results) {
System.out.println(bb);
}
em.close();
emf.close();
}
public List<Hotel> showAllHotels() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("$objectdb/db/test.odb");
EntityManager em = emf.createEntityManager();
TypedQuery<Hotel> query = em.createQuery("SELECT g FROM Hotel g ORDER BY g.id", Hotel.class);
return query.getResultList();
}
public void assignGuestToHotel(String guestId, String hotelId) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("$objectdb/db/test.odb");
EntityManager em = emf.createEntityManager();
Guest guest = em.find(Guest.class, Long.valueOf(guestId));
Hotel hotel = em.find(Hotel.class, Long.valueOf(hotelId));
if (guest != null && hotel != null) {
hotel.addGuest(guest);
guest.addHotel(hotel);
} else {
System.out.println("Guest or hotel not found.");
}
}
}
So again, could you explain the problem why the Hotel table is always empty but the Guest table always works even if I try to add a new hotel to the Hotel table and the joining that two tables don't work as well?
Thanks.
CodePudding user response:
Try with removing cascade=CascadeType.REMOVE
and fetch=FetchType.EAGER
from Hotel
class like this
@OneToMany(mappedBy="hotel")
Set<Guest> guests;
CodePudding user response:
I found the problem, I wrote incorrectly my jsp file. Thanks, everybody for help.