【发布时间】:2018-09-23 13:23:55
【问题描述】:
扩展 Payphone(table) 找不到 id 为 16 的 phonenumber(table),但存在 id 为 16 的 phonenumber(table)
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 上使用 DB 进行项目
在项目中存在 10 个表。 9张桌子只有1张不起作用。 在 index.html 中,我看到 id 为 16 的 phonenumber(table) 存在。 我已经尝试了所有方法,但只有在我删除关系时才能使用桌面公用电话。
【问题讨论】:
-
你最后一段说什么?如果您在发布之前没有查看您的问题,您希望社区提供什么帮助。请在发布之前查看您的问题。见stackoverflow.com/help/how-to-ask
-
我更正了这一段
标签: spring spring-boot spring-data-jpa repository