为什么我无法使用多对一映射

时间:2019-05-18 21:34:07

标签: java hibernate jpa many-to-one

有人知道为什么我不能使用多对一映射吗?

这是我的模特

    @Entity(name="trxrawatjalan")
    public class Rawatjalan implements Serializable {

        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private int idRawatjalan;
        @Column(nullable = false, insertable = false, updatable = false)
        private int idPasien;
        @Column(nullable = false, insertable = false, updatable = false)
        private int idDokter;
        @Column(nullable = false, insertable = false, updatable = false)
        private int idTreatment;
        public Rawatjalan() {
        }
        public Rawatjalan(int idRawatjalan) {
            this.idRawatjalan = idRawatjalan;
        }
        public int getIdPasien() {
            return idPasien;
        }
        public void setIdPasien(int idPasien) {
            this.idPasien = idPasien;
        }
        public int getIdDokter() {
            return idDokter;
        }
        public void setIdDokter(int idDokter) {
            this.idDokter = idDokter;
        }
        public int getIdTreatment() {
            return idTreatment;
        }
        public void setIdTreatment(int idTreatment) {
            this.idTreatment = idTreatment;
        }
        @ManyToOne(fetch = FetchType.LAZY, optional =  false)
        @JoinColumn(name= "idPasien", nullable = false)
        private Pasien pasien;
        @ManyToOne(fetch = FetchType.LAZY, optional =  false)
        @JoinColumn(name= "idDokter", nullable = false)
        private Dokter dokter;
        @ManyToOne(fetch = FetchType.LAZY, optional =  false)
        @JoinColumn(name= "idTreatment", nullable = false)
        private Treatment treatment;
        public int getIdRawatjalan() {
            return idRawatjalan;
        }
        public void setIdRawatjalan(int idRawatjalan) {
            this.idRawatjalan = idRawatjalan;
        }
            @Override
            public String toString () {
                return "Rawatjalan{" +
                        "idRawatjalan=" + idRawatjalan +
                        '}';

这是我的trxindex.jsp

     <c:forEach var="rawatjalan" items="${rawatjalans}">
                            <tr>
                                <td>${rawatjalan.idRawatjalan}</td>
                                <td>${rawatjalan.pasien.idPasien}</td>
                                <td>${rawatjalan.dokter.idDokter}</td>
                                <td>${rawatjalan.treatment.idTreatment}</td>
                                <td><a href="update-rawatjalan?idRawatjalan=${rawatjalan.idRawatjalan}"><span class="glyphicon glyphicon-pencil"></span></a></td>
                                <td><a href="delete-rawatjalan?idRawatjalan=${rawatjalan.idRawatjalan}"><span class="glyphicon glyphicon-trash"></span></a></td>
                            </tr>
                        </c:forEach>
                        </tbody>
                    </table>
                </div>
            </div>
    <form class="form-horizontal" method="POST" action="save-treatment">
                    <input type="hidden" name="idRawatjalan" value="${rawatjalan.idRawatjalan}"/>
                    <div class="form-group">
                        <label class="control-label col-md-3">Id Pasien</label>
                        <div class="col-md-7">
                            <input type="text" class="form-control" name="rawatjalan.pasien.idPasien" value="${rawatjalan.pasien.idPasien}"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-md-3">id Dokter</label>
                        <div class="col-md-7">
                            <input type="text" class="form-control" name="rawatjalan.dokter.idDokter" value="${rawatjalan.dokter.idDokter}"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-md-3">id Treatment</label>
                        <div class="col-md-7">
                          <input type="text" class="form-control" rows="3" name="rawatjalan.treatment.idTreatment" value="${rawatjalan.treatment.idTreatment}"/>
                        </div>
                    </div>

这是我的控制者:

    @Autowired
    private TrxService trxService;

    @GetMapping("/data-rawatjalan")
        public String dataRawatjalans(HttpServletRequest request){
            request.setAttribute("rawatjalans", trxService.findAll());
            request.setAttribute("mode", "MODE_TASKS");
            return "indextrx";
        }

    @GetMapping("/new-rawatjalan")
        public String newRawatjalan(HttpServletRequest request){
            request.setAttribute("mode", "MODE_NEW");
            return "indextrx";
        }

    @PostMapping("/save-rawatjalan")
        public String saveRawatjalan(@ModelAttribute Rawatjalan rawatjalan, BindingResult bindingResult, HttpServletRequest request){
            trxService.save(rawatjalan);
            request.setAttribute("rawatjalans", trxService.findAll());
            request.setAttribute("mode", "MODE_TASKS");
            return "indextrx";
        }

    @GetMapping("/update-rawatjalan")
        public String updateRawatjalan(@RequestParam int idRawatjalan, HttpServletRequest request){
            request.setAttribute("rawatjalan", trxService.findRawatjalan(idRawatjalan));
            request.setAttribute("mode", "MODE_UPDATE");
            return "indextrx";
        }

    @GetMapping("/delete-rawatjalan")
        public String deleteRawatjalan(@RequestParam int idRawatjalan, HttpServletRequest request){
            trxService.delete(idRawatjalan);
            request.setAttribute("rawatjalans", trxService.findAll());
            request.setAttribute("mode", "MODE_TASKS");
            return "indextrx";
        }

这是我的服务

    @Service
    @Transactional
    public class TrxService {

        private final TrxinapRepository trxinapRepository;

        public TrxService(TrxinapRepository trxinapRepository) {
            this.trxinapRepository = trxinapRepository;
        }

        public List<Rawatjalan> findAll(){
            List<Rawatjalan> rawatjalans = new ArrayList<>();
            for(Rawatjalan rawatjalan : trxinapRepository.findAll()){
                rawatjalans.add(rawatjalan);
            }
            return rawatjalans;
        }

        public Rawatjalan findRawatjalan(int idRawatjalan){
            return trxinapRepository.findOne(idRawatjalan);
        }

        public void save(Rawatjalan rawatjalan){
            trxinapRepository.save(rawatjalan);
        }

        public void delete(int idRawatjalan){
            trxinapRepository.delete(idRawatjalan);
        }
    }

    My Repository
package bootsample.dao;
    import bootsample.mod`enter code here`el.Rawatjalan;
    import org.springframework.data.repository.CrudRepository;
    public interface TrxinapRepository extends CrudRepository<Rawatjalan, Integer>{
    }
  

2019-05-19 03:10:50.977错误1712-[nio-8030-exec-4] oaccC [。[。[/]。[dispatcherServlet]:Servlet [dispatcherServlet]的Servlet.service()在带有路径[]的上下文中引发了异常[在第77行处理JSP页面/WEB-INF/jsp/indextrx.jsp时发生了异常       74:       75:       76:$ {rawatjalan.idRawatjalan}       77:$ {rawatjalan.pasien.idPasien}       78:$ {rawatjalan.dokter.idDokter}       79:$ {rawatjalan.treatment.idTreatment}       80:   Stacktrace:]具有根本原因   javax.el.PropertyNotFoundException:在bootsample.model.Rawatjalan类型上找不到属性“ pasien”

0 个答案:

没有答案