org.hibernate.exception.SQLGrammarException: could not collection element by index

Hibernate JIRA | Paul Roe | 9 years ago
  1. 0
  2. 0
  3. 0

    The problem appears to be that the hbm.xml file generated for two classes that have a Many-To-Many relationship does not include the attribute for the link (aka association) table name. see http://forum.hibernate.org/viewtopic.php?t=964363 When I tried to navigate across the association using the generated hbm.xml file I got this stack trace (snipped): WARNING: SQL Error: 1146, SQLState: 42S02 5/09/2006 14:00:24 org.hibernate.util.JDBCExceptionReporter logExceptions SEVERE: Table 'blah.enginemodels' doesn't exist Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not initialize a collection: [com.acme.model.ChassisModel.engineModels#1] at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) When I added the table="<linktablename>" attribute to the "set" element the navigation succeeded. <set name="roles" inverse="true" table="lt_role_person"> <key> <column name="person_pkid" not-null="true"> <comment></comment> </column> </key> <many-to-many entity-name="nz.co.linktest.model.Role"> <column name="role_pkid" not-null="true"> <comment></comment> </column> </many-to-many> </set> I don't think the stuff in the log is related but I could be wrong so... The file workspace\org.hibernate.eclipse.console\hibernate-tools.log had something like 2006-09-02 17:59:32,812 0 [ Worker-5] WARN ibernate.cfg.reveng.JDBCReader - The JDBC driver didn't report any primary key columns in lt_e_model_c_model. Asking rev.eng. strategy 2006-09-02 17:59:32,812 0 [ Worker-5] WARN ibernate.cfg.reveng.JDBCReader - Rev.eng. strategy did not report any primary key columns for lt_e_model_c_model 2006-09-02 18:09:08,093 575281 [ Worker-1] WARN ibernate.cfg.reveng.JDBCReader - The JDBC driver didn't report any primary key columns in lt_e_model_c_model. Asking rev.eng. strategy 2006-09-02 18:09:08,109 575297 [ Worker-1] WARN ibernate.cfg.reveng.JDBCReader - Rev.eng. strategy did not report any primary key columns for lt_e_model_c_model 2006-09-03 08:03:53,718 0 [ Worker-9] WARN ibernate.cfg.reveng.JDBCReader - The JDBC driver didn't report any primary key columns in lt_e_model_c_model. Asking rev.eng. strategy 2006-09-03 08:03:53,750 32 [ Worker-9] WARN ibernate.cfg.reveng.JDBCReader - Rev.eng. strategy did not report any primary key columns for lt_e_model_c_model 2006-09-03 16:01:57,687 0 [ Worker-3] WARN ibernate.cfg.reveng.JDBCReader - The JDBC driver didn't report any primary key columns in lt_e_model_c_model. Asking rev.eng. strategy 2006-09-03 16:01:58,125 438 [ Worker-3] WARN ibernate.cfg.reveng.JDBCReader - Rev.eng. strategy did not report any primary key columns for lt_e_model_c_model 2006-09-03 16:05:27,312 209625 [ Worker-3] WARN ibernate.cfg.reveng.JDBCReader - The JDBC driver didn't report any primary key columns in lt_e_model_c_model. Asking rev.eng. strategy 2006-09-03 16:05:27,406 209719 [ Worker-3] WARN ibernate.cfg.reveng.JDBCReader - Rev.eng. strategy did not report any primary key columns for lt_e_model_c_model where lt_e_model_c_model is like the name of the link table that the reveng originally "failed" on. Interestingly there does not seem to be any similar log entries for the attached example. The file workspace\org.hibernate.eclipse.console\hibernate-console.xml-8 had something (some other stuff removed) like <?xml version="1.0" encoding="UTF-8"?><hibernate-console><configuration name="Linktest"><hibernate-config-xml location="/Linktest/src/hibernate.cfg.xml"/><classpath><path location="/Linktest/bin"/><path location="/Linktest/lib/mysql-connector-java-3.1.13-bin.jar"/></classpath></configuration></hibernate-console>

    Hibernate JIRA | 1 decade ago | Vaughn Butt
    org.hibernate.exception.SQLGrammarException: could not initialize a collection: [com.acme.model.ChassisModel.engineModels#1]
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    The following test: public void test2(){ List<Storage> storages= getSF().openSession().createCriteria(Storage.class).list(); System.out.println("Storages:"); for(Storage storage:storages){ System.out.println(" - "+storage); Map<Product,Quantity> products=storage.getProductQuantity(); for(Product p:products.keySet()) System.out.println(" - - "+p+" "+products.get(p)); } @Entity @AccessType("field") public class Storage{ @Id protected int id; protected String name; @MapKey @OneToMany(cascade=CascadeType.ALL) protected Map<Product,Quantity> productQuantity=new HashMap<Product,Quantity>(); public Map<Product, Quantity> getProductQuantity(){ return productQuantity; } public void addProduct(Product product,Quantity quantity){ productQuantity.put(product,quantity); } Storage(){ } public Storage(String name){ this.name=name; id=hashCode(); } public String toString(){ return name; } } this class (Storage.class) persists perfectly (checked thru pgAdmin III), the name field is fetched, but when trying to fetch Map: for(Product p:products.keySet()) it fails with that kind of listing: Storages: - Main storage 12:04:36,154 WARN JDBCExceptionReporter:71 - SQL Error: 0, SQLState: 42703 12:04:36,154 ERROR JDBCExceptionReporter:72 - ERROR: column productqua0_.id does not exist Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not initialize a collection: [Storage.productQuantity#779317485] at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:65) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) at org.hibernate.loader.Loader.loadCollection(Loader.java:1926) at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:71) at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:520) at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60) at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1693) at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344) at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86) at org.hibernate.collection.PersistentMap.keySet(PersistentMap.java:190) at PTest.listStorage(Ptest.java:87) at PTest..test2(Ptest.java:80) at Ptest.<init>(Ptest.java:20) at Ptest.main(Ptest.java:13) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:86) Caused by: org.postgresql.util.PSQLException: ERROR: column productqua0_.id does not exist at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1501) at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1286) at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:177) at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:430) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:346) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:250) at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:139) at org.hibernate.loader.Loader.getResultSet(Loader.java:1669) at org.hibernate.loader.Loader.doQuery(Loader.java:662) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224) at org.hibernate.loader.Loader.loadCollection(Loader.java:1919) ... 16 more PS I've set priority to Critical, since it's really critical for me. It would be hard to write all the code now with Sets, which work fine, but the data structure is really ugly, and later refactor everything to Maps

    Hibernate JIRA | 1 decade ago | Philipp Pirozhkov
    org.hibernate.exception.SQLGrammarException: could not initialize a collection: [Storage.productQuantity#779317485]
  6. 0

    jtrac issues w/ UNIX and ORACLE

    j-trac | 8 years ago | hkansal
    org.hibernate.exception.SQLGrammarException: could not initialize a collection: \[info.jtrac.domain.Item.history\#313\]

    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. org.hibernate.exception.SQLGrammarException

      could not collection element by index

      at org.hibernate.exception.SQLStateConverter.convert()
    2. Hibernate
      OneToManyPersister.getElementByIndex
      1. org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
      2. org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
      3. org.hibernate.loader.Loader.loadEntity(Loader.java:1841)
      4. org.hibernate.loader.entity.CollectionElementLoader.loadElement(CollectionElementLoader.java:72)
      5. org.hibernate.persister.collection.OneToManyPersister.getElementByIndex(OneToManyPersister.java:360)
      5 frames
    3. Hibernate
      PersistentMap.get
      1. org.hibernate.collection.AbstractPersistentCollection.readElementByIndex(AbstractPersistentCollection.java:158)
      2. org.hibernate.collection.PersistentMap.get(PersistentMap.java:146)
      2 frames