title: org.hibernate.exception.SQLGrammarException: could not initialize a collection
body: <p>I have 3 tables tbl_category , tbl_Advertisment and tbl_linkcategoryadvert( having many to many association b/w category &amp; advertisment )</p>
  
  <p>My POJO's as given below</p>
  
  <p><strong>Advertisment.java</strong></p>
  
  <pre><code>@Entity
  @Table(name="tbl_advertisment")
  public class Advertisment implements Serializable {
      private long id;
      private String title;
      private String message;
      private Set&lt;Category&gt; categories;
  
      public Advertisment(String title, String message) {
          this.title = title;
          this.message = message;
          this.categories=new HashSet&lt;Category&gt;();
      }
  
      protected Advertisment() {
      }
  
      @Id
      @GeneratedValue
      protected long getId() {
          return id;
      }
  
      protected void setId(long id) {
          this.id = id;
      }
  
      public String getMessage() {
          return message;
      }
  
      public void setMessage(String message) {
          this.message = message;
      }
  
      @Column(nullable=false)
      public String getTitle() {
          return title;
      }
  
      public void setTitle(String title) {
          this.title = title;
      }
  
      @ManyToMany(mappedBy="adverts")
      public Set&lt;Category&gt; getCategories() {
          return categories;
      }
  
      public void setCategories(Set&lt;Category&gt; categories) {
          this.categories = categories;
      }
  }
  </code></pre>
  
  <p><strong>Category.java</strong></p>
  
  <pre><code>@Entity
  @Table(name="tbl_category")
  public class Category implements Serializable {
      private long id;
      private String title;
      private Set&lt;Advertisment&gt; adverts=new HashSet&lt;Advertisment&gt;();
  
      public Category(String title) {
          this.title = title;
      }
  
      protected Category() {
      }
  
      @ManyToMany
      @JoinTable(name="tbl_linkcategoryadvert")
      public Set&lt;Advertisment&gt; getAdverts() {
          return adverts;
      }
  
      public void setAdverts(Set&lt;Advertisment&gt; adverts) {
          this.adverts = adverts;
      }
  
      public void addAdvert(Advertisment advert){
          adverts.add(advert);
      }
  
      @Id
      @GeneratedValue
      protected long getId() {
          return id;
      }
  
      protected void setId(long id) {
          this.id = id;
      }
  
      @Column(unique=true,nullable=false)
      public String getTitle() {
          return title;
      }
  
      public void setTitle(String title) {
          this.title = title;
      }
  }
  </code></pre>
  
  <p><strong>PostAdvert.java - Main class</strong></p>
  
  <pre><code>public class PostAdvertisment extends DAO{
  
      public Advertisment post(String username,String catTitle,String title,String message) 
              throws DAOException {
          try{
              begin();
  
              Query categoryQuery=getSession().createQuery("from Category where title=:categoryTitle");
              categoryQuery.setString("categoryTitle", catTitle);
              Category category=(Category)categoryQuery.uniqueResult();
  
              Advertisment advert=new Advertisment(title,message,user);
              getSession().save(advert);
  
              category.addAdvert(advert);     
              getSession().save(category);
  
              commit();
  
              return advert;
          } catch(HibernateException he){
              he.printStackTrace();
              throw new DAOException(he.getMessage());
          }     
      }
  
      public static void main(String[] args){
  
          String catTitle="LCD";
          String title="Bravia";
          String message="High Contrast Television";
  
          try{
              PostAdvertisment post=new PostAdvertisment();
              post.post(username, catTitle, title, message);
              DAO.close();
          } catch(DAOException daoEx){
              System.out.println(daoEx.getMessage());
          }
      }
  }
  </code></pre>
  
  <p><strong>DAO.java</strong></p>
  
  <p>I'm not providing DAO.java as it only contains Hibernate boilerplate code.</p>
  
  <p>When I run main class I get following exception:</p>
  
  <pre><code>SEVERE: Unknown column 'adverts0_.categories_id' in 'field list'
  org.hibernate.exception.SQLGrammarException: could not initialize a collection: [hibernate.Category.adverts#3]
      at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
      at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
      at org.hibernate.loader.Loader.loadCollection(Loader.java:2001)
      at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:36)
      at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:565)
      at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
      at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1716)
      at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344)
      at org.hibernate.collection.PersistentSet.add(PersistentSet.java:189)
      at hibernate.Category.addAdvert(Category.java:48)
      at hibernate.client.PostAdvertisment.post(PostAdvertisment.java:35)
      at hibernate.client.PostAdvertisment.main(PostAdvertisment.java:56)
  Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'adverts0_.categories_id' in 'field list'
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
      at com.mysql.jdbc.Util.getInstance(Util.java:384)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
      at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3566)
      at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3498)
      at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
      at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113)
      at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2568)
      at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2113)
  </code></pre>
  
  <p>Please Help me out....</p>
  
  <hr>
  
  <p>My Tables looks like as :</p>
  
  <p><strong>1. tbl_advert</strong></p>
  
  <pre><code>id PK BIGINT
  title VARCHAR(255)
  </code></pre>
  
  <p><strong>2. tbl_category</strong></p>
  
  <pre><code>id PK BIGINT
  title VARCHAR(255)
  </code></pre>
  
  <p><strong>3. tbl_linkcategoryadvert</strong></p>
  
  <pre><code>advert_id FK references(advert.id)
  category_id FK references(category.id)
  </code></pre>
  
  <p>Now my questions are...</p>
  
  <ol>
  <li>Am i taking wrong names for fields in table "link_categoryadvert"?</li>
  <li>how hibernate looks for column names &amp; mappings?</li>
  <li>what are other possible attributes for @JoinTable Annotation?</li>
  </ol>