Redis cache help

Mike Barnes <mike.barnes-f9aWwpgOZj5Wk0Htik3J/[email protected]> Thu, 13 Dec 2018 17:14:50 -0800
Newsgroups gmane.org.user-groups.ajug.members
Message-ID <CAA71U-70ToYMGus=kcg7DwBH3CdSt9RGPVdhmpLjObXr_PVqvg@mail.gmail.com>
--===============0642381925977693998==
Content-Type: multipart/alternative; boundary="000000000000fdd717057cf12879"

--000000000000fdd717057cf12879
Content-Type: text/plain; charset="UTF-8"

I am using spring boot 2 with hibernate and redis.
I am able to get my object into redis however when spring tries to get the
object back out I am getting the following error:

*Could not write JSON: failed to lazily initialize a collection of role:
de.codeboje.springboot.tutorials.caching.model.Location.images, could not
initialize proxy - no Session; nested exception is
com.fasterxml.jackson.databind.JsonMappingException: failed to lazily
initialize a collection of role:
de.codeboje.springboot.tutorials.caching.model.Location.images, could not
initialize proxy - no Session (through reference chain:
de.codeboje.springboot.tutorials.caching.model.Location[\"parent\"]->de.codeboje.springboot.tutorials.caching.model.Location[\"parent\"]->de.codeboje.springboot.tutorials.caching.model.Location[\"images\"]*

here is the code if someone can point me in the right direction I would
appreciate it


=========================== Code follows below ================
@Data
@ToString
@EqualsAndHashCode
@Entity(name="locationimage")
public class LocationImage implements Serializable{

/**
*
*/
private static final long serialVersionUID = -2443135589636413335L;

@JsonIgnore
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@JsonProperty(value="imageName")
private String name;
@JsonProperty(value="imageValue")
private String value;
@JsonIgnore
@EqualsAndHashCode.Exclude
private ActivityTracking tracking;
@JsonIgnore
private Boolean active;
@JsonIgnore
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "locationId")
private Location locationImage;
}
@Entity(name = "location")
@DynamicUpdate(value = true)
@Data
@Builder
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
public class Location implements Serializable {

/**
*
*/
private static final long serialVersionUID = -6772358081259494374L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String name;
private String description;
private String note;
private String accountId;
private String code;
@Builder.Default
private Boolean active = Boolean.TRUE;
private Float squareFeet;

private String constructionCode;
private String navigationCode;
private Boolean external;

private Long locationTypeId;

private String floor;

@Temporal(TemporalType.DATE)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private Date substantialCompletionDate;

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "addressId")
@EqualsAndHashCode.Exclude
private Address address;

@EqualsAndHashCode.Exclude
private ActivityTracking tracking;

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "parentId")
@EqualsAndHashCode.Exclude
private Location parent;

// line below is causing the error
@OneToMany(mappedBy = "locationImage", fetch = FetchType.LAZY)
@Builder.Default
@EqualsAndHashCode.Exclude
private Set<LocationImage> images = new HashSet<LocationImage>();

}
@Service
@Slf4j
public class LocationService {
@Autowired
LocationRepository locationRepository;
@Cacheable("location")
public Location findById(Long id) {
log.info("findById called for id {}", id);
return locationRepository.findOne(id);
}

@CacheEvict(value = "location", allEntries = true)
public void clearCache() {
log.info("Clearing location cache");
}
}

=================== End of code =================

Thanks in advance for any help
-- 

Michael D. Barnes

Sr Software Engineer

Office: +1 404-960-9621

E-mail: mike.barnes-f9aWwpgOZj5Wk0Htik3J/[email protected]

Web: www.site1001.com

Follow Site 1001 on: LinkedIn <https://linkedin.com/company/site1001> |
Instagram <http://instagram.com/site1001> | Twitter
<https://twitter.com/site1001> | Facebook <http://www.facebook.com/site1001>
| YouTube <https://www.youtube.com/channel/UCmQBqlisUgdd0HjQRVFD6WQ>
20772 Carmen Loop, Suite 130
<https://www.google.com/maps/place/20772+Carmen+Loop,+Suite+130+Bend,+OR+97702>

Bend, OR 97702
<https://www.google.com/maps/place/20772+Carmen+Loop,+Suite+130+Bend,+OR+97702>

