2

i have generated texo model from EMF.

Following is the code

 try{

             Session session = factory.openSession();
              Transaction tx = null;
              Integer employeeID = null;
              try{
                 tx = session.beginTransaction();
                 Country country = new Country();
                 country.setCode("PK");;
                 country.setCountry("PAKISTAN");
                 System.out.println((Integer) session.save(country));
                 //^ HERE THE ERROR COMES

                 tx.commit();
              }catch (HibernateException e) {
                 if (tx!=null) tx.rollback();
                 e.printStackTrace(); 
              }finally {
                 session.close(); 
              }

          }catch (Throwable ex) { 
             System.err.println("Failed to create sessionFactory object." + ex);
             throw new ExceptionInInitializerError(ex); 
          }

When i try to add country object with or without locations, I get the error

Failed to create sessionFactory object.java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.Set

The model is generated by Texo have List and simple getter and setter generated.

I have checked this link. but i dont find any answer.

COUNTRY.java

import java.util.ArrayList;
import java.util.List;
public class Country {
    private int iD = 0;
    private String country = null;
    private String code = null;
    private List<Location> locations = new ArrayList<Location>();
    public int getID() {
        return iD;
    }
    public void setID(int newID) {
        iD = newID;
    }    
    public String getCountry() {
        return country;
    }    
    public void setCountry(String newCountry) {
        country = newCountry;
    }       
    public String getCode() {
        return code;
    }    
    public void setCode(String newCode) {
        code = newCode;
    }       
    public List<Location> getLocations() {
        return locations;
    }   
    public void setLocations(List<Location> newLocations) {
        locations = newLocations;
    }
    @Override
    public String toString() {
        return "Country " + " [iD: " + getID() + "]" + " [country: "
                + getCountry() + "]" + " [code: " + getCode() + "]";
    }
}
Community
  • 1
  • 1
Shan Khan
  • 9,667
  • 17
  • 61
  • 111

2 Answers2

2

As discussed in Texo , I have to generate SET instead of LIST in java entities in order to work with Hibernate.

So i had to configure the TEXO to do this for all entities.

  1. Generate the Annotation Model.

  2. Find the entity ( locations ) and add new annotation. goto its properties and set USE LIST = FALSE

  3. Generate the texo models and all the required entities will be change from List to Set

enter image description here

Shan Khan
  • 9,667
  • 17
  • 61
  • 111
-1

please try changing Set<Location> sLoc = new HashSet<Location>(locations); to List<Location> sLoc = new ArrayList<Location>(locations);. U have ur locations as array and sLoc as Hashset so it is giving casting exception.. Hope this solves your problem

NewBee Developer
  • 432
  • 2
  • 9
  • 26
  • I have tried changing it. but there is still exception. java.util.ArrayList cannot be cast to java.util.Set – Shan Khan Nov 17 '15 at 10:02
  • Why do you want your results in set? Sets are used when we need single instance of object, but in list there are multiple instances. Can you try removing `Set sLoc = new HashSet(locations);` – NewBee Developer Nov 17 '15 at 10:34
  • i have checked by removing a line. Also i have tried removing the location I came to know even i add single country object without any location i get the same error – Shan Khan Nov 17 '15 at 11:31
  • @NewBeeDeveloper Sets are useful when you dont want duplicates in your list – bruno_cw Nov 17 '15 at 13:24
  • @NewBeeDeveloper i have posted all the required details like country class and stack trace. – Shan Khan Nov 17 '15 at 13:46
  • @ShanKhan after removing the HashSet, did you perform a rebuild? – bruno_cw Nov 17 '15 at 14:10
  • @bruno_cw I have checked and find out. When i wrote private Set locations = new HashSet(0); Instead of private List locations = new ArrayList(); It works but can you tell me how to generate set type instead of list in texo model generation – Shan Khan Nov 17 '15 at 14:14
  • In your `hibernate.cfg.xml` do you have `locations` mapped with `` ? – bruno_cw Nov 17 '15 at 14:29
  • @bruno_cw i have posted an answer rather then using list in texo entities i have generated SET to make it work with hibernate. Thank you for your time and help – Shan Khan Nov 17 '15 at 14:32
  • @bruno_cw sure i have to wait for 2 days ;) to accept – Shan Khan Nov 17 '15 at 14:34