在具有双向连接的特定实体上运行 findById(Long id) Jpa 方法时出现“java.lang.***Error: null”
Posted
技术标签:
【中文标题】在具有双向连接的特定实体上运行 findById(Long id) Jpa 方法时出现“java.lang.***Error: null”【英文标题】:Having a `java.lang.***Error: null` when running a findById(Long id) Jpa method on a specific Entity with bidirectional connection 【发布时间】:2019-04-22 06:28:10 【问题描述】:我在使用特定实体 (CtThhlastikaPressThreats
) 的 findById Jpa 方法时遇到问题。
为了更清楚地了解该项目,它使用了 Spring Boot、Jpa 和 mysql。
存在具有双向连接@OneToMany
和@ManyToOne
的实体,同时它们之间还有mappedBy
和JoinColumn
。
我遇到问题的特定实体具有与 DeigmaThhlastikwn
实体类似的双向连接(没问题,我曾经遇到过 *** 异常,但通过 @JsonIgnore
的注释解决了. 我在这个上试过了,你可以在@ManyToOne
一侧看到DeigmaThhlastikwnXPressThreats
实体,但它没有用)
CtThhlastikaPressThreatsController
休息控制器:
@RestController
@RequestMapping(CtThhlastikaPressThreatsController.BASE_URL)
public class CtThhlastikaPressThreatsController
public static final String BASE_URL = "/v1/ctThhlastikaPressThreats";
private CtThhlastikaPressThreatsService ctThhlastikaPressThreatsService;
@Autowired
private CtThhlastikaPressThreatsRepositoryImpl ctThhlastikaPressThreatsRepository;
public CtThhlastikaPressThreatsController(CtThhlastikaPressThreatsService ctThhlastikaPressThreatsService)
this.ctThhlastikaPressThreatsService = ctThhlastikaPressThreatsService;
@CrossOrigin
@GetMapping("/id")
public CtThhlastikaPressThreats findById(@PathVariable Long id)
return ctThhlastikaPressThreatsRepository.findCtThhlastikaPressThreatsById(id);
@CrossOrigin
@PostMapping()
public CtThhlastikaPressThreats addPressThreat(@RequestBody CtThhlastikaPressThreatDTO ctThhlastikaPressThreatDTO)
try
return ctThhlastikaPressThreatsService.addPressThreat(ctThhlastikaPressThreatDTO);
catch (Exception e)
throw e;
@CrossOrigin
@GetMapping("/getAllActCodes")
public ArrayList<String> getAllActCodes()
try
return ctThhlastikaPressThreatsService.getAllActCodes();
catch (Exception e)
throw e;
CtThhlastikaPressThreats
实体:
package com.teicm.kerkinibackend.domain;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
public class CtThhlastikaPressThreats
@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "ctThhlastikaPressThreats")
private Set<DeigmaThhlastikwnXPressThreats> deigmaThhlastikwnXPressThreatsSet = new HashSet<>();
@Column(name = "act_code", unique = true, length = 50)
private String actCode;
@Column(name = "description_en")
private String descriptionEn;
@Column(name = "remarks")
private String remarks;
public CtThhlastikaPressThreats addPressThreat(DeigmaThhlastikwnXPressThreats deigmaThhlastikwnXPressThreats)
deigmaThhlastikwnXPressThreats.setCtThhlastikaPressThreats(this);
this.deigmaThhlastikwnXPressThreatsSet.add(deigmaThhlastikwnXPressThreats);
return this;
public CtThhlastikaPressThreats()
public CtThhlastikaPressThreats(String actCode, Set<DeigmaThhlastikwnXPressThreats> deigmaThhlastikwnXPressThreatsSet, String descriptionEn, String remarks)
this.actCode = actCode;
this.deigmaThhlastikwnXPressThreatsSet = deigmaThhlastikwnXPressThreatsSet;
this.descriptionEn = descriptionEn;
this.remarks = remarks;
public Long getId()
return id;
public void setId(Long id)
this.id = id;
public String getActCode()
return actCode;
public void setActCode(String actCode)
this.actCode = actCode;
public Set<DeigmaThhlastikwnXPressThreats> getDeigmaThhlastikwnXPressThreatsSet()
return deigmaThhlastikwnXPressThreatsSet;
public void setDeigmaThhlastikwnXPressThreatsSet(Set<DeigmaThhlastikwnXPressThreats> deigmaThhlastikwnXPressThreatsSet)
this.deigmaThhlastikwnXPressThreatsSet = deigmaThhlastikwnXPressThreatsSet;
public String getDescriptionEn()
return descriptionEn;
public void setDescriptionEn(String descriptionEn)
this.descriptionEn = descriptionEn;
public String getRemarks()
return remarks;
public void setRemarks(String remarks)
this.remarks = remarks;
DeigmaThhlastikwnXPressThreats
实体:
package com.teicm.kerkinibackend.domain;
import com.fasterxml.jackson.annotation.JsonIgnore;
import javax.persistence.*;
@Entity
public class DeigmaThhlastikwnXPressThreats
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "deigma_thhlastikwn_id")
@JsonIgnore
private DeigmaThhlastikwn deigmaThhlastikwn;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "ct_thhlastika_press_threats_id")
@JsonIgnore
private CtThhlastikaPressThreats ctThhlastikaPressThreats;
@Column(name = "kwdikos_eidous", length = 50)
private String kwdikosEidous;
@Column(name = "press_threat", length = 50)
private String pressThreat;
@Column(name = "importance", length = 50)
private String importance;
public DeigmaThhlastikwnXPressThreats()
public DeigmaThhlastikwnXPressThreats(DeigmaThhlastikwn deigmaThhlastikwn, CtThhlastikaPressThreats ctThhlastikaPressThreats, String kwdikosEidous, String pressThreat, String importance)
this.deigmaThhlastikwn = deigmaThhlastikwn;
this.ctThhlastikaPressThreats = ctThhlastikaPressThreats;
this.kwdikosEidous = kwdikosEidous;
this.pressThreat = pressThreat;
this.importance = importance;
public Long getId()
return id;
public void setId(Long id)
this.id = id;
public DeigmaThhlastikwn getDeigmaThhlastikwn()
return deigmaThhlastikwn;
public void setDeigmaThhlastikwn(DeigmaThhlastikwn deigmaThhlastikwn)
this.deigmaThhlastikwn = deigmaThhlastikwn;
public CtThhlastikaPressThreats getCtThhlastikaPressThreats()
return ctThhlastikaPressThreats;
public void setCtThhlastikaPressThreats(CtThhlastikaPressThreats ctThhlastikaPressThreats)
this.ctThhlastikaPressThreats = ctThhlastikaPressThreats;
public String getKwdikosEidous()
return kwdikosEidous;
public void setKwdikosEidous(String kwdikosEidous)
this.kwdikosEidous = kwdikosEidous;
public String getPressThreat()
return pressThreat;
public void setPressThreat(String pressThreat)
this.pressThreat = pressThreat;
public String getImportance()
return importance;
public void setImportance(String importance)
this.importance = importance;
DeigmaThhlastikwn
实体:
package com.teicm.kerkinibackend.domain;
import javax.persistence.*;
import java.sql.Date;
import java.sql.Time;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "deigma_thhlastikwn")
public class DeigmaThhlastikwn
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "deigmaThhlastikwn", fetch = FetchType.EAGER)
private Set<DeigmaThhlastikwnXEidh> deigmaThhlastikwnXEidhSet = new HashSet<>();
@OneToMany(cascade = CascadeType.ALL, mappedBy = "deigmaThhlastikwn", fetch = FetchType.EAGER)
private Set<DeigmaThhlastikwnXPressThreats> deigmaThhlastikwnXPressThreatsSet = new HashSet<>();
@Column(name = "kwdikos_deigmatos", nullable = false)
private String kwdikosDeigmatolhpsias;
@Column(name = "xrhmatodothsh")
private String xrhmatodothsh;
@Column(name = "ereunhths")
private String ereunhths;
@Column(name = "topothesia")
private String topothesia;
@Column(name = "date")
private Date date;
@Column(name = "time")
private Time time;
@Column(name = "diarkeia", length = 40)
private String diarkeia;
@Column(name = "tupos_vlasthshs")
private String tuposVlasthshs;
@Column(name = "tupos_oikotopou")
private String tupos_Oikotopou;
@Column(name = "epifaneia_deigmatolhpsias")
private Integer epifaneiaDeigmatolhpsias;
@Column(name = "latitude_egsa")
private Double latitudeEGSA;
@Column(name = "longitude_egsa")
private Double longitudeEGSA;
// Preferred for Google Maps Markers
@Column(name = "latitude_wgs84")
private Double latitudeWGS84;
// Preferred for Google Maps Markers
@Column(name = "longitude_wgs84")
private Double longitudeWGS84;
@Column(name = "grid_cell", length = 30)
private String gridCell;
@Column(name = "kwdikos_natura", length = 20)
private String kwdikosNatura;
@Column(name = "methodos_deigmatolhpsias")
private String methodosDeigmatolhpsias;
@Column(name = "parathrhseis")
private String parathrhseis;
@Column(name = "nomos")
private String nomos;
@Column(name = "picture")
@Lob
private Byte picture;
@Column(name = "file")
@Lob
private Byte file;
public DeigmaThhlastikwn()
public DeigmaThhlastikwn(Set<DeigmaThhlastikwnXEidh> deigmaThhlastikwnXEidhSet, Set<DeigmaThhlastikwnXPressThreats> deigmaThhlastikwnXPressThreatsSet, String kwdikosDeigmatolhpsias, String xrhmatodothsh, String ereunhths, String topothesia, Date date, Time time, String diarkeia, String tuposVlasthshs, String tupos_Oikotopou, Integer epifaneiaDeigmatolhpsias, Double latitudeEGSA, Double longitudeEGSA, Double latitudeWGS84, Double longitudeWGS84, String gridCell, String kwdikosNatura, String methodosDeigmatolhpsias, String parathrhseis, String nomos, Byte picture, Byte file)
this.deigmaThhlastikwnXEidhSet = deigmaThhlastikwnXEidhSet;
this.deigmaThhlastikwnXPressThreatsSet = deigmaThhlastikwnXPressThreatsSet;
this.kwdikosDeigmatolhpsias = kwdikosDeigmatolhpsias;
this.xrhmatodothsh = xrhmatodothsh;
this.ereunhths = ereunhths;
this.topothesia = topothesia;
this.date = date;
this.time = time;
this.diarkeia = diarkeia;
this.tuposVlasthshs = tuposVlasthshs;
this.tupos_Oikotopou = tupos_Oikotopou;
this.epifaneiaDeigmatolhpsias = epifaneiaDeigmatolhpsias;
this.latitudeEGSA = latitudeEGSA;
this.longitudeEGSA = longitudeEGSA;
this.latitudeWGS84 = latitudeWGS84;
this.longitudeWGS84 = longitudeWGS84;
this.gridCell = gridCell;
this.kwdikosNatura = kwdikosNatura;
this.methodosDeigmatolhpsias = methodosDeigmatolhpsias;
this.parathrhseis = parathrhseis;
this.nomos = nomos;
this.picture = picture;
this.file = file;
// Custom method for adding a new PressThreat for a specific DeigmaThhlastikwn allong with specifying the parent's id in the child object.
public DeigmaThhlastikwn addPressThreat(DeigmaThhlastikwnXPressThreats deigmaThhlastikwnXPressThreats)
deigmaThhlastikwnXPressThreats.setDeigmaThhlastikwn(this);
this.deigmaThhlastikwnXPressThreatsSet.add(deigmaThhlastikwnXPressThreats);
return this;
public DeigmaThhlastikwn addXEidh(DeigmaThhlastikwnXEidh deigmaThhlastikwnXEidh)
deigmaThhlastikwnXEidh.setDeigmaThhlastikwn(this);
this.deigmaThhlastikwnXEidhSet.add(deigmaThhlastikwnXEidh);
return this;
public Long getId()
return id;
public void setId(Long id)
this.id = id;
public Set<DeigmaThhlastikwnXEidh> getDeigmaThhlastikwnXEidhSet()
return deigmaThhlastikwnXEidhSet;
public void setDeigmaThhlastikwnXEidhSet(Set<DeigmaThhlastikwnXEidh> deigmaThhlastikwnXEidhSet)
this.deigmaThhlastikwnXEidhSet = deigmaThhlastikwnXEidhSet;
public Set<DeigmaThhlastikwnXPressThreats> getDeigmaThhlastikwnXPressThreatsSet()
return deigmaThhlastikwnXPressThreatsSet;
public void setDeigmaThhlastikwnXPressThreatsSet(Set<DeigmaThhlastikwnXPressThreats> deigmaThhlastikwnXPressThreatsSet)
this.deigmaThhlastikwnXPressThreatsSet = deigmaThhlastikwnXPressThreatsSet;
public String getKwdikosDeigmatolhpsias()
return kwdikosDeigmatolhpsias;
public void setKwdikosDeigmatolhpsias(String kwdikosDeigmatolhpsias)
this.kwdikosDeigmatolhpsias = kwdikosDeigmatolhpsias;
public String getXrhmatodothsh()
return xrhmatodothsh;
public void setXrhmatodothsh(String xrhmatodothsh)
this.xrhmatodothsh = xrhmatodothsh;
public String getEreunhths()
return ereunhths;
public void setEreunhths(String ereunhths)
this.ereunhths = ereunhths;
public String getTopothesia()
return topothesia;
public void setTopothesia(String topothesia)
this.topothesia = topothesia;
public Date getDate()
return date;
public void setDate(Date date)
this.date = date;
public Time getTime()
return time;
public void setTime(Time time)
this.time = time;
public String getDiarkeia()
return diarkeia;
public void setDiarkeia(String diarkeia)
this.diarkeia = diarkeia;
public String getTuposVlasthshs()
return tuposVlasthshs;
public void setTuposVlasthshs(String tuposVlasthshs)
this.tuposVlasthshs = tuposVlasthshs;
public String getTupos_Oikotopou()
return tupos_Oikotopou;
public void setTupos_Oikotopou(String tupos_Oikotopou)
this.tupos_Oikotopou = tupos_Oikotopou;
public Integer getEpifaneiaDeigmatolhpsias()
return epifaneiaDeigmatolhpsias;
public void setEpifaneiaDeigmatolhpsias(Integer epifaneiaDeigmatolhpsias)
this.epifaneiaDeigmatolhpsias = epifaneiaDeigmatolhpsias;
public Double getLatitudeEGSA()
return latitudeEGSA;
public void setLatitudeEGSA(Double latitudeEGSA)
this.latitudeEGSA = latitudeEGSA;
public Double getLongitudeEGSA()
return longitudeEGSA;
public void setLongitudeEGSA(Double longitudeEGSA)
this.longitudeEGSA = longitudeEGSA;
public Double getLatitudeWGS84()
return latitudeWGS84;
public void setLatitudeWGS84(Double latitudeWGS84)
this.latitudeWGS84 = latitudeWGS84;
public Double getLongitudeWGS84()
return longitudeWGS84;
public void setLongitudeWGS84(Double longitudeWGS84)
this.longitudeWGS84 = longitudeWGS84;
public String getGridCell()
return gridCell;
public void setGridCell(String gridCell)
this.gridCell = gridCell;
public String getKwdikosNatura()
return kwdikosNatura;
public void setKwdikosNatura(String kwdikosNatura)
this.kwdikosNatura = kwdikosNatura;
public String getMethodosDeigmatolhpsias()
return methodosDeigmatolhpsias;
public void setMethodosDeigmatolhpsias(String methodosDeigmatolhpsias)
this.methodosDeigmatolhpsias = methodosDeigmatolhpsias;
public String getParathrhseis()
return parathrhseis;
public void setParathrhseis(String parathrhseis)
this.parathrhseis = parathrhseis;
public String getNomos()
return nomos;
public void setNomos(String nomos)
this.nomos = nomos;
public Byte getPicture()
return picture;
public void setPicture(Byte picture)
this.picture = picture;
public Byte getFile()
return file;
public void setFile(Byte file)
this.file = file;
只是通知一下,添加一个新的CtThhlastikaPressThreats
已成功完成。此外,MySQL 架构是在 Run
上自动创建的。
项目的github页面在https://github.com/alexgil1994/kerkinibackend
错误的堆栈跟踪是(并且它保持相同的递归方式,我没有插入整个堆栈,因为您可以看到它已经重复自身):
2018-11-19 21:59:48.808 ERROR 1372 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.***Error] with root cause
java.lang.***Error: null
at org.springframework.data.repository.util.ClassUtils.unwrapReflectionException(ClassUtils.java:154) ~[spring-data-commons-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:646) ~[spring-data-commons-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:608) ~[spring-data-commons-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$invoke$3(RepositoryFactorySupport.java:595) ~[spring-data-commons-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:595) ~[spring-data-commons-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59) ~[spring-data-commons-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294) ~[spring-tx-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) ~[spring-tx-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:117) ~[spring-data-jpa-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93) ~[spring-aop-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61) ~[spring-data-commons-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at com.sun.proxy.$Proxy112.findCtThhlastikaPressThreatsById(Unknown Source) ~[na:na]
at sun.reflect.GeneratedMethodAccessor63.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) ~[spring-aop-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:206) ~[spring-aop-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at com.sun.proxy.$Proxy94.findCtThhlastikaPressThreatsById(Unknown Source) ~[na:na]
at com.teicm.kerkinibackend.repositories.CtThhlastikaPressThreatsRepositoryImpl.findCtThhlastikaPressThreatsById(CtThhlastikaPressThreatsRepositoryImpl.java:37) ~[classes/:na]
at com.teicm.kerkinibackend.repositories.CtThhlastikaPressThreatsRepositoryImpl$$FastClassBySpringCGLIB$$f23e415a.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746) ~[spring-aop-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) ~[spring-aop-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at com.teicm.kerkinibackend.repositories.CtThhlastikaPressThreatsRepositoryImpl$$EnhancerBySpringCGLIB$$b6491b5e.findCtThhlastikaPressThreatsById(<generated>) ~[classes/:na]
at sun.reflect.GeneratedMethodAccessor64.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:359) ~[spring-data-commons-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200) ~[spring-data-commons-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:644) ~[spring-data-commons-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:608) ~[spring-data-commons-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$invoke$3(RepositoryFactorySupport.java:595) ~[spring-data-commons-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:595) ~[spring-data-commons-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59) ~[spring-data-commons-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294) ~[spring-tx-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) ~[spring-tx-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:117) ~[spring-data-jpa-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93) ~[spring-aop-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61) ~[spring-data-commons-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at com.sun.proxy.$Proxy112.findCtThhlastikaPressThreatsById(Unknown Source) ~[na:na]
at sun.reflect.GeneratedMethodAccessor63.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) ~[spring-aop-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:206) ~[spring-aop-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at com.sun.proxy.$Proxy94.findCtThhlastikaPressThreatsById(Unknown Source) ~[na:na]
at com.teicm.kerkinibackend.repositories.CtThhlastikaPressThreatsRepositoryImpl.findCtThhlastikaPressThreatsById(CtThhlastikaPressThreatsRepositoryImpl.java:37) ~[classes/:na]
对于任何不当行为,我深表歉意,该项目处于我论文的早期阶段。
非常感谢任何帮助。
更新 - 已解决:
按照答案中的建议解决了问题。在我的一个 cmets 的正确答案下有进一步的解释。 另外,如果有人有兴趣查看所有更改的详细信息,您可以查看解决问题的提交: https://github.com/alexgil1994/kerkinibackend/commit/230c1df96623363c61317216913b310c1c7ec231
【问题讨论】:
为什么要为所有内容设置FetchType.EAGER
?我没有仔细追踪,因为您在这里有很多代码,但我怀疑您有无限的 fetch 递归。 FetchType.EAGER
通常是禁止的。仅当您专门查询获取关系时才应获取关系。
您好,FetchType.EAGER
是我试图做的其他事情留下的,删除它似乎与问题没有任何关系,错误仍然像以前一样发生。
我相信你,但我不愿意看任何有那些 fetch 类型急切的东西。代码太多了。为什么不能删除所有额外的字段并从问题的基本示例开始?
【参考方案1】:
我首先将以下内容添加到您的 application.properties 文件中,这样您就可以准确地看到调用了哪些 sql 语句。
# Log sql statements and their parameters
logging.level.org.hibernate.SQL=debug
logging.level.org.hibernate.type.descriptor.sql=trace
@JsonIgnore 不会帮助读取数据。只有当您使用 Jackson 将对象转换为 Json 时才会出现这种情况。即当您从 RestController 返回数据时。
我刚刚查看了 github 上 repo 中的 ctThhlastikaPressThreatsRepository.findCtThhlastikaPressThreatsById 方法。它似乎在调用自己,这会导致堆栈溢出。
public CtThhlastikaPressThreats findCtThhlastikaPressThreatsById(Long id)
try
// TODO: 11/17/2018 !!! -- Try as an Optional findById(id) instead!!!
return ctThhlastikaPressThreatsRepository.findCtThhlastikaPressThreatsById(id);
catch (Exception e)
throw e;
您如何使用 spring 和 jpa 非常令人困惑。我要么使用你自己的@Repository(你拥有的 Impl 版本),要么坚持扩展 CrudRepository。不要尝试两者都做。
如果你想使用 CrudRepository,你只需要拥有你已经拥有的东西。
public interface CtThhlastikaPressThreatsRepository extends CrudRepository<CtThhlastikaPressThreats, Long>, org.springframework.data.repository.Repository<CtThhlastikaPressThreats, Long>
Optional<CtThhlastikaPressThreats> findById(Long id);
CtThhlastikaPressThreats findCtThhlastikaPressThreatsById(Long id);
@Query(nativeQuery = true, value = "SELECT DISTINCT ct.act_code FROM ct_thhlastika_press_threats ct ORDER BY act_code ASC")
ArrayList<String> findDistinctByActCodeOrderByActCode();
您应该能够删除 Impl 版本并使用它。
我建议你编写一些测试类来简化调试。
例如
@RunWith(SpringJUnit4ClassRunner.class)
@DataJpaTest
public class CtThhlastikaPressThreatsRepositoryImplTest
@Autowired
private CtThhlastikaPressThreatsRepository ctThhlastikaPressThreatsRepository;
@Test
public void findByIdTest()
CtThhlastikaPressThreats pressThreats = new CtThhlastikaPressThreats();
pressThreats.setActCode("actcode");
pressThreats.setDescriptionEn("description");
pressThreats.setRemarks("remarks");
ctThhlastikaPressThreatsRepository.save(pressThreats);
CtThhlastikaPressThreats found = ctThhlastikaPressThreatsRepository.findById(pressThreats.getId()).orElse(null);
assertEquals("description", found.getDescriptionEn());
CtThhlastikaPressThreats found2 = ctThhlastikaPressThreatsRepository.findCtThhlastikaPressThreatsById(pressThreats.getId());
assertEquals("description", found2.getDescriptionEn());
@Test
public void listCodesTest()
CtThhlastikaPressThreats pressThreats = new CtThhlastikaPressThreats();
pressThreats.setActCode("actcode1");
pressThreats.setDescriptionEn("description1");
pressThreats.setRemarks("remarks1");
ctThhlastikaPressThreatsRepository.save(pressThreats);
pressThreats = new CtThhlastikaPressThreats();
pressThreats.setActCode("actcode2");
pressThreats.setDescriptionEn("description2");
pressThreats.setRemarks("remarks2");
ctThhlastikaPressThreatsRepository.save(pressThreats);
List<String> expected = Arrays.asList(new String[] "actcode1", "actcode2");
ArrayList<String> found = ctThhlastikaPressThreatsRepository.findDistinctByActCodeOrderByActCode();
assertTrue(found.containsAll(expected) && expected.containsAll(found));
【讨论】:
您好,感谢您抽出宝贵的时间来回答,我确实插入了它们并看到了工作请求的操作,例如将CtThhlastikaPressThreats
添加到数据库但对于findById(id)
或findCtThhlastikaPressThreatById(id)
或即使findAll()
请求它没有帮助,它直接进入错误。这可能意味着双向连接有问题? 不过,谢谢,我一直在寻找这种检查查询功能。请随时提出其他建议。
是的,你是对的,当我从RestController
所需的数据返回时,@JsonIgnore
帮助我解决了另一个问题,该数据也具有双向引用。但我相信这是一个不同的问题,实体 (DeigmaThhlastika
) 工作正常(即使它具有类似的双向连接。
好的,我将重构它并仅使用接口,尽管我认为它是一个更有条理的结构,但我想我错了。以这种方式尝试虽然看起来错误仍然相同,但正如您所说,当使用除自定义查询findDistinctByActCodeOrderByActCode
之外的任何find
方法时,它看起来确实会调用自身。我显然在某个地方做错了什么,但我找不到它。
我确实按照您的建议重构了整个项目,我删除了每个存储库的 RepositoryImpl 并且仅使用接口存储库。在为CtThhlastikaEidh
实施了一些工作以将其与CtThhlastikaPressThreats
进行比较后,我发现那个工作正常(它们具有基本相同的双向映射),因此需要进行更多工作并更改CtThhlastikaPressThreats
的一个注释id
专栏按预期工作!感谢您的所有建议,除了解决它之外,我还通过您的帮助学到了一些东西。开始测试!以上是关于在具有双向连接的特定实体上运行 findById(Long id) Jpa 方法时出现“java.lang.***Error: null”的主要内容,如果未能解决你的问题,请参考以下文章
有没有办法在不同网络上通过双向连接连接两台计算机(一台具有端口转发)
PostgreSQL 中具有特定模式的 Linq 和实体迁移
Hibernate:将现有子实体添加到具有 OneToMany 双向关系的新实体并将其持久化(“分离的实体传递给持久化”)