原因:org.hibernate.AnnotationException:mappedBy引用了未知的目标实体属性:jpa + springboot

时间:2019-02-10 12:16:16

标签: spring-boot jpa one-to-many many-to-one

模型类

@Entity
@Table(name="RequisitionRequest")
public class Requisition {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private String requestedPerson;
    private String contactDetails;
    private String appliedDate;
    private String branch;
    private String department;

    private String status;
     @OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER,mappedBy = "RequisitionRequest")
    private List<Nationality> nationality;
//getters and setters
}

民族国籍

@Entity
@Table(name="nationality")
public class Nationality {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String nationality;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "nationality_id", nullable = false)
    private Requisition requisition;
//getters and setters
}

存储库类

@Repository
public interface NationalityRepository extends JpaRepository<Nationality,Long>{

}

@Repository
public interface RequisitionRepository extends JpaRepository<Requisition,Long> {

}

服务等级

@Service
public class RequisitionServiceImpl implements RequisitionService {
    @Autowired
    RequisitionRepository requisitionRepository;

    @Override
    public void save(Requisition requisition) {
        requisitionRepository.save(requisition);

    }
}

控制器类

@RestController
public class RequisitionController {
    @Autowired
    RequisitionService requisitionService;

    @Autowired
    RequisitionRepository requisitionRepository;
@PostMapping("/requisition/")
    @CacheEvict(value = "requisitions", allEntries=true)
    public ResponseEntity<Requisition> addRequisition(@RequestBody Requisition requisition) {
        System.out.print(requisition);

        Requisition requisitionR = new Requisition();
        Requisition response = new Requisition();




        requisitionR.setBranch(requisition.getBranch());
        requisitionR.setDepartment(requisition.getDepartment());
        requisitionR.setExpectedDateofJoin(requisition.getExpectedDateofJoin());

        //requisitionR.setNationality(requisition.getNationality());


        requisitionRepository.save(requisitionR);


        return new ResponseEntity<Requisition>(response, HttpStatus.OK);
    }

我正在学习spring boot,只需编写示例程序来学习spring boot + jpa +一对多关系。当执行上述代码时,出现

之类的错误

原因:org.hibernate.AnnotationException:mappedBy引用未知目标实体属性:officekitRecruitment.model.Requisition.nationality中的officekitRecruitment.model.Nationality.RequisitionRequest     在org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:769)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final]     在org.hibernate.cfg.annotations.CollectionBinder $ 1.secondPass(CollectionBinder.java:719)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final]     在org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:54)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final]     在org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1655)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final]     在org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1623)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final]     在org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final]     在org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:847)〜[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]     在org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:874)〜[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]

我尝试了此站点中已经提到的解决方案,但对我而言不起作用。 谁能帮我解决这个问题

1 个答案:

答案 0 :(得分:1)

您应该在MappedBy参数上引用映射实体中字段的名称。

喜欢

mappedBy = "requisition"