我正在尝试进行一对多关系(表EmployeeEntity和AddressEntity),但是在构建时出现错误。
AddressEntity类
@Data
@EqualsAndHashCode(exclude = "eeEntity")
public class AddressEntity {
..
@OneToMany(mappedBy = "addressEntity", targetEntity = com.howtodoinjava.demo.model.AddressEntity.class,cascade = CascadeType.ALL)
private Set<EmployeeEntity> eeEntity;
public AddressEntity(String city, EmployeeEntity... eeEntity) {
this.city = city;
this.eeEntity = Stream.of(eeEntity).collect(Collectors.toSet());
this.eeEntity.forEach(x -> x.setAddressEntity(this));
}
---------
EmployeeEntity类
@Data
@Entity
public class EmployeeEntity {
...
@ManyToOne(targetEntity = com.howtodoinjava.demo.model.AddressEntity.class)
private AddressEntity addressEntity;
public AddressEntity getAddressEntity() {
return addressEntity;
}
public void setAddressEntity(AddressEntity addressEntity) {
this.addressEntity = addressEntity;
错误:2020-01-17 00:01:24.401 WARN 4890 --- [main] ConfigServletWebServerApplicationContext:上下文初始化期间遇到异常-取消刷新尝试:org.springframework.beans.factory.BeanCreationException:创建名称定义为'entityManagerFactory'的bean时出错类路径资源[org / springframework / boot / autoconfigure / orm / jpa / HibernateJpaConfiguration.class]:调用init方法失败;嵌套异常是org.hibernate.AnnotationException:mappedBy通过引用未知目标实体属性:com.howtodoinjava.demo.model.AddressEntity.addressEntity中的com.howtodoinjava.demo.model.AddressEntity.addressEntity
org.springframework.beans.factory.BeanCreationException:在类路径资源中创建名称为'entityManagerFactory'的bean时出错[org / springframework / boot / autoconfigure / orm / jpa / HibernateJpaConfiguration.class]:调用init方法失败;嵌套异常是org.hibernate.AnnotationException:mappedBy通过引用未知目标实体属性:com.howtodoinjava.demo.model.AddressEntity.addressEntity中的com.howtodoinjava.demo.model.AddressEntity.addressEntity
有人可以帮助我理解我为什么得到这个吗?
尝试下面的代码段,
在AddressEntity类中,
@OneToMany(mappedBy = "addressEntity", targetEntity = com.howtodoinjava.demo.model.AddressEntity.class,cascade = CascadeType.ALL)
private Set<EmployeeEntity> eeEntity;
在EmployeeEntity类中,
@ManyToOne
@JoinColumn(name="ADDRESS_ID", nullable=false)
private AddressEntity addressEntity;
其中ADDRESS_ID可以是AddressEntity的主键。