当前位置: 首页 > 知识库问答 >
问题:

Hibernate映射抛出- ORA-01722:无效数字错误

巫马泰
2023-03-14

我正在尝试使用带有Hibernate的Map创建一个简单的程序。我有一个带有州地图的Country实体。这是我的类:

@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]

我在第< code >行session.save(country)处得到此异常;请告诉我为什么会出现这个错误?

更新:

根据JB给出的答案,现在我在Country和State中添加了@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=?

我不清楚我还遗漏了什么。

共有1个答案

仲智
2023-03-14

您的国家/省/市/自治区实体具有未自动生成的 ID。而且,您始终无需指定任何 ID 即可创建状态。因此,您的所有州都具有相同的 ID:0。因此例外。

 类似资料:
  • 我正在我的Oracle客户机中运行下面提到的查询,我得到了 ORA-01722:无效号码 错误。我知道这个问题是由于TAG_VALUE列的类型是“”,我正在将其转换为数字,然后在where子句中使用该字段。我尝试过使用“”功能,但这也没有帮助。如果我运行查询时忽略了最后一个 where 条件,代码为

  • 我有两个表,模式如下: 表1 table1_id(pk)(整数) 姓名 表2 table2_id(pk)(字符串) table1_id(pk)(整数) 姓名 我的代码看起来像这样 现在当我查询:: 我得到以下错误: 似乎出现了一些不匹配。它似乎使用字符串“表2_id”来查询表1,而它应该使用“表1_id” 好心帮忙!

  • 对于某人来说,这是一个非常简单的方法,下面的插入内容给了我 ORA-01722:无效的数字 为什么?

  • 我不明白为什么我会犯这个错误。 当我对交互式报表执行此查询时,它会引发 ORA-01722 错误。此查询不仅在 SQL 开发人员中正确运行,而且作为经典报表正确运行。当我将类型更改为交互式报表时,再次抛出相同的错误。 我该怎么办? 提前多谢。

  • 请帮助我修复在尝试将内容插入下表时收到的错误消息。举个例子可以帮助我更好地理解它。 插入: 错误报告-SQL错误:ORA-01722:无效号码01722。00000-"无效号码"*原因:指定的号码无效。*操作:指定有效号码。

  • 我正在创建一个对账报告,我主要是想知道每个发送批次的单据价值。 批处理 ID 位于其自己的列中,发送的数据位于一列中,并以逗号分隔,请参阅下面随机生成的示例: 每个批次至少有2行,最大可以是2000行。在此表中,我有两列:Batch ID和Data 我需要将所有文件金额相加,在这种情况下为100 200。 目前,我正在尝试通过此查询完成工作: 但是以错误ORA-01722结束:无效数字 提前感谢,