[英]Hibernate Map throws - ORA-01722: invalid number error
I am trying to create a simple program using Map with Hibernate. 我正在尝试使用Map with Hibernate创建一个简单的程序。 I have a Country entity with a Map of State's.
我有一个国家实体,有国家地图。 Here are my classes:
这是我的课程:
@Entity
public class Country implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "id")
@GeneratedValue
private int id;
@Column(name = "name")
private String name;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "country_id")
@MapKeyColumn(name = "id")
private Map<String, State> states;
// setters & getters
}
@Entity
@Table(name = "state")
public class State {
@Id
@Column(name = "id")
@GeneratedValue
private int id;
@Column(name = "name")
private String name;
// setters & getters
}
Here is my program that creates some countries and states: 这是我创建一些国家和州的计划:
public class AppTest {
private static final SessionFactory concreteSessionFactory;
static {
try {
concreteSessionFactory = HibernateUtil.getSessionFactory();
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static Session getSession()
throws HibernateException {
return concreteSessionFactory.openSession();
}
public static void main(String... args){
saveCountries();
showCountries();
}
private static void saveCountries() {
saveCountry("US", "CA", "Texas");
saveCountry("UK", "London", "Cambridge");
}
private static void saveCountry(String countryName, String... states) {
Session session=getSession();
session.beginTransaction();
Country country = new Country();
country.setName(countryName);
Map<String,State> stateMap = new HashMap<String, State>();
int count = 0;
for (String stateName : states) {
State state = new State();
state.setName(stateName);
stateMap.put(stateName+ count++, state);
}
country.setStates(stateMap);
session.save(country);
session.close();
}
private static void showCountries() {
Session session=getSession();
session.beginTransaction();
Country c=(Country)session.get(Country.class, new Integer(1));
Map<String,State> states = c.getStates();
Iterator entries = states.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<String,State> entry = (Map.Entry) entries.next();
String key = entry.getKey();
State value = (State)entry.getValue();
System.out.println("Key = " + key + ", Value = " + value.getName());
}
session.close();
}
}
When I try to run this program then I am getting exception as : 当我尝试运行此程序时,我得到例外:
Exception in thread "main" org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [org.hibernate.examples.State#0]
I am getting this exception at line session.save(country);
我在line
session.save(country);
获得此异常session.save(country);
Please let me know why I am getting this error? 请让我知道为什么我收到此错误?
Update: 更新:
Based on answer given by JB, now I added @GeneratedValue
to my Id's in Country and State. 根据JB给出的答案,现在我将
@GeneratedValue
添加到我的国家和州的Id中。 This time I started getting exception as: 这次我开始得到例外:
Aug 22, 2014 1:44:49 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1722, SQLState: 42000
Aug 22, 2014 1:44:49 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ORA-01722: invalid number
Also below are DDL and DML operations that Hibernate has generated: 以下是Hibernate生成的DDL和DML操作:
Hibernate: create table Country (id number(10,0) not null, name varchar2(255 char), primary key (id))
Hibernate: create table state (id number(10,0) not null, name varchar2(255 char), country_id number(10,0), primary key (id))
Hibernate: alter table state add constraint FK_lxoqjm8644epv72af3k3jpalx foreign key (country_id) references Country
Hibernate: create sequence hibernate_sequence
Aug 22, 2014 1:44:49 AM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into Country (name, id) values (?, ?)
Hibernate: insert into state (name, id) values (?, ?)
Hibernate: insert into state (name, id) values (?, ?)
Hibernate: update state set country_id=?, id=? where id=?
I am not clear on where I am still missing. 我不清楚我还在哪里。
Your State entity has an ID that is not autogenerated. 您的State实体具有未自动生成的ID。 And you always create states without specifying any ID.
而且您始终创建状态而不指定任何ID。 So all your states have the same ID: 0. Hence the exception.
因此所有状态都具有相同的ID:0因此异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.