Spring存储库exeption

时间:2018-04-13 08:36:45

标签: spring spring-boot spring-data-jpa repository

exaption 公用电话(表)不会找到id为16的phonenumber(表),但是存在id为16的phonenumber(表)

   Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find com.example.demo.model.Phonenumber with id 16; nested exception is javax.persistence.EntityNotFoundException: Unable to find com.example.demo.model.Phonenumber with id 16] with root cause

javax.persistence.EntityNotFoundException: Unable to find 
com.example.demo.model.Phonenumber with id 16

控制器

package com.example.demo.controller;

import com.example.demo.model.*;
import com.example.demo.model.Number;
import com.example.demo.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/get")
public class MyController {
@Autowired
RegionRepository regionRepository;

@Autowired
StreetRepository streetRepository;

@Autowired
CallsRepository callsRepository;

@Autowired
CityRepository cityRepository;

@Autowired
ConsumerRepository consumerRepository;

@Autowired
MTCRepository mtcRepository;

@Autowired
NumberRepository numberRepository;

@Autowired
PayphonesRepository payphonesRepository;

@Autowired
PhonenumberRepository phonenumberRepository;

@Autowired
QueueRepository queueRepository;

@RequestMapping("/streets")
public List<Street> getStreets(){
    return streetRepository.findAll();
}

@RequestMapping("/regions")
public List<Region> getRegions(){
    return regionRepository.findAll();
}

@RequestMapping("/calls")
public List<Calls> getCalls(){
    return callsRepository.findAll();
}

@RequestMapping("/cities")
public List<City> getCities(){
    return cityRepository.findAll();
}

@RequestMapping("/consumers")
public List<Consumer> getConsumers(){
    return consumerRepository.findAll();
}

@RequestMapping("/mtces")
public List<Mtc> getMTCes(){
    return mtcRepository.findAll();
}

@RequestMapping("/numbers")
public List<Number> getNumbers(){
    return numberRepository.findAll();
}

@RequestMapping("/payphones")
public List<Payphones> getPayphones(){
    return payphonesRepository.findAll();
}

@RequestMapping("/phonenumbers")
public List<Phonenumber> getPhonenumbers(){
    return phonenumberRepository.findAll();
}

@RequestMapping("/queues")
public List<Queue> getQueues(){
    return queueRepository.findAll();
}


}

模型

    package com.example.demo.model;

import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;

@Entity
@Table(name = "payphones")
@EntityListeners(AuditingEntityListener.class)
public class Payphones {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;


@OneToOne(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
@JoinColumn(name = "phonenumber_id")
private Phonenumber phonenumber;

public Payphones() {
}

public Payphones(Phonenumber phonenumber) {
    this.phonenumber = phonenumber;
}

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public Phonenumber getPhonenumber() {
    return phonenumber;
}

public void setPhonenumber(Phonenumber phonenumber) {
    this.phonenumber = phonenumber;
}
}

package com.example.demo.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;

@Entity
@Table(name = "phonenumber")
@EntityListeners(AuditingEntityListener.class)
public class Phonenumber {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;

@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "number")
private Number number;

@JsonIgnore
@OneToOne(mappedBy = "phonenumber", cascade = CascadeType.ALL,
        fetch = FetchType.EAGER, optional = false)
private Consumer consumer;

@Column(name = "housenumber")
private String houseNumber;

@Column(name = "apartment")
private Long apartment;

@Column(name = "interspace")
private long interspace;

@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "Street_id")
private Street street;

@Enumerated(EnumType.STRING)
@Column(name = "phonetype")
private PhoneType phoneType;

public Phonenumber() {
}

public Phonenumber(Number number, Consumer consumer, String houseNumber, 
Long apartment, long interspace, Street street, PhoneType phoneType) {
    this.number = number;
    this.consumer = consumer;
    this.houseNumber = houseNumber;
    this.apartment = apartment;
    this.interspace = interspace;
    this.street = street;
    this.phoneType = phoneType;
}

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public Number getNumber() {
    return number;
}

public void setNumber(Number number) {
    this.number = number;
}

public Consumer getConsumer() {
    return consumer;
}

public void setConsumer(Consumer consumer) {
    this.consumer = consumer;
}

public String getHouseNumber() {
    return houseNumber;
}

public void setHouseNumber(String houseNumber) {
    this.houseNumber = houseNumber;
}

public Long getApartment() {
    return apartment;
}

public void setApartment(Long apartment) {
    this.apartment = apartment;
}

public long getInterspace() {
    return interspace;
}

public void setInterspace(long interspace) {
    this.interspace = interspace;
}

public Street getStreet() {
    return street;
}

public void setStreet(Street street) {
    this.street = street;
}

public PhoneType getPhoneType() {
    return phoneType;
}

public void setPhoneType(PhoneType phoneType) {
    this.phoneType = phoneType;
}
}

使用git click

上的数据库进行项目

项目中存在10个表。 9个表只工作1个不起作用。 在index.html中,我看到存在id为16的phonenumber(表)。 我已经尝试了所有东西,但桌面付费电话仅在我删除关系时起作用。

1 个答案:

答案 0 :(得分:0)

我解决了 改变这个

@JsonIgnore
@OneToOne(mappedBy = "phonenumber", cascade = CascadeType.ALL,
    fetch = FetchType.EAGER, optional = false)
private Consumer consumer;

到这个

@JsonIgnore
@OneToOne(mappedBy = "phonenumber", cascade = CascadeType.ALL,
    fetch = FetchType.EAGER)
private Consumer consumer;