--000000000000fdd717057cf12879
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr"><div di=
r=3D"ltr">I am using spring boot 2 with hibernate and redis.<div>I am able =
to get my object into redis however when spring tries to get the object bac=
k out I am getting the following error:</div><div><br></div><div><i>Could n=
ot write JSON: failed to lazily initialize a collection of role: de.codeboj=
e.springboot.tutorials.caching.model.Location.images, could not initialize =
proxy - no Session; nested exception is com.fasterxml.jackson.databind.Json=
MappingException: failed to lazily initialize a collection of role: de.code=
boje.springboot.tutorials.caching.model.Location.images, could not initiali=
ze proxy - no Session (through reference chain: de.codeboje.springboot.tuto=
rials.caching.model.Location[\&quot;parent\&quot;]-&gt;de.codeboje.springbo=
ot.tutorials.caching.model.Location[\&quot;parent\&quot;]-&gt;de.codeboje.s=
pringboot.tutorials.caching.model.Location[\&quot;images\&quot;]</i><br></d=
iv><div><div><br></div><div>here is the code if someone can point me in the=
 right direction I would appreciate it</div><div><br></div><div><br></div><=
div>=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D Code follows below =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D</div><div><div>@Data</div><div>@ToString</div><div>@EqualsAndHashCod=
e</div><div>@Entity(name=3D&quot;locationimage&quot;)</div><div>public clas=
s LocationImage implements Serializable{</div><div><br></div><div><span sty=
le=3D"white-space:pre">	</span>/**</div><div><span style=3D"white-space:pre=
">	</span> *=C2=A0</div><div><span style=3D"white-space:pre">	</span> */</d=
iv><div><span style=3D"white-space:pre">	</span>private static final long s=
erialVersionUID =3D -2443135589636413335L;</div><div><br></div><div><span s=
tyle=3D"white-space:pre">	</span>@JsonIgnore</div><div><span style=3D"white=
-space:pre">	</span>@Id</div><div><span style=3D"white-space:pre">	</span>@=
GeneratedValue(strategy =3D GenerationType.IDENTITY)</div><div><span style=
=3D"white-space:pre">	</span>private Long id;</div><div><span style=3D"whit=
e-space:pre">	</span>@JsonProperty(value=3D&quot;imageName&quot;)</div><div=
><span style=3D"white-space:pre">	</span>private String name;</div><div><sp=
an style=3D"white-space:pre">	</span>@JsonProperty(value=3D&quot;imageValue=
&quot;)</div><div><span style=3D"white-space:pre">	</span>private String va=
lue;</div><div><span style=3D"white-space:pre">	</span>@JsonIgnore</div><di=
v><span style=3D"white-space:pre">	</span>@EqualsAndHashCode.Exclude</div><=
div><span style=3D"white-space:pre">	</span>private ActivityTracking tracki=
ng;</div><div><span style=3D"white-space:pre">	</span>@JsonIgnore</div><div=
><span style=3D"white-space:pre">	</span>private Boolean active;</div><div>=
<span style=3D"white-space:pre">	</span></div><div><span style=3D"white-spa=
ce:pre">	</span>@JsonIgnore</div><div><span style=3D"white-space:pre">	</sp=
an>@ManyToOne(cascade =3D CascadeType.ALL)</div><div><span style=3D"white-s=
pace:pre">	</span>@JoinColumn(name =3D &quot;locationId&quot;)</div><div><s=
pan style=3D"white-space:pre">	</span>private Location locationImage;</div>=
<div>}</div></div><div><div>@Entity(name =3D &quot;location&quot;)</div><di=
v>@DynamicUpdate(value =3D true)</div><div>@Data</div><div>@Builder</div><d=
iv>@EqualsAndHashCode</div><div>@NoArgsConstructor</div><div>@AllArgsConstr=
uctor</div><div>public class Location implements Serializable {</div><div><=
br></div><div><span style=3D"white-space:pre">	</span>/**</div><div><span s=
tyle=3D"white-space:pre">	</span> *=C2=A0</div><div><span style=3D"white-sp=
ace:pre">	</span> */</div><div><span style=3D"white-space:pre">	</span>priv=
ate static final long serialVersionUID =3D -6772358081259494374L;</div><div=
><span style=3D"white-space:pre">	</span>@Id</div><div><span style=3D"white=
-space:pre">	</span>@GeneratedValue(strategy =3D GenerationType.IDENTITY)</=
div><div><span style=3D"white-space:pre">	</span>private Long id;</div><div=
><br></div><div><span style=3D"white-space:pre">	</span>private String name=
;</div><div><span style=3D"white-space:pre">	</span>private String descript=
ion;</div><div><span style=3D"white-space:pre">	</span>private String note;=
</div><div><span style=3D"white-space:pre">	</span>private String accountId=
;</div><div><span style=3D"white-space:pre">	</span>private String code;</d=
iv><div><span style=3D"white-space:pre">	</span>@Builder.Default</div><div>=
<span style=3D"white-space:pre">	</span>private Boolean active =3D Boolean.=
TRUE;</div><div><span style=3D"white-space:pre">	</span>private Float squar=
eFeet;</div><div><br></div><div><span style=3D"white-space:pre">	</span>pri=
vate String constructionCode;</div><div><span style=3D"white-space:pre">	</=
span>private String navigationCode;</div><div><span style=3D"white-space:pr=
e">	</span>private Boolean external;</div><div><br></div><div><span style=
=3D"white-space:pre">	</span>private Long locationTypeId;</div><div><br></d=
iv><div><span style=3D"white-space:pre">	</span>private String floor;</div>=
<div><br></div><div><span style=3D"white-space:pre">	</span>@Temporal(Tempo=
ralType.DATE)</div><div><span style=3D"white-space:pre">	</span>@JsonFormat=
(shape =3D JsonFormat.Shape.STRING, pattern =3D &quot;yyyy-MM-dd&quot;)</di=
v><div><span style=3D"white-space:pre">	</span>private Date substantialComp=
letionDate;</div><div><br></div><div><span style=3D"white-space:pre">	</spa=
n>@ManyToOne(cascade =3D CascadeType.ALL)</div><div><span style=3D"white-sp=
ace:pre">	</span>@JoinColumn(name =3D &quot;addressId&quot;)</div><div><spa=
n style=3D"white-space:pre">	</span>@EqualsAndHashCode.Exclude</div><div><s=
pan style=3D"white-space:pre">	</span>private Address address;</div><div><b=
r></div><div><span style=3D"white-space:pre">	</span>@EqualsAndHashCode.Exc=
lude</div><div><span style=3D"white-space:pre">	</span>private ActivityTrac=
king tracking;</div><div><br></div><div><span style=3D"white-space:pre">	</=
span>@ManyToOne(cascade =3D CascadeType.ALL)</div><div><span style=3D"white=
-space:pre">	</span>@JoinColumn(name =3D &quot;parentId&quot;)</div><div><s=
pan style=3D"white-space:pre">	</span>@EqualsAndHashCode.Exclude</div><div>=
<span style=3D"white-space:pre">	</span>private Location parent;</div><div>=
<br></div><div><span style=3D"white-space:pre">	</span>// line below is cau=
sing the error</div><div><span style=3D"white-space:pre">	</span>@OneToMany=
(mappedBy =3D &quot;locationImage&quot;, fetch =3D FetchType.LAZY)</div><di=
v><span style=3D"white-space:pre">	</span>@Builder.Default</div><div><span =
style=3D"white-space:pre">	</span>@EqualsAndHashCode.Exclude</div><div><spa=
n style=3D"white-space:pre">	</span>private Set&lt;LocationImage&gt; images=
 =3D new HashSet&lt;LocationImage&gt;();</div><div><br></div><div>}</div></=
div><div><div>@Service</div><div>@Slf4j</div><div>public class LocationServ=
ice {</div><div><span style=3D"white-space:pre">	</span></div><div><span st=
yle=3D"white-space:pre">	</span>@Autowired</div><div><span style=3D"white-s=
pace:pre">	</span>LocationRepository locationRepository;</div><div><span st=
yle=3D"white-space:pre">	</span></div><div><span style=3D"white-space:pre">=
	</span>@Cacheable(&quot;location&quot;)</div><div><span style=3D"white-spa=
ce:pre">	</span>public Location findById(Long id) {</div><div><span style=
=3D"white-space:pre">		</span><a href=3D"http://log.info">log.info</a>(&quo=
t;findById called for id {}&quot;, id);</div><div><span style=3D"white-spac=
e:pre">		</span>return locationRepository.findOne(id);</div><div><span styl=
e=3D"white-space:pre">	</span>}</div><div><br></div><div><span style=3D"whi=
te-space:pre">	</span>@CacheEvict(value =3D &quot;location&quot;, allEntrie=
s =3D true)</div><div><span style=3D"white-space:pre">	</span>public void c=
learCache() {</div><div><span style=3D"white-space:pre">		</span><a href=3D=
"http://log.info">log.info</a>(&quot;Clearing location cache&quot;);</div><=
div><span style=3D"white-space:pre">	</span>}</div><div>}</div></div><div><=
br></div><div>=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D End=
 of code =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D</div><div><br>=
