【问题标题】:ArrayList cannot be case to Set In hibernate using Texo generated ModelsArrayList 不能使用 Texo 生成的模型在休眠中设置
【发布时间】:2015-11-17 09:45:41
【问题描述】:

我已经从 EMF 生成了 texo 模型。

下面是代码

 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); 
          }

当我尝试添加带或不带位置的国家对象时,我收到错误

无法创建 sessionFactory object.java.lang.ClassCastException:java.util.ArrayList 无法转换为 java.util.Set

Texo 生成的模型有 List 和简单的 getter 和 setter 生成。

我已经检查了这个link.,但我没有找到任何答案。

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() + "]";
    }
}

【问题讨论】:

  • 你能发布完整的堆栈跟踪吗?你的国家豆呢?
  • @bruno_cw 你能检查一下我已经发布了详细信息吗。

标签: java hibernate arraylist emf ecore


【解决方案1】:

正如Texo 中所讨论的,我必须在 java 实体中生成 SET 而不是 LIST 才能使用 Hibernate。

所以我必须配置 TEXO 来为所有实体执行此操作。

  1. 生成注释模型。

  2. 找到实体(位置)并添加新注释。转到其属性并设置 USE LIST = FALSE

  3. 生成 texo 模型,所有需要的实体将从 List 更改为 Set

【讨论】:

    【解决方案2】:

    请尝试将Set&lt;Location&gt; sLoc = new HashSet&lt;Location&gt;(locations); 更改为List&lt;Location&gt; sLoc = new ArrayList&lt;Location&gt;(locations);。你有你的locations作为数组和sLoc作为Hashset,所以它给出了强制转换异常.. 希望这能解决你的问题

    【讨论】:

    • 我已尝试更改它。但仍有例外。 java.util.ArrayList 不能强制转换为 java.util.Set
    • 为什么要设置结果?当我们需要对象的单个实例时使用集合,但在列表中有多个实例。你可以尝试删除Set&lt;Location&gt; sLoc = new HashSet&lt;Location&gt;(locations);
    • 我已经通过删除一行进行了检查。我也尝试过删除我知道的位置,即使我添加了单个国家对象而没有任何位置我得到同样的错误
    • @NewBeeDeveloper 集在您不希望列表中有重复项时很有用
    • @NewBeeDeveloper 我已经发布了所有必需的详细信息,例如国家/地区类和堆栈跟踪。
    猜你喜欢
    • 2014-03-17
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-19
    • 2014-06-19
    • 1970-01-01
    • 2012-10-29
    相关资源
    最近更新 更多