Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use @Subselect in Hibernate

I am trying to work on an example for @Subselect using Hibernate documentaion.

I have created entities for Bid and Item as follows:

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

I have inserted some records in database tables for Bid and Item. Now I have created another entity to test the @Subselect as:

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

I am new to Hibernate so trying to create a sample program to test the feature of @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();
    }

When I run this program I am getting below errors:

Hibernate: select hibernate_sequence.nextval from dual Hibernate: insert into ( select item.name name, max(bid.amount) amount, count(*) count from item join bid on bid.item_id = item.id group by item.name ) (amount, count, name, id) values (?, ?, ?, ?) Aug 10, 2014 1:24:31 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions WARN: SQL Error: 904, SQLState: 42000 Aug 10, 2014 1:24:31 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions ERROR: ORA-00904: "ID": invalid identifier

Caused by: java.sql.SQLSyntaxErrorException: ORA-00904: "ID": invalid identifier

Please help me how to test the feature of @Subselect of hibernate

Also I tried using HQL, even then I am getting same error:

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

Update: The error I am getting with this HQL query is :

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)
like image 910
Chaitanya Avatar asked Sep 17 '25 06:09

Chaitanya


1 Answers

Summary is supposed to be an immutable, read-only entity. It makes no sense to create a Summary. What you can do is create Items, create Bids, then query Summary instances.

EDIT: the error is pretty clear. Look at the generated 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_

and at the error:

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

Your entity defines an id property:

@Id @GeneratedValue
private int id;

but the query of the Subselect annotation doesn't select any property named 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

You probably want your query to be

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

Also note that the @GeneratedValue annotation doesn't make sense, since you can't persist instances of Summary, and Hibernate will thus never have to generate an ID for this entity.

like image 167
JB Nizet Avatar answered Sep 19 '25 16:09

JB Nizet