我有两个实体foo
和bar
有多对多关系。我为它们创建了manyToMany实体FooBar,将其主键用作带有@IdClass的复合主键ID:
Foo类:
@Entity
@Table(name = "foos")
public class Foo implements Serializable {
private static final long serialVersionUID = -1L;
@Id
@Column(name="name")
private String name;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "bar", orphanRemoval = true)
@NotFound(action = NotFoundAction.IGNORE)
@JsonIgnoreProperties
@JsonIgnore
private List<Foo_bar> fooBars;
// constructors, getters, setters, ..
}
班级酒吧:
@Entity
@Table(name = "bars")
public class Bar implements Serializable {
private static final long serialVersionUID = -1L;
@Id
@Column(name="name")
private String name;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "foo", orphanRemoval = true)
@NotFound(action = NotFoundAction.IGNORE)
@JsonIgnoreProperties
@JsonIgnore
private List<Foo_bar> fooBars;
// constructors, getters, setters, ..
}
FooBar类:
@Entity
@IdClass(FooBarID.class)
@Table(name = "foo_bars")
public class Foo_bar implements Serializable {
@Id
@ManyToOne(cascade=CascadeType.MERGE, fetch=FetchType.EAGER)
private Foo foo;
@Id
@ManyToOne(cascade=CascadeType.MERGE, fetch=FetchType.EAGER)
private Bar bar;
// constructors, getters, setters, ..
}
FooBarId类:
public class FooBarID implements Serializable {
Foo foo;
Bar bar;
public FooBarID() {
}
public FooBarID(Foo foo, Bar bar) {
this.foo = foo;
this.bar = bar;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((foo == null) ? 0 : foo.hashCode());
result = prime * result + ((bar == null) ? 0 : bar.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
return
(this.foo == ((FooBarID) obj).foo)
&& (this.bar == ((FooBarID) obj).bar);
}
}
如果我现在尝试使用jpa和以下代码创建并保存新的FooBar:
Foo foo = this.fooRepository.getByName('foo123').get();
Bar bar = this.barRepository.getByName('bar123').get();
FooBar fooBar = new FooBar();
fooBar.setFoo(foo);
fooBar.setBar(bar);
this.fooBarRepository.save(fooBar); <-- exception
抛出InvalidDataAccessApiUsageException
:Can not set package.Foo field package.FooBarID.foo to java.lang.String; nested exception is java.lang.IllegalArgumentException: Can not set package.Foo field package.FooBarID.foo to java.lang.String
有人知道如何解决此问题吗? :/