title: Cannot manage convert hibernate mapping to annotations
body: <p>I do not really want to talk about why one approach is better than other. I started with annotations and could not get it working simple as it was. When tried using XML configuration it worked. But why? How to debug this kind of problem?</p>
  
  <p>I have a pretty simple existing table "tRegion" with 2 String columns - "Code" and "Region" where Code field is the unique key.</p>
  
  <p>I create a class:</p>
  
  <pre><code>@Entity
  @Table(name = "tRegion")
  public class Region {
      @Id String Code;
      @Column String Region;
  
      public void setCode(String code) {
          Code = code;
      }
      public void setRegion(String region) {
          Region = region;
      }
      public String getCode() {
          return Code;
      }
      public String getRegion() {
          return Region;
      }
  }
  </code></pre>
  
  <p>If I run the code:</p>
  
  <pre><code>List&lt;Region&gt; result = (List&lt;Region&gt;) session.createQuery("from Region").list(); 
  </code></pre>
  
  <p>I get the error:</p>
  
  <pre><code>INFO: HHH000397: Using ASTQueryTranslatorFactory org.hibernate.hql.internal.ast.QuerySyntaxException: Region is not mapped [from Region]
  </code></pre>
  
  <p>Something is wrong with my annotations... However if I add an XML mapping file:</p>
  
  <pre><code>&lt;hibernate-mapping package="com.mypackage"&gt;
  &lt;class name="Region" table="tRegion"&gt;
      &lt;id name="Code" /&gt;
      &lt;property name="Region"/&gt;
  &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>it works. For me the mapping looks equivalent to my annotations. What could be wrong? I'm using Hibernate 4.3.1 libraries if it could make any difference.</p>
  
  <p><strong>Edited/solution:</strong></p>
  
  <p>Everybody seemed to agree on solution to modify the <em>persistence.xml</em>, however I'm a beginner in Entity management in Java, so I had no clue I was supposed to use one. Well, after reading suggestions, I even tried to add it (I enabled JPA facet and added the class element to <em>persistence.xml</em>), but I guess I had to do something else to make it use JPA (tried to add @PersistanceContext annotation with no success)</p>
  
  <p>I am also not keen to add another configuration file. In fact the way I deploy my application prevents me modifying any configuration files, and having configuration files that you do not use to eventually modify configuration are kind of pointless.</p>
  
  <p>The approach that finally worked was to simply add <code>.addAnnotatedClass(com.mypackage.Region.class)</code> to my routine that instantiate SessionFactory.</p>
  
  <pre><code>    public static SessionFactory createSessionFactory() {
              Configuration configuration = new Configuration();
              configuration
                      .addAnnotatedClass(com.mypackage.Region.class)
                      .configure();
              ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
              return configuration.buildSessionFactory(serviceRegistry);
      }
  </code></pre>