我正在尝试使用 Map 和 Hibernate 创建一个简单的程序。我有一个带有州 map 的国家/地区实体。这是我的类(class):
@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=?
我不清楚我还想念哪里。
最佳答案
您的州实体有一个非自动生成的 ID。而且您总是在不指定任何 ID 的情况下创建状态。所以你所有的州都有相同的 ID:0。因此异常(exception)。
关于java - Hibernate 映射抛出 - ORA-01722 : invalid number error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25434892/