单向JPA OnetoMany映射 - ORA-01722:无效数字

时间:2017-07-25 07:30:27

标签: hibernate jpa orm spring-data-jpa hibernate-mapping

我有两个带有模式的表,如下所示:

表1

table1_id(pk)(整数)

名称

表2

table2_id(pk)(String)

table1_id(pk)(整数)

名称

我的代码看起来像这样

@Entity
@Table(name = "Table2")
class Table2 {

    @Id
    @Column(name = "table2_id")  
    private String id;

    @Column(name="name")
    private String name;

    @OneToMany
    @JoinColumn (name="table1_id")
    private Set<Table1> table1Set = new HashSet<>();


    //setters and getters
}

@Entity
@Table(name = "Table1")
class Table1 {

    @Id
    @Column(name = "table1_id")  
    private int id;

    @Column(name="name")
    private String name;

    //setters and getters
}

现在我查询:Table2Repository.findById("One")

我得到以下错误:

Hibernate: select table2.id as table2_id_1_, table2.name as name2_1_ from table2 table20_
Hibernate: select table10_.table1_id as table1_id1_1_0_, table1_.table1_id as table1_id1_0_0_, table10_.table1_id as table1_id1_0_1_, table10_.name as name2_0_1_ from table1 table10_ where table10_.table1_id=?
2017-07-25 12:53:11.352  WARN 11572 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1722, SQLState: 42000
2017-07-25 12:53:11.352 ERROR 11572 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : ORA-01722: invalid number

似乎发生了一些不匹配。它似乎使用String“table2_id”来查询Table1,因为它应该使用“table1_id”

请帮忙!

1 个答案:

答案 0 :(得分:0)

您的地图中存在名称冲突:@JoinColumn (name="table1_id")上的Table2.table1Set@Id @Column(name = "table1_id")上的Table1.id。请注意,加入列会转到Table1不是 Table2

您可能的意思是这样的架构:PERSON(id, name, age, a_bunch_of_other_stuff)PERSON_PHONE_NUMBER(person_id, phone_id)PHONE_NUMBER(id, phone_number),在这种情况下,您的映射应如下所示:

@Entity
@Table(name = "PERSON")
public class Person {

    @Id
    private int id;

    private String name;
    ...

    @OneToMany
    @JoinTable(name = "PERSON_PHONE_NUMBER", 
        joinColumns = @JoinColumn(name = "PERSON_ID", referencedColumnName = "ID"), 
        inverseJoinColumns = @JoinColumn(name = "PHONE_ID", referencedColumnName = "ID")
    )
    private Set<PhoneNumber> phoneNumbers;
    ...
}

@Entity
@Table(name = "PHONE_NUMBER")
public class PhoneNumber {

    @Id
    private String id;

    @Column(name = "PHONE_NUMBER")
    private String phoneNumber;
    ...
}