2011-10-09 118 views
5

我有3個表tbl_category,tbl_Advertisment和tbl_linkcategoryadvert(具有多對多關聯的B/W類&廣告)org.hibernate.exception.SQLGrammarException:無法初始化集合

我POJO的下面

給出

Advertisment.java

@Entity 
@Table(name="tbl_advertisment") 
public class Advertisment implements Serializable { 
    private long id; 
    private String title; 
    private String message; 
    private Set<Category> categories; 

    public Advertisment(String title, String message) { 
     this.title = title; 
     this.message = message; 
     this.categories=new HashSet<Category>(); 
    } 

    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<Category> getCategories() { 
     return categories; 
    } 

    public void setCategories(Set<Category> categories) { 
     this.categories = categories; 
    } 
} 

Category.java

@Entity 
@Table(name="tbl_category") 
public class Category implements Serializable { 
    private long id; 
    private String title; 
    private Set<Advertisment> adverts=new HashSet<Advertisment>(); 

    public Category(String title) { 
     this.title = title; 
    } 

    protected Category() { 
    } 

    @ManyToMany 
    @JoinTable(name="tbl_linkcategoryadvert") 
    public Set<Advertisment> getAdverts() { 
     return adverts; 
    } 

    public void setAdverts(Set<Advertisment> 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; 
    } 
} 

PostAdvert.java - 主類

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()); 
     } 
    } 
} 

DAO.java

我不提供DAO.java,因爲它僅包含Hibernate的樣板代碼。

當我運行主類,我得到以下異常:

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) 

請幫我....


我的表看起來爲:

1 tbl_advert

id PK BIGINT 
title VARCHAR(255) 

2. tbl_category

id PK BIGINT 
title VARCHAR(255) 

3 tbl_linkcategoryadvert

advert_id FK references(advert.id) 
category_id FK references(category.id) 

現在我的問題是...

  1. 難道我錯取的名字爲表中的字段「link_categoryadvert 「?
  2. hibernate如何查找列名&映射?
  3. @JoinTable註解的其他可能屬性是什麼?
+1

在連接表中執行外鍵tbl_linkcategoryadvert是否有名稱categories_id和advertisments_id?否則,您可能必須配置您的@JoinTable映射以反映這些名稱。 –

+0

你能告訴我們廣告表是怎麼樣的嗎?也許你需要定義多對多的列,例如'@JoinTable(name =「adverts」,joinColumns = @ JoinColumn(name =「cat_id」))'? –

+0

請稍後編輯您的問題以提供更多詳細信息。另外,請查看編輯器中的格式幫助鏈接。 –

回答

0

您想在擁有關係的類上使用@Jointable註釋。您需要在Advertisement類上放置@Jointable註釋。

2

Hibernate正在爲您的連接列生成列名「categories_id」,其中您的表使用名稱「category_id」。參考指南的例子是how to map a many-to-many relationship。周圍文本中還有幾個相關的細節。你應該只需要在你的類別上進行映射。getAdverts()看起來像這樣:

@JoinTable(
    name="tbl_linkcategoryadvert", 
    [email protected](name="category_id"), 
    [email protected](name="advert_id") 
) 
相關問題