</div><div>Thanks in advance for any help</div>-- <br><div dir=3D"ltr" clas=
s=3D"gmail_signature"><div dir=3D"ltr"><p dir=3D"ltr" style=3D"line-height:=
1.38;margin-top:0pt;margin-bottom:0pt"><span style=3D"font-size:9.5pt;font-=
family:Verdana;color:rgb(68,68,68);background-color:rgb(255,255,255);font-w=
eight:700;font-style:normal;font-variant:normal;text-decoration:none;vertic=
al-align:baseline">Michael D. Barnes<br></span></p><p dir=3D"ltr" style=3D"=
line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style=3D"font-size=
:9.5pt;font-family:Verdana;color:rgb(68,68,68);background-color:transparent=
;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none=
;vertical-align:baseline">Sr Software Engineer<br></span></p><p dir=3D"ltr"=
 style=3D"line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style=3D=
"font-size:9.5pt;font-family:Verdana;color:rgb(68,68,68);background-color:t=
ransparent;font-weight:400;font-style:normal;font-variant:normal;text-decor=
ation:none;vertical-align:baseline"><img src=3D"https://lh4.googleuserconte=
nt.com/Sp9xnvepxJBESkxLRN40IGyXFPHgUMicv8nVNkx8viNjvY7U7ITrO50HyJSkzL-8Z1Wj=
vjTrBm2sS6f-aiJw4Yh5EYTYJW3Cn41oKj5tYSERDj2Valjzuffry1aNsBvjbIdCC4c4" style=
=3D"border: none;" width=3D"146" height=3D"35"></span></p><p dir=3D"ltr" st=
yle=3D"line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style=3D"fo=
nt-size:9.5pt;font-family:Verdana;color:rgb(68,68,68);background-color:tran=
sparent;font-weight:400;font-style:normal;font-variant:normal;text-decorati=
on:none;vertical-align:baseline">Office: +1 404-960-9621<br></span></p><p d=
ir=3D"ltr" style=3D"line-height:1.38;margin-top:0pt;margin-bottom:0pt"><spa=
n style=3D"font-size:9.5pt;font-family:Verdana;color:rgb(68,68,68);backgrou=
nd-color:transparent;font-weight:400;font-style:normal;font-variant:normal;=
text-decoration:none;vertical-align:baseline">E-mail: </span><span style=3D=
"font-size:9.5pt;font-family:Verdana;color:rgb(17,85,204);background-color:=
transparent;font-weight:400;font-style:normal;font-variant:normal;text-deco=
ration:none;vertical-align:baseline"><a href=3D"mailto:mike.barnes@site1001=
.com" target=3D"_blank">mike.barnes-f9aWwpgOZj5Wk0Htik3J/[email protected]</a></span></p><p dir=3D"lt=
r" style=3D"line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style=
=3D"font-size:9.5pt;font-family:Verdana;color:rgb(68,68,68);background-colo=
r:transparent;font-weight:400;font-style:normal;font-variant:normal;text-de=
coration:none;vertical-align:baseline">Web: </span><a href=3D"http://www.si=
te1001.com/" style=3D"text-decoration:none" target=3D"_blank"><span style=
=3D"font-size:9.5pt;font-family:Verdana;color:rgb(17,85,204);background-col=
or:transparent;font-weight:400;font-style:normal;font-variant:normal;text-d=
ecoration:underline;vertical-align:baseline">www.site1001.com</span></a></p=
><br><p dir=3D"ltr" style=3D"line-height:1.38;margin-top:0pt;margin-bottom:=
0pt"><span style=3D"font-size:10pt;font-family:Arial;color:rgb(34,34,34);ba=
ckground-color:transparent;font-weight:700;font-style:normal;font-variant:n=
ormal;text-decoration:none;vertical-align:baseline">Follow Site 1001 on: </=
span><a href=3D"https://linkedin.com/company/site1001" style=3D"text-decora=
tion:none" target=3D"_blank"><span style=3D"font-size:10pt;font-family:Aria=
l;color:rgb(17,85,204);background-color:transparent;font-weight:700;font-st=
yle:normal;font-variant:normal;text-decoration:underline;vertical-align:bas=
eline">LinkedIn</span></a><span style=3D"font-size:10pt;font-family:Arial;c=
olor:rgb(34,34,34);background-color:transparent;font-weight:700;font-style:=
normal;font-variant:normal;text-decoration:none;vertical-align:baseline"> |=
 </span><a href=3D"http://instagram.com/site1001" style=3D"text-decoration:=
