{"id":276,"date":"2024-03-05T18:42:24","date_gmt":"2024-03-05T17:42:24","guid":{"rendered":"https:\/\/fixthebugs.com\/?p=276"},"modified":"2024-03-05T18:43:26","modified_gmt":"2024-03-05T17:43:26","slug":"jpa-could-not-resolve-target-entity","status":"publish","type":"post","link":"https:\/\/fixthebugs.com\/index.php\/2024\/03\/05\/jpa-could-not-resolve-target-entity\/","title":{"rendered":"JPA: Could not resolve target entity"},"content":{"rendered":"\n<p><strong>Problem<\/strong>:  When you are trying to use @Query and returns next error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Could not resolve target entity<\/code><\/pre>\n\n\n\n<p><strong>Solution<\/strong>: You have to use as table name, the name of the Java class. Check if it is correct in the @Query and check that you are not using the database name of the table. <strong>YOU HAVE TO USE THE JAVA&#8217;S CLASS NAME OF THE ENTITY.<\/strong><\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/This is the repository class\n@Entity\n@Table(name = \"CAR_TABLE\")\nclass <strong>CarDto <\/strong>{\n String colour;\n String numberOfDoors;\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>@Repository\npublic interface CarRepository extends JpaRepository&lt;CarDto, String&gt; {\n\n\/\/Wrong, the name 'Cor' is not correct\n@Modifying\n@Query(\"update Cor c set c.color = :color where c.numberOfDoors = 5\")\nvoid wrongMethod1(@Param(\"color\") String color);\n\n\/\/Wrong, the name 'CAR_TABLE' could be the name of the table in database, but you don't have to use it\n@Modifying\n@Query(\"update CAR_TABLE c set c.color = :color where c.numberOfDoors = 5\")\nvoid wrongMethod2(@Param(\"color\") String color);\n\n<strong>\/\/Correct<\/strong>\n@Modifying\n@Query(\"update <strong>CarDto <\/strong>c set c.color = :color where c.numberOfDoors = 5\")\nvoid correctMethod(@Param(\"color\") String color);\n}<\/code><\/pre>\n\n\n\n<p>Source: <x> <a href=\"https:\/\/docs.spring.io\/spring-data\/jpa\/reference\/jpa\/query-methods.html#jpa.query-methods.at-query\" rel=\"dofollow noopener\" target=\"_blank\">documentation<\/a><\/x><\/p>\n\n\n\n<p>For more interesting tutorials &amp; guides just check them&nbsp;<a rel=\"dofollow\" href=\"https:\/\/fixthebugs.com\/index.php\/category\/tutorials-guides\/\">HERE<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: When you are trying to use @Query and returns next error: Solution: You have to use as table name, the name of the Java class. Check if it is correct in the @Query and check that you are not using the database name of the table. YOU HAVE TO USE THE JAVA&#8217;S CLASS NAME [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":278,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[9,11],"tags":[],"_links":{"self":[{"href":"https:\/\/fixthebugs.com\/index.php\/wp-json\/wp\/v2\/posts\/276"}],"collection":[{"href":"https:\/\/fixthebugs.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fixthebugs.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fixthebugs.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fixthebugs.com\/index.php\/wp-json\/wp\/v2\/comments?post=276"}],"version-history":[{"count":3,"href":"https:\/\/fixthebugs.com\/index.php\/wp-json\/wp\/v2\/posts\/276\/revisions"}],"predecessor-version":[{"id":282,"href":"https:\/\/fixthebugs.com\/index.php\/wp-json\/wp\/v2\/posts\/276\/revisions\/282"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/fixthebugs.com\/index.php\/wp-json\/wp\/v2\/media\/278"}],"wp:attachment":[{"href":"https:\/\/fixthebugs.com\/index.php\/wp-json\/wp\/v2\/media?parent=276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fixthebugs.com\/index.php\/wp-json\/wp\/v2\/categories?post=276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fixthebugs.com\/index.php\/wp-json\/wp\/v2\/tags?post=276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}