我有两个实体A和B-A是通过使用swagger-codegen-maven-plugin自动生成的,而B是由我创建的,并且包含与A的多对一关系。自动生成的一个(A)具有属性为4,但是数据库中与之对应的表还有一个名为id的附加列,该列是第二个实体(B)中外键的引用列,称为idA。
我必须将类型B的对象保留在数据库中,并且还需要设置idA的值。即使值设置得很好,也可以通过从实体A类型的对象(从数据库中获取)获取值,我得到java.lang.IllegalArgumentException:对象不是声明类的实例。如果我有一个类型为A的对象,则会得到该值,但如果将其分配给对象B,则会出现该错误。
我将显示代码:
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2019-10-31T17:01:04.704+02:00")
public class A{
protected Integer accountId = null;
protected Integer range = null;
protected Integer address = null;
protected Integer agency = null;
protected Long id;
@Column(
name = "ID"
)
@GeneratedValue(
strategy = GenerationType.AUTO
)
public Long getId() {
return this.id;
}
public void setid(Long value) {
this.id = value;
}
@Entity
@Table(name = "STUDENTS")
public class B implements Serializable {
private long id;
private String name;
private long idA;
private long age;
@ManyToOne(targetEntity = A.class, cascade = {
CascadeType.ALL
})
@JoinColumn(name = "ID_A", referencedColumnName = "ID", nullable = false)
public long getIdA() {
return idA;
}
public void setIdA(long idA) {
this.idA= idA;
}
EntityManager em;
List<A> entitiesList = repositoryA.findAllEntities(); // method which gets all the entries from the table
for(int i = 0; i < entitiesList.size(); i++) {
B target = new B();
target.setName("Andrew");
target.setidA(entitiesList.get(i).getId()); // the value for the id is correctly taken from the database entry
em.persist(B); ---> line which gives error because of IllegalArgumentException
如果自动生成的类没有作为外键的列的getter或setter方法,如何保存我的对象?