我已经从 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 为所有实体执行此操作。
生成注释模型。
找到实体(位置)并添加新注释。转到其属性并设置 USE LIST = FALSE
生成 texo 模型,所有必需的实体将从 List 更改为 Set
关于java - ArrayList 不能使用 Texo 生成的模型设置为 hibernate 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33753653/