none" target=3D"_blank"><span style=3D"font-size:10pt;font-family:Arial;col=
or:rgb(17,85,204);background-color:transparent;font-weight:700;font-style:n=
ormal;font-variant:normal;text-decoration:underline;vertical-align:baseline=
">Instagram</span></a><span style=3D"font-size:10pt;font-family:Arial;color=
:rgb(34,34,34);background-color:transparent;font-weight:700;font-style:norm=
al;font-variant:normal;text-decoration:none;vertical-align:baseline"> | </s=
pan><a href=3D"https://twitter.com/site1001" style=3D"text-decoration:none"=
 target=3D"_blank"><span style=3D"font-size:10pt;font-family:Arial;color:rg=
b(17,85,204);background-color:transparent;font-weight:700;font-style:normal=
;font-variant:normal;text-decoration:underline;vertical-align:baseline">Twi=
tter</span></a><span style=3D"font-size:10pt;font-family:Arial;color:rgb(34=
,34,34);background-color:transparent;font-weight:700;font-style:normal;font=
-variant:normal;text-decoration:none;vertical-align:baseline"> | </span><a =
href=3D"http://www.facebook.com/site1001" style=3D"text-decoration:none" ta=
rget=3D"_blank"><span style=3D"font-size:10pt;font-family:Arial;color:rgb(1=
7,85,204);background-color:transparent;font-weight:700;font-style:normal;fo=
nt-variant:normal;text-decoration:underline;vertical-align:baseline">Facebo=
ok</span></a><span style=3D"font-size:10pt;font-family:Arial;color:rgb(34,3=
4,34);background-color:transparent;font-weight:700;font-style:normal;font-v=
ariant:normal;text-decoration:none;vertical-align:baseline"> | </span><a hr=
ef=3D"https://www.youtube.com/channel/UCmQBqlisUgdd0HjQRVFD6WQ" style=3D"te=
xt-decoration:none" target=3D"_blank"><span style=3D"font-size:10pt;font-fa=
mily:Arial;color:rgb(17,85,204);background-color:transparent;font-weight:70=
0;font-style:normal;font-variant:normal;text-decoration:underline;vertical-=
align:baseline">YouTube</span></a></p><a href=3D"https://www.google.com/map=
s/place/20772+Carmen+Loop,+Suite+130+Bend,+OR+97702" style=3D"text-decorati=
on:none" target=3D"_blank"><span style=3D"font-size:9pt;font-family:Verdana=
;color:rgb(17,85,204);background-color:transparent;font-weight:400;font-sty=
le:normal;font-variant:normal;text-decoration:underline;vertical-align:base=
line">20772 Carmen Loop, Suite 130</span></a><p dir=3D"ltr" style=3D"line-h=
eight:1.38;margin-top:0pt;margin-bottom:0pt"><a href=3D"https://www.google.=
com/maps/place/20772+Carmen+Loop,+Suite+130+Bend,+OR+97702" style=3D"text-d=
ecoration:none" target=3D"_blank"><span style=3D"font-size:9pt;font-family:=
Verdana;color:rgb(17,85,204);background-color:transparent;font-weight:400;f=
ont-style:normal;font-variant:normal;text-decoration:underline;vertical-ali=
gn:baseline">Bend, OR 97702</span></a></p></div></div></div></div></div></d=
iv></div></div>

--000000000000fdd717057cf12879--

--===============0642381925977693998==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: inline

X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KYWp1Zy1tZW1i
ZXJzIG1haWxpbmcgbGlzdAphanVnLW1lbWJlcnNAbGlzdHMuYWp1Zy5vcmcKTWFuYWdlIHlvdXIg
c3Vic2NyaXB0aW9uIGFuZCB1bnN1YnNjcmliZSBodHRwOi8vbGlzdHMuYWp1Zy5vcmcvbWFpbG1h
bi9saXN0aW5mby9hanVnLW1lbWJlcnM=

--===============0642381925977693998==--