在对象的同一个表中显示OneToMany列表的大小?

时间:2012-10-29 12:26:39

标签: java sql hibernate java-ee one-to-many

我有一个表格,它显示数据库中包含的所有用户。 用户拥有自己的产品列表。我想在同一个表格中显示产品列表的大小。

我在这里做错了什么?

class User {
    String username;

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

class Product {
    @ManyToOne
    User user;
}

例如:

 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

3 个答案:

答案 0 :(得分:3)

User是数据库的关键字。尝试将User实体名称更改为其他名称(例如UserProfile)。

同时检查您的实体是否都映射到hibernate.cfg.xml

答案 1 :(得分:0)

这可能由于各种原因而发生。我经历的主要原因是列名不匹配。确保映射xml / annotations与模式匹配。还要确保您具有指定了表名的正确@Entity和@Table映射(确保您没有对表名使用任何保留字)。

答案 2 :(得分:0)

从日志中我们可以看到hsqldb中没有表Product。您是否使用hibernate来创建架构。我没有在你班上看到@Entity。如果你只是想获得产品的大小,你可以尝试使用@formula注释,它可以让你的域更多样本。