title: I need to cast an object to string then retrieve it by casting it back to my object
body: <p>I need to learn how to create my custom .tostring and .fromstring</p>
  
  <p>I have tried casting it back to the object but it does not seem to be working
  I have also tried to retrieve the object and converting it to string then comparing the strings but it still does not work</p>
  
  <p>the issue began here so the .toString() was trying to bypass the issue</p>
  
  <p>I could only save the elements id but it would be hectic during retrieval while dealing with three different tables at the same time</p>
  
  <p>I would like a single persist to save the all the objects which I can achieve through that but would have to create three queries during retrieval unlike what I am currently doing with the Event entity</p>
  
  <p><a href="https://stackoverflow.com/questions/57165768/detached-entity-passed-to-persist-entities-event">detached entity passed to persist: entities.Event</a></p>
  
  <pre class="lang-java prettyprint-override"><code>package entities;
  // Generated Jul 23, 2019 11:11:55 AM by Hibernate Tools 4.3.1
  
  import com.fasterxml.jackson.core.JsonProcessingException;
  import com.fasterxml.jackson.databind.ObjectMapper;
  import java.io.IOException;
  
  
  
  
  /**
   * Event generated by hbm2java
   */
  public class Event  implements java.io.Serializable {
  
  
       private Integer id;
       private String image;
       private String location;
       private int upto100;
       private Integer upto300;
       private Integer upto500;
       private Integer upto1000;
       private int phoneno;
       private String scenario;
       private String name;
       private String mail;
       private Users owner;
       private String description;
  
      public Event() {
      }
  
  
      public Event(String image, String location, int upto100, int phoneno, String scenario, String name, String mail) {
          this.image = image;
          this.location = location;
          this.upto100 = upto100;
          this.phoneno = phoneno;
          this.scenario = scenario;
          this.name = name;
          this.mail = mail;
      }
      public Event(String image, String location, int upto100, Integer upto300, Integer upto500, Integer upto1000, int phoneno, String scenario, String name, String mail, Users owner, String description) {
         this.image = image;
         this.location = location;
         this.upto100 = upto100;
         this.upto300 = upto300;
         this.upto500 = upto500;
         this.upto1000 = upto1000;
         this.phoneno = phoneno;
         this.scenario = scenario;
         this.name = name;
         this.mail = mail;
         this.owner = owner;
         this.description = description;
      }
  
      public Integer getId() {
          return this.id;
      }
  
      public void setId(Integer id) {
          this.id = id;
      }
      public String getImage() {
          return this.image;
      }
  
      public void setImage(String image) {
          this.image = image;
      }
      public String getLocation() {
          return this.location;
      }
  
      public void setLocation(String location) {
          this.location = location;
      }
      public int getUpto100() {
          return this.upto100;
      }
  
      public void setUpto100(int upto100) {
          this.upto100 = upto100;
      }
      public Integer getUpto300() {
          return this.upto300;
      }
  
      public void setUpto300(Integer upto300) {
          this.upto300 = upto300;
      }
      public Integer getUpto500() {
          return this.upto500;
      }
  
      public void setUpto500(Integer upto500) {
          this.upto500 = upto500;
      }
      public Integer getUpto1000() {
          return this.upto1000;
      }
  
      public void setUpto1000(Integer upto1000) {
          this.upto1000 = upto1000;
      }
      public int getPhoneno() {
          return this.phoneno;
      }
  
      public void setPhoneno(int phoneno) {
          this.phoneno = phoneno;
      }
      public String getScenario() {
          return this.scenario;
      }
  
      public void setScenario(String scenario) {
          this.scenario = scenario;
      }
      public String getName() {
          return this.name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
      public String getMail() {
          return this.mail;
      }
  
      public void setMail(String mail) {
          this.mail = mail;
      }
      public Users getOwner() {
          return this.owner;
      }
  
      public void setOwner(Users owner) {
          this.owner = owner;
      }
      public String getDescription() {
          return this.description;
      }
  
      public void setDescription(String description) {
          this.description = description;
      }
  
      /*public String toStreng(Event event) throws JsonProcessingException {
          return new ObjectMapper().writeValueAsString(event);
      }
  
      public Event fromStreng(String string) throws IOException {
          return new ObjectMapper().readValue(string, Event.class);
      }*/
  
  
  }
  
  
  
  </code></pre>
  
  <p>this is the class I need to overide the .tostring and .fromstring with
  the page below is where I had to convert the object to string</p>
  
  <pre><code>&lt;%-- 
      Document   : Book
      Created on : Jul 21, 2019, 8:27:27 PM
      Author     : Tariana
  --%&gt;
  
  &lt;%@page import="java.util.Iterator"%&gt;
  &lt;%@page import="java.util.List"%&gt;
  &lt;%@page import="org.hibernate.service.ServiceRegistry"%&gt;
  &lt;%@page import="org.hibernate.cfg.Configuration"%&gt;
  &lt;%@page import="org.hibernate.service.ServiceRegistryBuilder"%&gt;
  &lt;%@page import="org.hibernate.Session"%&gt;
  &lt;%@page import="org.hibernate.Query"%&gt;
  &lt;%@page import="org.hibernate.SessionFactory"%&gt;
  &lt;%@page import="entities.*"%&gt;
  &lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
  &lt;!DOCTYPE html&gt;
  &lt;%
      String eventid = (String) session.getAttribute("venueid");
      Users owner = new Users();
      int id = Integer.parseInt(eventid);
  
      SessionFactory sessionFactory;
      ServiceRegistry serviceRegistry;
  
      Configuration configuration = new Configuration();
      configuration.addAnnotatedClass(Users.class)
          .configure();
  
      serviceRegistry = new ServiceRegistryBuilder()
              .applySettings(configuration.getProperties())
              .configure("hibernate.cfg.xml")
              .build();        
      sessionFactory = configuration.buildSessionFactory(serviceRegistry);
  
      Session sess = sessionFactory.openSession();
      String hql = "SELECT e FROM Event e WHERE e.id = :id";
      Query query = sess.createQuery(hql);
      query.setInteger("id", id);
      List&lt;Event&gt; products;
      List results = query.list();
      products = results;
  
      Users own = null;
      Event venues = null;
      String upto;
  
      Iterator&lt;Event&gt; itr = products.iterator();
      while(itr.hasNext()){
          Event g = itr.next();
          System.out.println(g.getUpto100());
          owner = g.getOwner();
          own = owner;
          venues = g;
      }
      System.out.println(own.getUsername());
      String owns = (String) own.getUsername();
      sess.close();
      if(venues.getUpto300() != null){
          upto = "upto300";
      }else if(venues.getUpto500() != null){
          upto = "upto500";
      }else if(venues.getUpto1000() != null){
          upto = "upto1000";
      }else{
          upto = "upto100";
      }
      session.setAttribute("owner", owner);
      session.setAttribute("venue", venues);
  %&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;style&gt;
              .navy{
                  float: right;
              }
          &lt;/style&gt;
          &lt;%@include file = "BookHeader.jsp" %&gt;
          &lt;title&gt;Book&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;div id="lookbookHeader"&gt;
          &lt;video autoplay poster="data:image/png;base64, &lt;%=venues.getImage()%&gt;" onerror="this.onerror=null;this.poster='event.png';" id="bgvid"&gt;
  
          &lt;/video&gt;
          &lt;div id="headerContentContainer"&gt;
              &lt;h1 class="whitey"&gt;&lt;%=venues.getName()%&gt;&lt;/h1&gt;
              &lt;div class="initial-arrow-small"&gt;&lt;/div&gt;
          &lt;/div&gt;
          &lt;/div&gt;
          &lt;/div&gt;
          &lt;div id="introContent"&gt;
              &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"&gt;&lt;/script&gt;
              &lt;script src="https://cdn.jsdelivr.net/momentjs/2.14.1/moment.min.js"&gt;&lt;/script&gt;
              &lt;script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"&gt;&lt;/script&gt;
              &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"&gt;&lt;/script&gt;
              &lt;link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.min.css"&gt;
              &lt;link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"&gt;
              &lt;br&gt;
              &lt;h2&gt;Book Venue&lt;/h2&gt;
              &lt;h5 class="lefty"&gt;Email:&lt;/h5&gt; &lt;h5 class="rightey"&gt;&lt;%=venues.getMail()%&gt;&lt;/h5&gt;&lt;br&gt;&lt;br&gt;
              &lt;h5 class="lefty"&gt;Max Capacity:&lt;/h5&gt; &lt;h5 class="rightey"&gt;&lt;%=upto%&gt;&lt;/h5&gt;&lt;br&gt;&lt;br&gt;
              &lt;h5 class="lefty"&gt;Price for up to 100 people:&lt;/h5&gt; &lt;h5 class="rightey"&gt;&lt;%=venues.getUpto100()%&gt;&lt;/h5&gt;&lt;br&gt;&lt;br&gt;
              &lt;h5 class="lefty"&gt;Price for up to 300 people:&lt;/h5&gt; &lt;h5 class="rightey"&gt;&lt;%=venues.getUpto300()%&gt;&lt;/h5&gt;&lt;br&gt;&lt;br&gt;
              &lt;h5 class="lefty"&gt;Price for up to 500 people:&lt;/h5&gt; &lt;h5 class="rightey"&gt;&lt;%=venues.getUpto500() %&gt;&lt;/h5&gt;&lt;br&gt;&lt;br&gt;
              &lt;h5 class="lefty"&gt;Price for up to 1000 people:&lt;/h5&gt; &lt;h5 class="rightey"&gt;&lt;%=venues.getUpto1000()%&gt;&lt;/h5&gt;&lt;br&gt;&lt;br&gt;
              &lt;h5 class="lefty"&gt;Phone no.:&lt;/h5&gt; &lt;h5 class="rightey"&gt;&lt;%=venues.getPhoneno()%&gt;&lt;/h5&gt;&lt;br&gt;&lt;br&gt;
              &lt;h5 class="lefty"&gt;Location:&lt;/h5&gt; &lt;h5 class="rightey"&gt;&lt;%=venues.getLocation()%&gt;&lt;/h5&gt;&lt;br&gt;&lt;br&gt;
              &lt;h5 class="lefty"&gt;Scenario:&lt;/h5&gt; &lt;h5 class="rightey"&gt;&lt;%=venues.getScenario()%&gt;&lt;/h5&gt;&lt;br&gt;&lt;br&gt;
              &lt;h5 class="lefty"&gt;Posted By:&lt;/h5&gt; &lt;h5 class="rightey"&gt;&lt;%=owns%&gt;&lt;/h5&gt;&lt;br&gt;&lt;br&gt;
              &lt;p class="lefty"&gt;Description:&lt;/p&gt; &lt;p class="rightey"&gt;
                  &lt;%=venues.getDescription()%&gt;
              &lt;/p&gt;&lt;br&gt;&lt;br&gt;
              &lt;h4&gt;Set Event Details&lt;/h4&gt;
              &lt;form action="/Eventer/Booking"&gt;
                  &lt;!--&lt;script src="//code.jquery.com/jquery-1.10.2.js"&gt;&lt;/script&gt;
                  &lt;script src="//code.jquery.com/ui/1.11.1/jquery-ui.js"&gt;&lt;/script&gt;
                  &lt;link rel="stylesheet" href="//code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css"/&gt;
                  &lt;script&gt;
                      $( "#from" ).datepicker().datepicker("setDate", "today");
                      $(function() {  
                          $( "#from" ).datepicker({   
                              defaultDate: "+0w",  
                              changeMonth: true,   
                              numberOfMonths: 1,
                              onClose: function( selectedDate ) {  
                                $( "#to" ).datepicker( "option", "minDate", "today" );
                                $( "#to" ).datepicker( "option", "minDate", selectedDate );
                                $( "#to" ).datepicker( "option", "maxDate", selectedDate);
                              }  
                          });  
                          $( "#to" ).datepicker({
                              defaultDate: "today",
                              changeMonth: true,
                              numberOfMonths: 1,
                              onClose: function( selectedDate ) {
                                $( "#from" ).datepicker( "option", "minDate", "today" );
                              }
                          });  
                      });  
                  &lt;/script&gt;
                  &lt;div class="form-group"&gt;
                      &lt;label class="sr-only" for="eventedit"&gt;Start date:&lt;/label&gt;
                      &lt;input type="text" id="from" name="from" &gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      &lt;label class="sr-only" for="eventedit"&gt;End date:&lt;/label&gt;
                      &lt;input type="text" id="to" name = "to"&gt;
                  &lt;/div&gt;--&gt;
                  &lt;div class="form-group"&gt;
                      &lt;label for="Event Name"&gt;Event Name:&lt;/label&gt;
                      &lt;input type="text" class="form-control" name="eventname" id="" aria-describedby="helpId" placeholder="" required&gt;
                      &lt;small id="emailHelpId" class="form-text text-muted"&gt;Enter location of venue&lt;/small&gt;
                  &lt;/div&gt;
                  &lt;div class='input-group date' id='datetimepicker1'&gt;
                      &lt;label for="Event Name"&gt;Set Event Date:&lt;/label&gt;
                      &lt;input name="date" type='text' class="form-control" id="" aria-describedby="helpId" required/&gt;&lt;br&gt;
                      &lt;small id="helpId" class="form-text text-muted"&gt;Enter date of event&lt;/small&gt;
                      &lt;span class="input-group-addon"&gt;
                      &lt;span class="glyphicon glyphicon-calendar"&gt;&lt;/span&gt;
                      &lt;/span&gt;&lt;br&gt;
                  &lt;/div&gt;
                      &lt;script&gt;
                          $(function () {
                              $('#datetimepicker1').datetimepicker();
                              var minDate = $( "#datetimepicker1" ).datepicker( "option", "minDate" );
                              minDate.datepicker( "option", "minDate", "today" );
                          });
                        &lt;/script&gt;
                        &lt;h2&gt;Choose Booking Capacity&lt;/h2&gt;
                  &lt;div class="form-check"&gt;
                      &lt;input type="radio" class="form-check-input" name="capacity" id="" checked value="upto100"&gt;
                      &lt;label class="form-check-label"&gt;
                          &lt;br&gt;   
                          &lt;i class="fa fa-1x" aria-hidden="true"&gt;&lt;h3&gt;Up to 100 people&lt;/h3&gt;&lt;/i&gt;
                      &lt;/label&gt;
                  &lt;/div&gt;
                  &lt;%
                      if(venues.getUpto300() != null){
                          %&gt;
                          &lt;div class="form-check"&gt;
                              &lt;input type="radio" class="form-check-input" name="capacity" id="" value="upto300"&gt;
                              &lt;label class="form-check-label"&gt;
                                  &lt;br&gt;   
                                  &lt;i class="fa fa-1x" aria-hidden="true"&gt;&lt;h3&gt;Up to 300 people&lt;/h3&gt;&lt;/i&gt;
                              &lt;/label&gt;
                          &lt;/div&gt;
                          &lt;%
                      }
                      %&gt;
                  &lt;%
                      if(venues.getUpto500() != null){
                          %&gt;
                          &lt;div class="form-check"&gt;
                              &lt;input type="radio" class="form-check-input" name="capacity" id="" value="upto500"&gt;
                              &lt;label class="form-check-label"&gt;
                                  &lt;br&gt;  
                                  &lt;i class="fa fa-1x" aria-hidden="true"&gt;&lt;h3&gt;Up to 500 people&lt;/h3&gt;&lt;/i&gt;
                              &lt;/label&gt;
                          &lt;/div&gt;
                          &lt;%
                      }
                      %&gt;
                  &lt;%
                      if(venues.getUpto1000() != null){
                          %&gt;
                          &lt;div class="form-check"&gt;
                              &lt;input type="radio" class="form-check-input" name="capacity" id="" value="upto1000"&gt;
                              &lt;label class="form-check-label"&gt;
                                  &lt;br&gt;   
                                  &lt;i class="fa fa-1x" aria-hidden="true"&gt;&lt;h3&gt;Up to 1000 people&lt;/h3&gt;&lt;/i&gt;
                              &lt;/label&gt;
                          &lt;/div&gt;
                          &lt;%
                      }
                      %&gt;
                  &lt;button class="btn-view"&gt;Book Venue&lt;/button&gt;
              &lt;/form&gt;
          &lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  
  </code></pre>
  
  <p>I need to retrieve the items from the database in this page</p>
  
  <pre><code>&lt;%-- 
      Document   : MyEvents
      Created on : Jul 23, 2019, 11:17:46 AM
      Author     : Tariana
  --%&gt;
  
  &lt;%@page import="entities.Event"%&gt;
  &lt;%@page import="java.util.Iterator"%&gt;
  &lt;%@page import="java.util.List"%&gt;
  &lt;%@page import="newpackage.Booking"%&gt;
  &lt;%@page import="org.hibernate.Query"%&gt;
  &lt;%@page import="org.hibernate.Session"%&gt;
  &lt;%@page import="org.hibernate.service.ServiceRegistryBuilder"%&gt;
  &lt;%@page import="entities.Users"%&gt;
  &lt;%@page import="org.hibernate.cfg.Configuration"%&gt;
  &lt;%@page import="org.hibernate.service.ServiceRegistry"%&gt;
  &lt;%@page import="org.hibernate.SessionFactory"%&gt;
  &lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
  &lt;!DOCTYPE html&gt;
  &lt;%
      //checks whether there is a farmer account with a session within the system
      //this header spans over all the pages after the farmer has logged into the system
      if(session.getAttribute("mail") == null){
              out.println("&lt;script type=\"text/javascript\"&gt;");
              out.println("alert('You need to login first!!');");
              out.println("window.location.href = \"/Eventer/Users/Login.jsp\";");
              out.println("&lt;/script&gt;");
      }else{
          %&gt;
          &lt;html&gt;
              &lt;head&gt;
                  &lt;%@include file="BookHeader.jsp" %&gt;
                  &lt;title&gt;My Events&lt;/title&gt;
              &lt;/head&gt;
              &lt;body class="eventer"&gt;
                  &lt;div class="kadi"&gt;
              &lt;%
                  SessionFactory sessionFactory;
                  ServiceRegistry serviceRegistry;
  
                  Configuration configuration = new Configuration();
                  configuration.addAnnotatedClass(Users.class)
                      .configure();
  
                  serviceRegistry = new ServiceRegistryBuilder()
                          .applySettings(configuration.getProperties())
                          .configure("hibernate.cfg.xml")
                          .build();        
                  sessionFactory = configuration.buildSessionFactory(serviceRegistry);
  
                  Session sess = sessionFactory.openSession();
                  String hql = "FROM Booking";
                  Query queryy = sess.createQuery(hql);
                  List&lt;Booking&gt; buying;
                  List results = queryy.list();
                  buying = results;
                  Iterator&lt;Booking&gt; itr = buying.iterator();
                  Users owner;
                  Event venue;
                  Booking event = new Booking();
                  System.out.println();
  
                  while(itr.hasNext()){
                      Booking g = itr.next();
                      event = g;
  
                      %&gt;
  
                          &lt;div class="card"&gt;
                              &lt;img class="card-img-top" src="event.png" alt=""&gt;
                              &lt;div class="card-body"&gt;
                                  &lt;h4 class="card-title"&gt;&lt;%=event.getEventname()%&gt;&lt;/h4&gt;
                                  &lt;p class="card-text"&gt;Date: &lt;%=event.getDate()%&gt;&lt;/p&gt;
                                  &lt;p class="card-text"&gt;Capacity for: &lt;%=event.getCapacity()%&gt;&lt;/p&gt;
                                  &lt;p class="card-text eve"&gt;ksh.&lt;%=event.getPricing()%&gt;&lt;/p&gt;
                                  &lt;form&gt;
                                      &lt;div class="form-group"&gt;
                                          &lt;label class="sr-only" for="inputName"&gt;Hidden input label&lt;/label&gt;
                                          &lt;input type="text" class="form-control" name="inputName" id="inputName" placeholder="" value="" hidden&gt;
                                          &lt;button type="submit" class="btn-view"&gt;View Venue&lt;/button&gt;
                                      &lt;/div&gt;
                                  &lt;/form&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
  
                      &lt;%
                  }
              %&gt;
                  &lt;/div&gt;
              &lt;/body&gt;
          &lt;/html&gt;
          &lt;%
      }
  %&gt;
  
  </code></pre>
  
  <p>booking class</p>
  
  <pre class="lang-java prettyprint-override"><code>package newpackage;
  // Generated Jul 23, 2019 11:11:55 AM by Hibernate Tools 4.3.1
  
  
  
  /**
   * Booking generated by hbm2java
   */
  public class Booking  implements java.io.Serializable {
  
  
       private Integer id;
       private String capacity;
       private String date;
       private String eventname;
       private String venue;
       private String owner;
       private int pricing;
       private String user;
  
      public Booking() {
      }
  
      public Booking(String capacity, String date, String eventname, String venue, String owner, int pricing, String user) {
         this.capacity = capacity;
         this.date = date;
         this.eventname = eventname;
         this.venue = venue;
         this.owner = owner;
         this.pricing = pricing;
         this.user = user;
      }
  
      public Integer getId() {
          return this.id;
      }
  
      public void setId(Integer id) {
          this.id = id;
      }
      public String getCapacity() {
          return this.capacity;
      }
  
      public void setCapacity(String capacity) {
          this.capacity = capacity;
      }
      public String getDate() {
          return this.date;
      }
  
      public void setDate(String date) {
          this.date = date;
      }
      public String getEventname() {
          return this.eventname;
      }
  
      public void setEventname(String eventname) {
          this.eventname = eventname;
      }
      public String getVenue() {
          return this.venue;
      }
  
      public void setVenue(String venue) {
          this.venue = venue;
      }
      public String getOwner() {
          return this.owner;
      }
  
      public void setOwner(String owner) {
          this.owner = owner;
      }
      public int getPricing() {
          return this.pricing;
      }
  
      public void setPricing(int pricing) {
          this.pricing = pricing;
      }
      public String getUser() {
          return this.user;
      }
  
      public void setUser(String user) {
          this.user = user;
      }
  
  
  
  
  }
  
  
  
  </code></pre>
  
  <p>I need to learn a way to compare the objects or changing the object back to it's former object state</p>
  
  <p>I want to try a .tostring method that converts each element to a string but it would raise an issue there I am storing classes for example the owner in the above POJO class which would cause the same issue that I am facing currently</p>