{"id":356,"date":"2026-02-10T16:19:32","date_gmt":"2026-02-10T22:19:32","guid":{"rendered":"https:\/\/www.p14nd4.com\/blog\/?p=356"},"modified":"2026-02-10T16:25:26","modified_gmt":"2026-02-10T22:25:26","slug":"solved-hibernate-not-autoscanning-entity-classes","status":"publish","type":"post","link":"http:\/\/www.p14nd4.com\/blog\/2026\/02\/10\/solved-hibernate-not-autoscanning-entity-classes\/","title":{"rendered":"Solved: Hibernate not autoscanning entity classes"},"content":{"rendered":"\n<p>I recently had to update an old Java (.war) JPA\/Hibernate project (from Hibernate 5.4 to 7.2, among other things), and ran into <code>org.hibernate.query.sqm.UnknownEntityException: Could not resolve root entity 'Foo'<\/code> issues at runtime. (My classes were annotated with <code>@Entity<\/code> and named correctly.) Relatedly, when I tried to set <code>jakarta.persistence.schema-generation.database.action<\/code> to <code>CREATE<\/code> or similar (e.g., for testing in a new environment), it wouldn&#8217;t generate my tables.<\/p>\n\n\n\n<p>This <em>used<\/em> to work, and while I discovered that explicitly adding <code>&lt;class>org.example.Foo.Bar&lt;\/class><\/code>-type entries to my <code>persistence.xml<\/code> worked around the issue, that was dissatisfying, since everything indicated this should &#8220;just work&#8221; (as it had before), and I&#8217;d rather avoid adding additional touch-points that inevitably will be missed when some new <code>@Entity<\/code> class is added in the future.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Several proposed solutions didn&#8217;t work for me, complicated by the (apparently unusual?) situation that my project builds a <code>.war<\/code> file with all of my code compiled into <code>.class<\/code> files nested under <code>WEB-INF\/classes<\/code> instead of a <code>.jar<\/code> file that could be succinctly referenced in a <code>&lt;jar-file><\/code> element of <code>persistence.xml<\/code>.<\/p>\n\n\n\n<p>Along the way, I found many guides explaining that the Persistence \/ EntityManager provider should automatically scan \/ include <code>@Entity<\/code> classes found under a <code>PersistenceUnit<\/code>&#8216;s &#8220;root&#8221;, without explaining what that was. I eventually turned to the <a href=\"https:\/\/jakarta.ee\/specifications\/persistence\/3.2\/jakarta-persistence-spec-3.2#a12236\">JPA 3.2 specification \u00a78.2<\/a> to learn that, in my case, the root was &#8220;the WEB-INF\/classes directory of a WAR file&#8221;. (My .war contains all of my classes as, e.g., <code>WEB-INF\/classes\/org\/example\/Foo\/Bar.class<\/code> files.)<\/p>\n\n\n\n<p>Long story short, the suggestion found several places of adding <code>&lt;property name=\"hibernate.archive.autodetection\" value=\"class\" \/><\/code> to <code>persistence.xml<\/code> was close, but, at least in my scenario, missed an important piece. (Many had actually unnecessarily suggested <code>\"class, hbm\"<\/code> as a value; while the additional &#8216;<code>, hbm<\/code>&#8216; shouldn&#8217;t break things, none of the people had expressed that their <code>hbm.xml<\/code> files were being ignored, so it seems like ignorant advice.) Due to poor formatting of the Hibernate documentation, I didn&#8217;t see a critical addendum:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-style-default is-layout-flow wp-block-quote-is-layout-flow\" style=\"font-size:1.7rem\">\n<p>When <code>.class<\/code> discovery is enabled, the module <code>hibernate-scan-jandex<\/code> must be added as a dependency, or some other implementation of the service <code>ScannerFactory<\/code> must be made available.<\/p>\n<cite><a href=\"https:\/\/docs.hibernate.org\/orm\/7.2\/userguide\/html_single\/#settings-hibernate.archive.autodetection\">https:\/\/docs.hibernate.org\/orm\/7.2\/userguide\/html_single\/#settings-hibernate.archive.autodetection<\/a><\/cite><\/blockquote>\n\n\n\n<p>Sure enough, once I <a href=\"https:\/\/gitlab.com\/p14nd4\/examplewebservice\/-\/commit\/c51cb0e9fa29fd939507864052a1a82efbd3f87f\">added <code>hibernate-scan-jandex<\/code> as a dependency to my <code>pom.xml<\/code><\/a> (and re-enabled a previously failed attempt at adding <code>&lt;property name=\"hibernate.archive.autodetection\" value=\"class\" \/><\/code> to <code>persistence.xml<\/code>), everything started to work again!<\/p>\n\n\n\n<p>Thanks to dreamreal from the Libera.chat #java IRC channel for asking good questions and willingness to help.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Fix hibernate UnkownEntityException \/ not autoscanning for annotated @Entity classes<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[17,33,19],"class_list":["post-356","post","type-post","status-publish","format-standard","hentry","category-technical","tag-error","tag-java","tag-solved"],"_links":{"self":[{"href":"http:\/\/www.p14nd4.com\/blog\/wp-json\/wp\/v2\/posts\/356","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.p14nd4.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.p14nd4.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.p14nd4.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.p14nd4.com\/blog\/wp-json\/wp\/v2\/comments?post=356"}],"version-history":[{"count":4,"href":"http:\/\/www.p14nd4.com\/blog\/wp-json\/wp\/v2\/posts\/356\/revisions"}],"predecessor-version":[{"id":360,"href":"http:\/\/www.p14nd4.com\/blog\/wp-json\/wp\/v2\/posts\/356\/revisions\/360"}],"wp:attachment":[{"href":"http:\/\/www.p14nd4.com\/blog\/wp-json\/wp\/v2\/media?parent=356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.p14nd4.com\/blog\/wp-json\/wp\/v2\/categories?post=356"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.p14nd4.com\/blog\/wp-json\/wp\/v2\/tags?post=356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}