title: Why cannot Hibernate find secondary table in an Entity?
body: <p><img src="https://i.stack.imgur.com/F8U4P.png" alt="Two related database tables with a left join"></p>
  
  <hr>
  
  <p>I found no answers to this question which is also asked on many other forums. Although I have the tables IAS_FORM_BKT_BAYI and IAS_FORM_BKT_BAYI_SO present in the database, with the annotated entity class (the source code below the exception) I'm getting this exception in my JSF &amp; Hibernate-based application:</p>
  
  <pre><code>org.hibernate.AnnotationException: Cannot find the expected secondary table: no IAS_FORM_BKT_BAYI available for net.ozar.bpm.entity.bkt.Bayi
      org.hibernate.cfg.Ejb3Column.getJoin(Ejb3Column.java:293)
      org.hibernate.cfg.Ejb3Column.getTable(Ejb3Column.java:272)
      org.hibernate.cfg.annotations.SimpleValueBinder.make(SimpleValueBinder.java:222)
      org.hibernate.cfg.AnnotationBinder.bindId(AnnotationBinder.java:1898)
      org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1279)
      org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:754)
      org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:546)
      org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:291)
      org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1148)
      org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1226)
      org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:173)
      org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:854)
      org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:191)
      org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:253)
      org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:125)
      javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
      javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:60)
      net.ozar.bpm.util.EntityUtil.getEntityManagerFactory(EntityUtil.java:13)
      net.ozar.bpm.util.EntityUtil.getEntityManager(EntityUtil.java:21)
      net.ozar.bpm.servisler.CalisanJpaController.getEntityManager(CalisanJpaController.java:31)
      net.ozar.bpm.servisler.CalisanJpaController.getPersonId(CalisanJpaController.java:120)
      net.ozar.bpm.web.jsfmanaged.LoginBean.oturumAc(LoginBean.java:122)
      sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      java.lang.reflect.Method.invoke(Unknown Source)
      org.apache.el.parser.AstValue.invoke(AstValue.java:191)
      org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
      com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
      javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
      com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
      javax.faces.component.UICommand.broadcast(UICommand.java:387)
      org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:321)
      org.ajax4jsf.component.AjaxViewRoot.broadcastEvents(AjaxViewRoot.java:296)
      org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:253)
      org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:466)
      com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
      com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
      com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
      javax.faces.webapp.FacesServlet.service(FacesServlet.java:265)
      org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
      org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
      org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:390)
      org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:517)
      net.ozar.bpm.web.filters.RestrictPageFilter.doFilter(RestrictPageFilter.java:180)
  </code></pre>
  
  <p><strong>Bayi.java</strong></p>
  
  <pre><code>@Entity
  @Table(name = "IAS_FORM_BKT_BAYI")
  @SecondaryTable(name="IAS_FORM_BKT_BAYI_SO", pkJoinColumns = @PrimaryKeyJoinColumn(name="CUSTOMER_ID"))
  public class Bayi implements Serializable {
  private static final long serialVersionUID = 8681843504687728382L;
      @Id
      @Basic(optional = false)
      @NotNull
      @Column(table="IAS_FORM_BKT_BAYI", name = "CUSTOMER_ID", nullable = false)
      private Integer customerId;
      @Basic(optional = false)
      @NotNull
      @Size(min = 1, max = 30)
      @Column(table="IAS_FORM_BKT_BAYI", name = "CUSTOMER_NUMBER", nullable = false, length = 30)
      private String customerNumber;
      @Size(max = 50)
      @Column(table="IAS_FORM_BKT_BAYI", name = "CUSTOMER_NAME", length = 50)
      private String customerName;
      @Size(max = 50)
      @Column(table="IAS_FORM_BKT_BAYI", name = "VERGI_DAIRESI", length = 50)
      private String vergiDairesi;
      @Size(max = 20)
      @Column(table="IAS_FORM_BKT_BAYI", name = "VERGI_NO", length = 20)
      private String vergiNo;
      @Size(max = 150)
      @Column(table="IAS_FORM_BKT_BAYI", name = "OPERATOR", length = 150)
      private String operator;
      @Column(table="IAS_FORM_BKT_BAYI_SO", name = "SOZLESME", length = 5)
      private String sozlesme;
      @Column(table="IAS_FORM_BKT_BAYI_SO", name = "SOZLESME_UYGUN", length = 5)
      private String sozlesmeUygun;
      @Column(table="IAS_FORM_BKT_BAYI_SO", name = "KEFIL_IMZA", length = 5)
      private String kefilImza;
      @Column(table="IAS_FORM_BKT_BAYI_SO", name = "ACIKLAMA", length = 2000)
      private String aciklama;
  
      public Bayi() {
      }
  
      // getters and setters
      // ...
  
  }
  </code></pre>
  
  <p>What I'm really trying to achieve is get the resultset which I obtain running the following query mapped to the Bayi (entity) object:</p>
  
  <pre><code>SELECT B.CUSTOMER_ID, B.CUSTOMER_NUMBER, B.CUSTOMER_NAME, B.VERGI_DAIRESI, 
  B.VERGI_NO, C.SOZLESME, C.SOZLESME_UYGUN, C.KEFIL_IMZA, C.ACIKLAMA  FROM   
  IAS_FORM_BKT_BAYI B LEFT JOIN IAS_FORM_BKT_BAYI_SO C ON B.CUSTOMER_ID = 
  C.CUSTOMER_ID WHERE ORG_ID = 102
  </code></pre>