{"id":564,"date":"2009-12-28T11:25:51","date_gmt":"2009-12-28T17:25:51","guid":{"rendered":"http:\/\/www.mooreds.com\/wordpress\/archives\/000564"},"modified":"2009-12-27T11:26:24","modified_gmt":"2009-12-27T17:26:24","slug":"hibernate-booleaninteger-classcastexception","status":"publish","type":"post","link":"https:\/\/www.mooreds.com\/wordpress\/archives\/564","title":{"rendered":"Hibernate Boolean\/Integer ClassCastException"},"content":{"rendered":"<p>I ran into an issue the other day with Hibernate configuration.<\/p>\n<p>I have a bean that maps to a table in the database.\u00a0 It has a column, featured, that only has values of 0 or 1.\u00a0 For legacy reasons, we map that to an integer (turning it into a boolean is on the List Of Things To Do).<\/p>\n<p>I ran into an issue, and was getting this error in the logs:<\/p>\n<p><code \/><\/p>\n<pre>Caused by: java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.Integer\r\nat org.hibernate.type.IntegerType.set(IntegerType.java:41)\r\nat org.hibernate.type.NullableType.nullSafeSet(NullableType.java:136)\r\nat org.hibernate.type.NullableType.nullSafeSet(NullableType.java:116)\r\nat org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1698)\r\nat org.hibernate.loader.Loader.bindParameterValues(Loader.java:1669)\r\nat org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1554)\r\nat org.hibernate.loader.Loader.doQuery(Loader.java:661)\r\nat org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)\r\nat org.hibernate.loader.Loader.doList(Loader.java:2211)\r\nat org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2095)\r\nat org.hibernate.loader.Loader.list(Loader.java:2090)\r\nat org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)\r\nat org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)\r\nat org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)<\/pre>\n<p>Here's the relevant section of the mapping file:<\/p>\n<p>&lt;property name=\"featured\" type=\"integer\" columnn=\"featured\" \/&gt;<\/p>\n<p>I tried explicitly laying out the type of the SQL column:<\/p>\n<p>&lt;property name=\"featured\" type=\"integer\"&gt;<br \/>\n&lt;column sql-type=\"INTEGER\" name=\"featured\" \/&gt;<br \/>\n&lt;\/property&gt;<\/p>\n<p>But neither of these worked; I received the same error.<\/p>\n<p>I took a step back and looked at the bean.<br \/>\n<code \/><\/p>\n<pre>Integer featured;\r\n\r\npublic Integer getFeatured() {\r\nreturn featured;\r\n}\r\npublic void setFeatured(Integer featured) {\r\nthis.featured = featured;\r\n}\r\n\r\n...<\/pre>\n<p>and realized that I had added a convenience method that was confusing the Hibernate code:<br \/>\n<code \/><\/p>\n<pre>public boolean isFeatured() {\r\nreturn ...\r\n}<\/pre>\n<p>When I changed the signature of the convenience method to:<br \/>\n<code \/><\/p>\n<pre>public boolean isFeaturedListing() {\r\nreturn ...\r\n}<\/pre>\n<p>the exception went away.\u00a0 Thought I'd share for anyone searching for this stacktrace.<\/p>\n<p>[tags]javabeans[\/tags]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I ran into an issue the other day with Hibernate configuration. I have a bean that maps to a table in the database.\u00a0 It has a column, featured, that only [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,5],"tags":[],"class_list":["post-564","post","type-post","status-publish","format-standard","hentry","category-databases","category-java"],"_links":{"self":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/564","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/comments?post=564"}],"version-history":[{"count":0,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/564\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/media?parent=564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/categories?post=564"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/tags?post=564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}