Cha*_*nya 2 java oracle hibernate
我正在尝试@Subselect使用Hibernate documentaion的示例。
我已经为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
}
Run Code Online (Sandbox Code Playgroud)
我已经在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
}
Run Code Online (Sandbox Code Playgroud)
我是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();
}
Run Code Online (Sandbox Code Playgroud)
当我运行该程序时,出现以下错误:
休眠:从双休眠中选择hibernate_sequence.nextval:插入(选择item.name名称,max(bid.amount)数量,count(*)来自bid.item_id = item.id按item.name分组的项目联合投标的计数) (数量,数量,名称,ID)值(?,?,?,?)2014年8月10日1:24:31 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions警告:SQL错误:904,SQLState: 42000 2014年8月10日,下午1:24:31 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions错误:ORA-00904:“ ID”:无效的标识符
引起原因:java.sql.SQLSyntaxErrorException:ORA-00904:“ ID”:无效的标识符
请帮助我如何测试休眠的@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();
}
Run Code Online (Sandbox Code Playgroud)
更新:此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)
Run Code Online (Sandbox Code Playgroud)
摘要应该是一个不变的只读实体。创建摘要是没有意义的。您可以做的是创建项目,创建投标,然后查询摘要实例。
编辑:错误非常明显。查看生成的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_
Run Code Online (Sandbox Code Playgroud)
并出现错误:
ORA-00904: "SUMMARY0_"."ID": invalid identifier
Run Code Online (Sandbox Code Playgroud)
您的实体定义了一个id属性:
@Id @GeneratedValue
private int id;
Run Code Online (Sandbox Code Playgroud)
但是查询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
Run Code Online (Sandbox Code Playgroud)
您可能希望查询为
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
Run Code Online (Sandbox Code Playgroud)
还要注意,@GeneratedValue注释没有意义,因为您不能持久保存Summary的实例,因此Hibernate永远不必为此实体生成ID。
| 归档时间: |
|
| 查看次数: |
12594 次 |
| 最近记录: |