我有这个错误
访问字段[私有java.lang.Integer时出错
通过反射的fearx.projects.animal.finder.api.entity.LoginEntity.id]
永久财产
[fearx.projects.animal.finder.api.entity.LoginEntity#id]:2;嵌套的
例外是
org.hibernate.property.access.spi.PropertyAccessException:错误
访问字段[private java.lang.Integer
通过反射的fearx.projects.animal.finder.api.entity.LoginEntity.id]
永久财产
[fearx.projects.animal.finder.api.entity.LoginEntity#id]:2
当我尝试这样做时:
public void registerUser(OwnerEntity body) {
ownerRepository.save(body);
loginRepository.save(new LoginEntity(body.getEmail(), body.getPassword()));
LoginEntity loginEntity = loginRepository.findByEmail(body.getEmail());
OwnerEntity ownerEntity = ownerRepository.findByEmail(body.getEmail());
ownerLoginRepository.save(new OwnerLoginEntity(loginEntity.getId(),ownerEntity.getId()));
}
我的实体:
@Entity(name = "owner_login")
@Data
public class OwnerLoginEntity {
@Id
@GeneratedValue
private Integer id;
@ManyToOne(targetEntity = LoginEntity.class)
@JoinColumn(name = "id_login", referencedColumnName = "id")
private Integer login;
@ManyToOne(targetEntity = OwnerEntity.class)
@JoinColumn(name = "id_owner", referencedColumnName = "id")
private Integer owner;
public OwnerLoginEntity(Integer login, Integer owner) {
this.login = login;
this.owner = owner;
}
}
public class OwnerEntity {
@Id
@GeneratedValue
private Integer id;
@Column
private String name;
@Column
private String email;
@Column
private String phone;
@Column
private String password;
@ManyToOne(targetEntity = LostPetEntity.class)
@JoinColumn(name = "id_pet", referencedColumnName = "id")
private Integer id_pet;
}
@Entity(name = "login")
@Data
public class LoginEntity {
@Id
@GeneratedValue
private Integer id;
@Column(unique = true, nullable = false)
private String email;
@Column
private String password;
public LoginEntity(String email, String password) {
this.email = email;
this.password = password;
}
public LoginEntity() {
}
}
最佳答案
O不知道您拥有什么数据库,但是尝试更改GeneratedValue的方法,如下所示:
@Entity(name = "owner_login")
@Data
public class OwnerLoginEntity {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@ManyToOne
private LoginEntity login;
@ManyToOne
private OwnerEntity owner;
public OwnerLoginEntity(LoginEntity login, OwnerEntity owner) {
this.login = login;
this.owner = owner;
}
}
然后您可以拨打
ownerLoginRepository.save(new OwnerLoginEntity(loginEntity, ownerEntity));