模型类
@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);
}
我正在学习春天开机,只是写的示例程序来学习春天开机+ JPA +一个一对多relation.when执行此上面的代码得到错误像
org.hibernate.AnnotationException:通过引起officekitRecruitment.model.Requisition.nationality在org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:769 officekitRecruitment.model.Nationality.RequisitionRequest:的mappedBy引用一个未知目标实体属性)〜[冬眠核-5.0.12.Final.jar:5.0.12.Final]在org.hibernate.cfg.annotations.CollectionBinder $ 1.secondPass(CollectionBinder.java:719)〜[冬眠核-5.0.12 .Final.jar:5.0.12.Final]在org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:54)〜[冬眠核-5.0.12.Final.jar:5.0.12.Final]在有机.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1655)〜[冬眠核-5.0.12.Final.jar:5.0.12.Final]在org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses( InFlightMetadataCollectorImpl.java:1623)〜[冬眠核-5.0.12.Final.jar:5.0.12.Final]在org.hibernate.boot.model .process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278)〜[休眠核-5.0.12.Final.jar:5.0.12.Final]在org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata( EntityManagerFactoryBuilderImpl.java:847)〜[冬眠-的EntityManager-5.0.12.Final.jar:5.0.12.Final]在org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:874)〜[休眠-entitymanager-5.0.12.Final.jar:5.0.12.Final]
我试着在这个网站已经提到的解决方案,但它并没有为我工作。任何人可以帮助我解决这个问题
你应该是指字段的名称在映射实体您的mappedBy参数。
像那样
mappedBy = "requisition"