我已经从 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。
我已经检查了这个链接。但我没有找到任何答案。
国家.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() + "]";
}
}