{"id":325,"date":"2013-11-05T11:54:07","date_gmt":"2013-11-05T11:54:07","guid":{"rendered":"http:\/\/www.andreagirardi.it\/blog\/?p=325"},"modified":"2013-11-13T10:33:16","modified_gmt":"2013-11-13T10:33:16","slug":"spring-data-cannot-use-a-complex-object-as-a-key-value","status":"publish","type":"post","link":"https:\/\/www.andreagirardi.it\/blog\/spring-data-cannot-use-a-complex-object-as-a-key-value\/","title":{"rendered":"Spring-data: Cannot use a complex object as a key value"},"content":{"rendered":"<p>I was trying to figured out how to solve this issue. Basically I&#8217;m saving a user profile bean that contains multiple occurrences of other beans inside him. Something like date:<\/p>\n<pre lang=\"java\">public class UserProfile {\r\n\r\n\tList<Class1> classes1;\r\n\tList<Class2> classes2;\r\n\t\r\n\tInteger int1;\r\n\tMap<String, Class3> classes3;\r\n\r\n}<\/pre>\n<p>I was working fine until I changed a getter adding some business functionalities and surrounding all lines with a try \/ catch. Of course, I putted the exception message on the log using adding this definition inside the bean:<\/p>\n<pre lang=\"java\">protected final Log logger = LogFactory.getLog(getClass());<\/pre>\n<p>Good point for me, everything worked fine until I updated the object in Mongo. Holy crap, I got the &#8220;Cannot use a complex object as a key value&#8221; exception and I spent two hours trying to change the beans (of course I did a lot of other changes after the fatal adding of business logic inside my bean). Exhausted, I contacted my colleague Max (<a href=\"https:\/\/twitter.com\/maxfarnea\">@maxfarnea<\/a>) and he told me, &#8220;when I got this error, I removed the log inside a bean&#8221;. Ipso facto, I removed the log entry from my bean, added a throws exception instead of a try \/ catch and tested! Well done <a href=\"https:\/\/twitter.com\/maxfarnea\">@maxfarnea<\/a>, it works! <\/p>\n<p>I learned two lesson today: one, don&#8217;t put log into beans that need to be stored using spring-data and, two, if you are in pain, ask, talk, don&#8217;t be silly, don&#8217;t waste your time!!! <a href=\"http:\/\/stackoverflow.com\">Stackoverflow.com<\/a> is a good point to start (and my blog either, of course) but never is more helpful than a quick chat with a colleague! <\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-linkedin\"><a rel=\"nofollow\" data-shared=\"sharing-linkedin-325\" class=\"share-linkedin sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/spring-data-cannot-use-a-complex-object-as-a-key-value\/?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\"><span>LinkedIn<\/span><\/a><\/li><li class=\"share-reddit\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-reddit sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/spring-data-cannot-use-a-complex-object-as-a-key-value\/?share=reddit\" target=\"_blank\" title=\"Click to share on Reddit\"><span>Reddit<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow\" data-shared=\"sharing-twitter-325\" class=\"share-twitter sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/spring-data-cannot-use-a-complex-object-as-a-key-value\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\"><span>Twitter<\/span><\/a><\/li><li class=\"share-facebook\"><a rel=\"nofollow\" data-shared=\"sharing-facebook-325\" class=\"share-facebook sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/spring-data-cannot-use-a-complex-object-as-a-key-value\/?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\"><span>Facebook<\/span><\/a><\/li><li class=\"share-skype\"><a rel=\"nofollow\" data-shared=\"sharing-skype-325\" class=\"share-skype sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/spring-data-cannot-use-a-complex-object-as-a-key-value\/?share=skype\" target=\"_blank\" title=\"Share on Skype\"><span>Skype<\/span><\/a><\/li><li class=\"share-pocket\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-pocket sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/spring-data-cannot-use-a-complex-object-as-a-key-value\/?share=pocket\" target=\"_blank\" title=\"Click to share on Pocket\"><span>Pocket<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>I was trying to figured out how to solve this issue. Basically I&#8217;m saving a user profile bean that contains multiple occurrences of other beans inside him. Something like date: public class UserProfile { List classes1; List classes2; Integer int1; Map classes3; } I was working fine until I changed a getter adding some business [&hellip;]<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-linkedin\"><a rel=\"nofollow\" data-shared=\"sharing-linkedin-325\" class=\"share-linkedin sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/spring-data-cannot-use-a-complex-object-as-a-key-value\/?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\"><span>LinkedIn<\/span><\/a><\/li><li class=\"share-reddit\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-reddit sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/spring-data-cannot-use-a-complex-object-as-a-key-value\/?share=reddit\" target=\"_blank\" title=\"Click to share on Reddit\"><span>Reddit<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow\" data-shared=\"sharing-twitter-325\" class=\"share-twitter sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/spring-data-cannot-use-a-complex-object-as-a-key-value\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\"><span>Twitter<\/span><\/a><\/li><li class=\"share-facebook\"><a rel=\"nofollow\" data-shared=\"sharing-facebook-325\" class=\"share-facebook sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/spring-data-cannot-use-a-complex-object-as-a-key-value\/?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\"><span>Facebook<\/span><\/a><\/li><li class=\"share-skype\"><a rel=\"nofollow\" data-shared=\"sharing-skype-325\" class=\"share-skype sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/spring-data-cannot-use-a-complex-object-as-a-key-value\/?share=skype\" target=\"_blank\" title=\"Share on Skype\"><span>Skype<\/span><\/a><\/li><li class=\"share-pocket\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-pocket sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/spring-data-cannot-use-a-complex-object-as-a-key-value\/?share=pocket\" target=\"_blank\" title=\"Click to share on Pocket\"><span>Pocket<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"footnotes":""},"categories":[14],"tags":[4,32,13],"_links":{"self":[{"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/posts\/325"}],"collection":[{"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/comments?post=325"}],"version-history":[{"count":1,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/posts\/325\/revisions"}],"predecessor-version":[{"id":326,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/posts\/325\/revisions\/326"}],"wp:attachment":[{"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/media?parent=325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/categories?post=325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/tags?post=325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}