English 中文(简体)
org.hibernate. Tenion.SQLGrammarException: could not initialize a Collection
原标题:org.hibernate.exception.SQLGrammarException: could not initialize a collection

I have 3 tables tbl_category , tbl_Advertisment and tbl_linkcategoryadvert( having many to many association b/w category & advertisment )

My POJO s as given below

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

www.un.org/Depts/DGACM/index_spanish.htm PostAdvert.java - Main category

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

I m不提供DAO.java,因为它只载有Hibernate boilerplate代码。

在我担任主要职务时,我有以下例外:

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_link categoryadvert

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

现在我的问题是......

  1. Am i taking wrong names for fields in table "link_categoryadvert"?
  2. how hibernate looks for column names & mappings?
  3. what are other possible attributes for @JoinTable Annotation?
问题回答

你们想在拥有这种关系的类别上使用“可合用的注释”。 您将需要将“可相互参照的”说明列入广告类别。





相关问题
Multiple Hibernate instances using C3P0

I am facing a weird problem and it seems to be c3p0 related. I am starting two instances of an app in the same java vm which interact with each other. After some operations "APPARENT DEADLOCK" ...

Hibernate vs Ibatis caching

We can speed up a hibernate app easyly with 2nd level cache using infinispan or ehcache/terracotta,... but ibatis only have a simple interface to implement for caching. And hibernate knows more ...

Using annotations to implement a static join in hibernate

I m relatively new to hibernate and was wondering if someone could help me out. While I have no issues implementing a normal join on multiple columns in hibernate using the @JoinColumns tag, I m ...

Hibernate query with fetch question

I have a 2 entities in a One-To-Many relationship: OfficeView.java: public class OfficeView implements java.io.Serializable { private Integer officeId; private String addr1; private ...

hibernate interceptors : afterTransactionCompletion

I wrote a Hibernate interceptor : public class MyInterceptor extends EmptyInterceptor { private boolean isCanal=false; public boolean onSave(Object entity, Serializable arg1, Object[] arg2, String[]...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

Hibernate/GORM: collection was not processed by flush()

I have an integration test in my Grails application that fails when I try to save an entity of type Member invitingMember.save(flush: true) This raises the following exception org.hibernate....

Hibernate Criteria API equivalent for "elements()"

Is it possible to implement the following query using Criteria API? select order from ORDER as order,ITEM as item where item.itemID like ITM_01 and item in elements(order.items)

热门标签