java - ArrayList 不能使用 Texo 生成的模型设置为 hibernate 状态

标签 java hibernate arraylist emf ecore

我已经从 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); 
          }

当我尝试添加带或不带位置的国家/地区对象时,出现错误

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

该模型由 Texo 生成,并生成了 List 和简单的 getter 和 setter。

我检查过这个link.但我找不到任何答案。

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

最佳答案

Texo 中所述,我必须在 Java 实体中生成 SET 而不是 LIST 才能使用 Hibernate。

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

  1. 生成注释模型。

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

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

enter image description here

关于java - ArrayList 不能使用 Texo 生成的模型设置为 hibernate 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33753653/

相关文章:

spring - session.isOpen() 与 session.isConnected()

java - 仅打印以 pre Java 开头的单词

java - 通过 SSH Hibernate 远程 MySQL 数据库

sql - 性能问题 : difference between select s. * 与 select *

java - 在Java中,数组相对于Arraylist有什么优势?

在 Eclipse 3.x 中调试时,Java API 类不显示变量值

java - 将数组列表转为数组

Java:根据模式提取数组列表的子列表

java - 无法从错误公式单元格获取文本值

java - 如何让 Spring 使用 WildFly 而不是 Tomcat?