我正在尝试使用Map with Hibernate创建一个简单的程序。我有一个国家实体,其中包含州的地图。这是我的课程:
@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
}
这是我创建一些国家和州的程序:
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();
}
}
当我尝试运行此程序时,我得到例外:
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]
我在第session.save(country);
行收到此异常请告诉我为什么会收到此错误?
更新
根据JB给出的答案,现在我将@GeneratedValue
添加到我在国家和州的ID中。这次我开始异常:
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
以下是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=?
我不清楚我还在哪里。
答案 0 :(得分:3)
您的州实体的ID不是自动生成的。而且您始终创建状态而不指定任何ID。所以你所有的州都有相同的ID:0因此异常。