如何在Hibernate中使用@Subselect

时间:2014-08-10 08:06:58

标签: java oracle hibernate

我正在尝试使用Hibernate documentaion处理@Subselect的示例。

我为Bid和Item创建了如下实体:

@Entity
public class Bid {
    @Id
    private int id;
    @Column(name="item_id")
    private int itemId;
    @Column
    private int amount;
//getters & setters
}


@Entity
public class Item {
    @Id
    private int id;
    @Column
    private String name;
//getters and setters
}

我在Bid和Item的数据库表中插入了一些记录。现在我创建了另一个实体来测试@Subselect:

@Entity
@Subselect("select item.name name, max(bid.amount) amount, count(*) count " + "from item "
        + "join bid on bid.item_id = item.id " + "group by item.name")
@Synchronize({ "item", "bid" })
// tables impacted
public class Summary {
    @Id @GeneratedValue
    private int id;
    @Column
    private String name;
    @Column
    private String amount;
    @Column
    private String count;
//getters & setters
}

我是Hibernate的新手,所以试图创建一个示例程序来测试@Subselect的功能。

public class AppTest {
    public static void main(String[] args) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        //createItemsAndBids(session);
        Summary summary = new Summary();
        session.save(summary);

        session.getTransaction().commit();
        HibernateUtil.getSessionFactory().close();
    }

当我运行此程序时,我遇到了以下错误:

  

Hibernate:从双Hibernate中选择hibernate_sequence.nextval:   插入(选择item.name名称,最大(bid.amount)金额,计数(*)   从bid.item_id = item.id group by item.name的项目加入出价计算   (金额,数量,名称,身份证)值(?,?,?,?)2014年8月10日下午1:24:31   org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions WARN:   SQL错误:904,SQLState:42000 Aug 10,2014 1:24:31 PM   org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions错误:   ORA-00904:“ID”:标识符无效

     

引起:java.sql.SQLSyntaxErrorException:ORA-00904:“ID”:无效   标识符

请帮我看看如何测试hibernate的@Subselect功能

我也尝试使用HQL,即使这样我也得到同样的错误:

public class AppTest {
    public static void main(String[] args) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        //createItemsAndBids(session);
        Query query = session.createQuery("from Summary");
        List result = query.list();
        System.out.println(result);

        session.getTransaction().commit();
        HibernateUtil.getSessionFactory().close();
    }

更新: 我在这个HQL查询中得到的错误是:

Aug 11, 2014 12:35:07 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
Hibernate: select summary0_.id as id1_2_, summary0_.amount as amount2_2_, summary0_.count as count3_2_, summary0_.name as name4_2_ from ( select item.name name, max(bid.amount) amount, count(*) count from item join bid on bid.item_id = item.id group by item.name ) summary0_
Aug 11, 2014 12:35:07 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 904, SQLState: 42000
Aug 11, 2014 12:35:07 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ORA-00904: "SUMMARY0_"."ID": invalid identifier

Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not extract ResultSet
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:80)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:91)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:2065)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1862)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1838)
    at org.hibernate.loader.Loader.doQuery(Loader.java:909)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
    at org.hibernate.loader.Loader.doList(Loader.java:2553)
    at org.hibernate.loader.Loader.doList(Loader.java:2539)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2369)
    at org.hibernate.loader.Loader.list(Loader.java:2364)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:496)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:231)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1264)
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
    at org.hibernate.tutorials.hibernate5.one.AppTest.main(AppTest.java:19)

1 个答案:

答案 0 :(得分:4)

摘要应该是一个不可变的只读实体。创建摘要是没有意义的。您可以做的是创建项目,创建出价,然后查询摘要实例。

编辑:错误很清楚。查看生成的SQL:

select summary0_.id as id1_2_, summary0_.amount as amount2_2_, summary0_.count as count3_2_, summary0_.name as name4_2_ from ( select item.name name, max(bid.amount) amount, count(*) count from item join bid on bid.item_id = item.id group by item.name ) summary0_

并出现错误:

ORA-00904: "SUMMARY0_"."ID": invalid identifier

您的实体定义了一个id属性:

@Id @GeneratedValue
private int id;

但是Subselect注释的查询不会选择任何名为id的属性:

select item.name name, max(bid.amount) amount, count(*) count from item 
join bid on bid.item_id = item.id 
group by item.name

您可能希望查询

select item.id id, item.name name, max(bid.amount) amount, count(*) count from item 
join bid on bid.item_id = item.id 
group by item.id, item.name

另请注意,@GeneratedValue注释没有意义,因为您无法保留Summary的实例,因此Hibernate将永远不必为此实体生成ID。