title: "QuerySyntaxException: User is not mapped" when adding an entity from another package
body: <p>I've made a Hibernate configuration maven project (built with the maven-shade-plugin) to allow my codebase to use one unified "database class". However, the class does not seem to map (throws a <code>org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped</code> exception at runtime) when called from outside the running project.</p>
  
  <p>When ran within the project, everything works fine. However, Hibernate fails to map the entity when run from outside the project.</p>
  
  <hr>
  
  <p>CommonDB.java (part of the maven package meta1203-data)</p>
  
  <pre class="lang-java prettyprint-override"><code>package com.meta1203.microservices;
  
  import java.util.List;
  
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.hibernate.cfg.Configuration;
  
  public class CommonDB &lt;C extends BaseEntity&gt; {
      private Configuration cfg;
      private SessionFactory sf;
      private Session session;
      private Class&lt;C&gt; c;
  
      public CommonDB(Class&lt;C&gt; anoClass) {
          String jdbcUrl = String.format(
                  "jdbc:mysql://%s/%s",
                  System.getenv("DB_URL"),
                  System.getenv("DB_NAME"));
  
          cfg = new Configuration()
                  .setProperty("hibernate.connection.url", jdbcUrl)
                  .setProperty("hibernate.connection.username", System.getenv("DB_USERNAME"))
                  .setProperty("hibernate.connection.password", System.getenv("DB_PASSWORD"))
  
                  .setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver")
                  .setProperty("hibernate.connection.pool_size", "1")
                  .setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect")
                  .setProperty("hibernate.hbm2ddl.auto", "update")
                  .setProperty("hibernate.show_sql", "true")
                  .addAnnotatedClass(anoClass.getClass());
  
          c = anoClass;
      }
  
      public void open() {
          sf = cfg.buildSessionFactory();
          session = sf.openSession();
      }
  
      public void close() {
          session.close();
          sf.close();
      }
  
      public C findOneBy(String field, String o) {
          String query = "select u from " + c.getSimpleName() + " u where u." + field + " = :id";
  
          return session.createQuery(query, c).setParameter("id", o).getSingleResult();
      }
   // other CRUD functions
  }
  
  </code></pre>
  
  <p>TestDB.java (part of the maven package meta1203-userservice)</p>
  
  <pre class="lang-java prettyprint-override"><code>package com.meta1203.microservices.user;
  
  import com.meta1203.microservices.CommonDB;
  import com.meta1203.microservices.user.model.User;
  
  public class TestDB {
      public static void main(String[] args) {
          TestDB tdb = new TestDB();
          CommonDB&lt;User&gt; db = new CommonDB&lt;User&gt;(User.class);
          db.open();
  
          User hunter = db.findOneBy("username", "hunter");
          System.out.println(hunter.getUsername());
          System.out.println(db.countBy("username", "hunter"));
  
          db.close();
      }
  }
  </code></pre>
  
  <p>User.java (part of the maven package meta1203-userservice)</p>
  
  <pre class="lang-java prettyprint-override"><code>package com.meta1203.microservices.user.model;
  
  import java.util.Set;
  
  import javax.persistence.ElementCollection;
  import javax.persistence.Entity;
  import javax.persistence.Table;
  
  import com.meta1203.microservices.BaseEntity;
  
  @Entity
  @Table(name = "users")
  public class User extends BaseEntity {
      private String username;
  
      private String textNotificationList;
      private String emailNotificationList;
  
      @ElementCollection
      private Set&lt;Long&gt; ignoredAlerts;
      @ElementCollection
      private Set&lt;Long&gt; clients;
  
      // to be parsed with DateTimeFormatter.ISO_OFFSET_DATE_TIME
      private String lastItineraryUpdate;
  
       // getters and setters
  }
  </code></pre>
  
  <p>parent's pom.xml</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;com.meta1203&lt;/groupId&gt;
      &lt;artifactId&gt;meta1203-services&lt;/artifactId&gt;
      &lt;packaging&gt;pom&lt;/packaging&gt;
      &lt;version&gt;0.0.1&lt;/version&gt;
  
      &lt;properties&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
          &lt;maven.compiler.source&gt;1.8&lt;/maven.compiler.source&gt;
          &lt;maven.compiler.target&gt;1.8&lt;/maven.compiler.target&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.amazonaws&lt;/groupId&gt;
              &lt;artifactId&gt;aws-lambda-java-core&lt;/artifactId&gt;
              &lt;version&gt;1.2.0&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.meta1203&lt;/groupId&gt;
              &lt;artifactId&gt;meta1203-data&lt;/artifactId&gt;
              &lt;version&gt;0.0.1&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
              &lt;version&gt;5.3.11.Final&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;mysql&lt;/groupId&gt;
              &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
              &lt;version&gt;5.1.36&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
      &lt;modules&gt;
          &lt;module&gt;meta1203-userservice&lt;/module&gt;
      &lt;/modules&gt;
  &lt;/project&gt;
  </code></pre>
  
  <hr>
  
  <pre><code>Sep 07, 2019 2:36:13 AM org.hibernate.Version logVersion
  INFO: HHH000412: Hibernate Core {[WORKING]}
  Sep 07, 2019 2:36:13 AM org.hibernate.cfg.Environment &lt;clinit&gt;
  INFO: HHH000206: hibernate.properties not found
  Sep 07, 2019 2:36:13 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager &lt;clinit&gt;
  INFO: HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
  Sep 07, 2019 2:36:14 AM org.hibernate.dialect.Dialect &lt;init&gt;
  INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
  Sep 07, 2019 2:36:14 AM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
  INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@329dbdbf] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
  Sep 07, 2019 2:36:14 AM org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
  INFO: HHH000397: Using ASTQueryTranslatorFactory
  Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [select u from User u where u.username = :id]
      at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:138)
      at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
      at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188)
      at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:729)
      at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:745)
      at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:104)
      at com.meta1203.microservices.CommonDB.findOneBy(CommonDB.java:72)
      at com.meta1203.microservices.user.TestDB.main(TestDB.java:12)
  Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [select u from User u where u.username = :id]
      at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:79)
      at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103)
      at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:219)
      at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:143)
      at org.hibernate.engine.query.spi.HQLQueryPlan.&lt;init&gt;(HQLQueryPlan.java:119)
      at org.hibernate.engine.query.spi.HQLQueryPlan.&lt;init&gt;(HQLQueryPlan.java:80)
      at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:153)
      at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:611)
      at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:720)
      ... 4 more
  Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped
      at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:169)
      at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:91)
      at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:79)
      at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:331)
      at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3695)
      at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3584)
      at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:720)
      at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:576)
      at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:313)
      at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:261)
      at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:271)
      at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:191)
      ... 10 more
  </code></pre>