membersound
membersound

Reputation: 86845

Show size of a OneToMany list within same table of the object?

I have a table grid, which displays all users contained in the database. A user has a list of own products. I want to display the size of the products list also within the same table grid.

What am I doing wrong here?

class User {
    String username;

    @OneToMany(mappedBy="user", cascade=CascadeType.ALL) 
    List<Product> products = new ArrayList<Product>();
}

class Product {
    @ManyToOne
    User user;
}

Ex:

 org.hibernate.exception.SQLGrammarException: could not initialize a collection: [User.products#1]

at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.loader.Loader.loadCollection(Loader.java:2173)
    at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:62)
    at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:627)
    at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:83)
    at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1863)
    at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:369)
    at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)
    at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:134)
    at org.hibernate.collection.PersistentBag.size(PersistentBag.java:248)
    at UserPage.setupRender(UserPage.java:41)
    at UserPage.setupRender(UserPage.java)
    at org.apache.tapestry5.internal.structure.ComponentPageElementImpl$SetupRenderPhase.invokeComponent(ComponentPageElementImpl.java:174)
    at org.apache.tapestry5.internal.structure.ComponentPageElementImpl$AbstractPhase.invoke(ComponentPageElementImpl.java:133)
    ... 94 more

Caused by: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: PRODUCT
    at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
    at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCPreparedStatement.<init>(Unknown Source)
    at org.hsqldb.jdbc.JDBCConnection.prepareStatement(Unknown Source)
    at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534)
    at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:452)
    at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:161)
    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1700)
    at org.hibernate.loader.Loader.doQuery(Loader.java:801)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
    at org.hibernate.loader.Loader.loadCollection(Loader.java:2166)
    ... 106 more

Upvotes: 1

Views: 293

Answers (3)

Aleksandr M
Aleksandr M

Reputation: 24396

User is a keyword for database. Try to change User entity name to something else (e.g. UserProfile).

Also check that both your entities are mapped in hibernate.cfg.xml.

Upvotes: 3

OQJF
OQJF

Reputation: 1350

From the log we can see that there is not table Product in hsqldb. Do you use hibernate to create the schema. I didn't see @Entity in your class. And if you just want to get the size of product, you can try to use @formula annotation, it can make you domain more sample.

Upvotes: 0

Teja Kantamneni
Teja Kantamneni

Reputation: 17472

This can happen for a variety of reasons. The common reason I experienced mostly is that column names does not match. Make sure your mapping xml/annotations match with schema. Also make sure you have the proper @Entity and @Table mappings with table name specified (make sure you are not using any reserved words for table names).

Upvotes: 0

Related Questions