gpt4 book ai didi

java - 访问字段时出错(使用 Hibernate/JPA)

转载 作者:行者123 更新时间:2023-11-30 05:23:06 25 4
gpt4 key购买 nike

我遇到了这个错误

Error accessing field [private java.lang.Integer fearx.projects.animal.finder.api.entity.LoginEntity.id] by reflection for persistent property [fearx.projects.animal.finder.api.entity.LoginEntity#id] : 2; nested exception is org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private java.lang.Integer fearx.projects.animal.finder.api.entity.LoginEntity.id] by reflection for persistent property [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不知道你有什么数据库,但尝试改变GenerateValue的方法,如下所示:

@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));

关于java - 访问字段时出错(使用 Hibernate/JPA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59182111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com