-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: HQL : how to update attribute of derived class?
PostPosted: Mon Nov 26, 2007 6:52 am 
Newbie

Joined: Thu Apr 08, 2004 4:28 am
Posts: 16
Hibernate version:3.2.4 sp1

Used Database : Oracle 10

Model structure:

Code:
class A {
private String oid;
}

class B extends A {
private int quantity;
}

class C extends A {
}




Mapping structure:
Also used "joined-subclass" for the mapping file.

Java Code:
Code:
String sql = "update B set quantity = :newQuanity where oid = :oid";

            Query query = session.createQuery(sql);
            query.setString("oid", a.getOid());
            query.setLong("newQuanity", quanity);
            query.executeUpdate();


SQL output:
Hibernate: insert into HT_OR_B select b.oid as oid from ORDERS.OR_B b, ORDERS.OR_A a where b.oid=a.oid and oid=?
Hibernate: delete from HT_OR_B


Error:
Caused by: org.hibernate.exception.SQLGrammarException: could not insert/select ids for bulk update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.hql.ast.exec.MultiTableUpdateExecutor.execute(MultiTableUpdateExecutor.java:127)
at org.hibernate.hql.ast.QueryTranslatorImpl.executeUpdate(QueryTranslatorImpl.java:396)
at org.hibernate.engine.query.HQLQueryPlan.performExecuteUpdate(HQLQueryPlan.java:259)
at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:1141)
at org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:94)
...
Caused by: java.sql.SQLException: ORA-00918: Kolom is ambigu gedefinieerd.

at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:745)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:216)
at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:966)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1170)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3339)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3423)
at weblogic.jdbc.wrapper.PreparedStatement.executeUpdate(PreparedStatement.java:159)
at org.hibernate.hql.ast.exec.MultiTableUpdateExecutor.execute(MultiTableUpdateExecutor.java:118)
at org.hibernate.hql.ast.QueryTranslatorImpl.executeUpdate(QueryTranslatorImpl.java:396)
at org.hibernate.engine.query.HQLQueryPlan.performExecuteUpdate(HQLQueryPlan.java:259)
at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:1141)
at org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:94)



I think Hibernate can not resolve properties of a subclass in a HQL. So in my example Hibernate can not find the column name of the quantity attribute of my B class.
Is that right?

Thanks in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 9:19 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Quote:
Hibernate: insert into HT_OR_B select b.oid as oid from ORDERS.OR_B b, ORDERS.OR_A a where b.oid=a.oid and oid=?

Caused by: java.sql.SQLException: ORA-00918: Kolom is ambigu gedefinieerd.


With "oid = ?", SQL can't determine which oid column you are referring to: a.oid or b.oid


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 9:31 am 
Newbie

Joined: Thu Apr 08, 2004 4:28 am
Posts: 16
Thanks for your reaction!

The query you mentioned is generated by Hibernate. It's a temporary table Hibernate uses for doing a bulk update.

I also tried the following query:

Code:
String sql = "update B b set b.quantity = :newQuanity where b.oid = :oid";


But I receive the same error.

Any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 9:47 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
You could try using "id" instead of the actual identity column name. It should be replaced by the PK of the table, hopefully with the correct prefix attached.

From the docs:
Quote:
14.5. Refering to identifier property
...
The special property (lowercase) id may be used to reference the identifier property of an entity provided that entity does not define a non-identifier property named id.


If you're just updating one row why not load the entity and update the quantity value??


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 27, 2007 5:02 am 
Newbie

Joined: Thu Apr 08, 2004 4:28 am
Posts: 16
Thanks for your advice, but it didn't work. If I update an attribute of the base class, it's working with this construction. My base class looks like;

Code:
class A {
private String oid;
private int phase;
private long balance;
}


Then the sql output gives me:

Hibernate: insert into HT_A select a.oid as oid from ORDERS.A a where oid=?
Hibernate: update ORDERS.A set PHASE=? where (oid) IN (select oid from HT_A)
Hibernate: delete from HT_A

Hibernate: insert into HT_A select a.oid as oid from ORDERS.A a where oid=?
Hibernate: update ORDERS.A set BALANCE=? where (oid) IN (select oid from HT_A)
Hibernate: delete from HT_A

But when I try to update an attribute of my derived class with the following query (used id like you said):

query = update B b set b.quantity = :newQuantity where b.id = :oid

I get the following query output:

Hibernate: insert into HT_B select b.oid as oid from ORDERS.B b, ORDERS.A a where b.oid=a.oid and oid=?
Hibernate: delete from HT_B

So my update query is missing.... and I get this error

ORA-00918: column ambiguously defined
Cause: A column name used in a join exists in more than one table and is thus referenced ambiguously. In a join, any column name that occurs in more than one of the tables must be prefixed by its table name when referenced. The column should be referenced as TABLE.COLUMN or TABLE_ALIAS.COLUMN. For example, if tables EMP and DEPT are being joined and both contain the column DEPTNO, then all references to DEPTNO should be prefixed with the table name, as in EMP.DEPTNO or E.DEPTNO.
Action: Prefix references to column names that exist in multiple tables with either the table name or a table alias and a period (.), as in the examples above.

Can it be a bug in Hibernate? So that the columns are not prefixed when creating the sql query from the HQL statement?

Btw, loading the object and updating it is not an option for me, because I don't want that the version of the object is incremented. Therefore I use a HQL with the version keyword.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 27, 2007 10:31 am 
Newbie

Joined: Thu Apr 08, 2004 4:28 am
Posts: 16
One remark, if I create a select HQL to retrieve an attribute of the derived class, it runs successfully. But the update HQL still fails.

Any suggestions?

Thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.