Reputation: 1743
I'm new to Hibernate and JPA, so hopefully this is something basic. I'm getting an exception: "Foreign key referring Foo from Bar has the wrong number of columns". I'm not sure why. Any ideas? Thanks!
public abstract class Foo extends AbstractPersistable{
@Id @GeneratedValue
@Column(name = "FooId")
private Long foo_id;
@OneToMany(mappedBy= "foo")
private Set<Bar> bars;
}
public abstract class Bar extends AbstractPersistable{
@Id @GeneratedValue
@Column(name = "BarId")
private Long bar_id;
@ManyToOne
@JoinColumn(name = "FooId", nullable = false, insertable = false, updatable = false)
private Foo foo;
}
Upvotes: 1
Views: 547
Reputation: 1743
Turns out I was extending a class that already provided an Integer id. My id was of type Long, thus it was looking for two foreign keys.
Upvotes: 0
Reputation: 14149
Using @JoinColumn in such situation is redundant. Try such code:
public abstract class Foo extends AbstractPersistable{
@Id @GeneratedValue
@Column(name = "FooId")
private Long foo_id;
@OneToMany(mappedBy = "foo")
private Set<Bar> bars;
}
public abstract class Bar extends AbstractPersistable{
@Id @GeneratedValue
@Column(name = "BarId")
private Long bar_id;
@ManyToOne
private Foo foo;
}
Upvotes: 1