[Hibernate-JIRA] Created: (HHH-3226) HQL/JPQL query with where clause expression involving mulitple correlated subqueries does not parse
by Bob Tiernay (JIRA)
HQL/JPQL query with where clause expression involving mulitple correlated subqueries does not parse
---------------------------------------------------------------------------------------------------
Key: HHH-3226
URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-3226
Project: Hibernate3
Issue Type: Bug
Affects Versions: 3.2.6
Reporter: Bob Tiernay
The following named query will not parse:
@NamedQuery(name = "Account.findTotalInactiveCount", query = "SELECT COUNT(a) FROM Account a WHERE a.status.name NOT IN ('nst', 'ncl') AND a.seed = FALSE AND (SELECT CURRENT_DATE - MAX(t.date) FROM Trade t WHERE a.id IN (t.purchaser.id, t.seller.id)) > 183 AND ((SELECT SUM(t1.asset.price.value * t1.numberOfUnits) FROM Trade t1 WHERE t1.purchaser.id = a.id) - (SELECT SUM(t2.totalPrice * t2.numberOfUnits) FROM Trade t2 WHERE t2.seller.id = a.id) + (SELECT SUM(gl.debit) - SUM(gl.credit) FROM GeneralLedger gl WHERE gl.glAccount.id = 15 AND gl.account.id = a.id)) > 150.00")
The error(s) issued from Hibernate are:
0 [main] ERROR org.hibernate.hql.PARSER - <AST>:0:0: unexpected AST node: query
15 [main] ERROR org.hibernate.hql.PARSER - <AST>:0:0: unexpected AST node: query
For readability, I have formatted the query:
SELECT COUNT(a)
FROM Account a
WHERE a.status.name NOT IN ('nst', 'ncl') AND
a.seed = FALSE AND
(
SELECT CURRENT_DATE - MAX(t.date)
FROM Trade t
WHERE a.id IN (t.purchaser.id, t.seller.id)
) > 183 AND
(
(SELECT SUM(t1.asset.price.value * t1.numberOfUnits)
FROM Trade t1
WHERE t1.purchaser.id = a.id) -
(SELECT SUM(t2.totalPrice * t2.numberOfUnits)
FROM Trade t2
WHERE t2.seller.id = a.id) +
(SELECT SUM(gl.debit) - SUM(gl.credit)
FROM GeneralLedger gl
WHERE gl.glAccount.id = 15 AND
gl.account.id = a.id)
) > 150.00
As an aside, I've tried reproducing this query using the Criteria API with no luck. There doesn't seem to be a way to combine the results of subqueries for comparison.
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators....
-
For more information on JIRA, see: http://www.atlassian.com/software/jira
14 years, 6 months
[Hibernate-JIRA] Created: (HHH-4594) SchemaExport does not ignore the hibernate.default_schema property if it is not set
by Stefan Rufer (JIRA)
SchemaExport does not ignore the hibernate.default_schema property if it is not set
-----------------------------------------------------------------------------------
Key: HHH-4594
URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-4594
Project: Hibernate Core
Issue Type: Bug
Components: core
Affects Versions: 3.3.1
Environment: Hibernate 3.3.1.GA, derby 10.4.2.0, Spring 2.5.6.SEC01
Reporter: Stefan Rufer
Priority: Minor
Our datasource configuration contains a hibernate.default_schema property that may not be set by the application. In this case the generated SQL (select, insert) behaves correct and does not prepend the table names with a schema names. Example:
select project0_.id as id2_0_, project0_.name as name2_0_, project0_.version as version2_0_ from Project project0_ where project0_.id=?
However, SchemaExport does prepend the table name with the unresolved property name instead of omitting it if not available:
2009-11-18 22:38:24,263 | main | ERROR | SchemaExport | Unsuccessful: create table ${hibernate.default_schema}.Person (id varchar(255) not null, name varchar(255), primary key (id))
In my oppinion the behaviour should be consistent and I tend to say that the SchemaExport is wrong. If the property for the default schema is unresolved it must not be used altogether.
As an illustration a snip from our Spring config:
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"
value="classpath:my.properties" />
<!-- this means I can omit properties used below -->
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.dialect" value="${hibernate.dialect}" />
<entry key="hibernate.show_sql" value="${hibernate.show_sql}" />
<entry key="hibernate.hbm2ddl.auto" value="${hibernate.hbm2ddl.auto}" />
<entry key="hibernate.format_sql" value="${hibernate.format_sql}" />
<entry key="hibernate.generate_statistics" value="${hibernate.generate_statistics}" />
<entry key="hibernate.use_sql_comments" value="${hibernate.use_sql_comments}" />
<entry key="hibernate.default_batch_fetch_size" value="${hibernate.default_batch_fetch_size}" />
<entry key="hibernate.cache.provider_class" value="${hibernate.cache.provider_class}" />
<entry key="hibernate.cache.use_second_level_cache" value="${hibernate.cache.use_second_level_cache}" />
<entry key="hibernate.default_schema" value="${hibernate.default_schema}" />
</map>
</property>
<property name="persistenceUnitName" value="${jpa.persistenceUnitName}" />
</bean>
my.properties:
jpa.persistenceUnitName=default
hibernate.show_sql=false
hibernate.format_sql=false
hibernate.use_sql_comments=false
hibernate.hbm2ddl.auto=false
hibernate.generate_statistics=false
hibernate.default_batch_fetch_size=16
hibernate.cache.provider_class=net.sf.ehcache.hibernate.EhCacheProvider
hibernate.cache.use_second_level_cache=true
# hibernate.default_schema not defined for the standard case. only define it if needed.
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators....
-
For more information on JIRA, see: http://www.atlassian.com/software/jira
14 years, 6 months
[Hibernate-JIRA] Created: (HHH-2421) Cascading Delete In Wrong Order
by CannonBall (JIRA)
Cascading Delete In Wrong Order
-------------------------------
Key: HHH-2421
URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-2421
Project: Hibernate3
Type: Bug
Components: core
Versions: 3.2.1
Environment: Hibernate 3.2.1, Java5, MySQL 5 (InnoDB)
Reporter: CannonBall
Priority: Trivial
Mapping Document:
<hibernate-mapping>
<class name="scratchpad.hibernate.A">
<id name="id">
<generator class="increment"/>
</id>
<list name="bs" cascade="all,delete-orphan">
<key column="bId"/>
<list-index column="idx"/>
<one-to-many class="scratchpad.hibernate.B"/>
</list>
</class>
<class name="scratchpad.hibernate.B">
<id name="id">
<generator class="increment"/>
</id>
<many-to-one name="a" column="aId" insert="false" update="false"/>
<many-to-one name="c" column="cId" not-null="false"/>
</class>
<class name="scratchpad.hibernate.C">
<id name="id">
<generator class="increment"/>
</id>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
long id;
SessionFactory factory = new Configuration().configure()
.buildSessionFactory();
try {
Session s = factory.openSession();
try {
Transaction tx = s.beginTransaction();
try {
C c = new C();
s.save(c);
B b = new B();
b.setC(c);
A a = new A();
a.getBs().add(b);
s.save(a);
tx.commit();
id = b.getId();
} catch (Exception e) {
try {
tx.rollback();
} catch (Exception e2) {
// do nothing
}
throw e;
}
} finally {
s.close();
}
s = factory.openSession();
try {
Transaction tx = s.beginTransaction();
try {
A a = (A) s.load(A.class, id);
B b = a.getBs().get(0);
a.getBs().remove(b);
s.delete(b.getC());
tx.commit();
} catch (Exception e) {
try {
tx.rollback();
} catch (Exception e2) {
// do nothing
}
throw e;
}
} finally {
s.close();
}
} finally {
factory.close();
}
The generated SQL (show_sql=true):
Hibernate: select max(id) from C
Hibernate: select max(id) from A
Hibernate: select max(id) from B
Hibernate: insert into C (id) values (?)
Hibernate: insert into A (id) values (?)
Hibernate: insert into B (cId, id) values (?, ?)
Hibernate: update B set bId=?, idx=? where id=?
Hibernate: select a0_.id as id0_0_ from A a0_ where a0_.id=?
Hibernate: select bs0_.bId as bId1_, bs0_.id as id1_, bs0_.idx as idx1_, bs0_.id as id1_0_, bs0_.aId as aId1_0_, bs0_.cId as cId1_0_ from B bs0_ where bs0_.bId=?
Hibernate: select c0_.id as id2_0_ from C c0_ where c0_.id=?
Hibernate: update B set cId=? where id=?
Hibernate: update B set bId=null, idx=null where bId=?
Hibernate: delete from C where id=?
Hibernate: delete from B where id=?
When you have a collection that is mapped with a cascade of 'delete-orphan', when removing an entity from the collection, the corresponding orphan delete is scheduled at the end of the session's deletions queue. As you can see from my example above, when you have a relationship of A has a list of B's, B has a relationship with C, removing B from the A's list results in its deletion after C's deletion (despite the order of statements dictating C's deletion after B's). If I were to make B's relationship to C not-null, the above code would result in a FK constraint error as C would be removed before B.
You could force the correct removal of B before C with a manual delete of B like so:
A a = (A) s.load(A.class, id);
B b = a.getBs().get(0);
C c = b.getC();
a.getBs().remove(b);
s.delete(b);
s.delete(c);
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://opensource.atlassian.com/projects/hibernate/secure/Administrators....
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira
14 years, 6 months
[Hibernate-JIRA] Created: (HHH-2390) select clause alias in HQL is mapped incosistently in SQL.
by Natto Lover (JIRA)
select clause alias in HQL is mapped incosistently in SQL.
----------------------------------------------------------
Key: HHH-2390
URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-2390
Project: Hibernate3
Type: Bug
Components: query-hql
Versions: 3.2.2
Environment: Win2K+JDK1.5+Eclipse3.2.1+Hibernate3.2.2, Solaris Express x86+MySQL5
Reporter: Natto Lover
Attachments: HQLTest.zip
Hi. I was told at the users forum to post a test case here.
I give an alias to an expression in the select clause, try to refer it in the where clause. In SQL, Hibernate replaces the alias in the select clause, but leaves the original text in the where clause.
This is the HQL:
select (p.endDate - p.startDate) as period, p
from Project as p
where period > :period_length
See above where the alias 'period' appears.
Now, This is the resulting SQL: select
project0_.end_date-project0_.start_date as col_0_0_,
project0_.id as col_1_0_,
project0_.id as id0_,
project0_.name as name0_,
project0_.start_date as start3_0_,
project0_.end_date as end4_0_
from PROJECT project0_ where period>?
Note above the 'period' that was in the select clause is replaced with machine generated "col_0_0_", but that is not applied for the alias in the where clause.
Please find attached a test case archive.
Two HQL statements are tried. One uses the alias in the where clause, the other one uses the alias in the order by clause.
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://opensource.atlassian.com/projects/hibernate/secure/Administrators....
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira
14 years, 6 months
[Hibernate-JIRA] Updated: (HHH-892) HQL parser does not resolve alias in ORDER BY clause
by Chris Wilson (JIRA)
[ http://opensource.atlassian.com/projects/hibernate/browse/HHH-892?page=co... ]
Chris Wilson updated HHH-892:
-----------------------------
Attachment: HibernateHavingAliasTest.java
This is also a problem for HAVING columns. See the attached test case for a very simple example, which generates the following query on MySQL:
select house0_.id as col_0_0_,
SUM(case cats1_.sex when 'M' then 1 else 0 end) as col_1_0_,
SUM(case cats1_.sex when 'F' then 1 else 0 end) as col_2_0_
from House house0_
inner join Cat cats1_ on house0_.id=cats1_.house_id
group by house0_.id
having num_male<>num_female;
generating the same error message:
WARN JDBCExceptionReporter: SQL Error: 1054, SQLState: 42S22
ERROR JDBCExceptionReporter: Unknown column 'num_male' in 'having clause'
In my view, using the Hibernate generated aliases (col_1_0_ and col_2_0_) in the having clause is not a very good workaround, because they are not specified by the standard and could change at any time.
Since I provided an explicit alias name, I would like it to be used in the SQL query, but failing that, I hope it would be possible to resolve aliases in the HAVING clause.
> HQL parser does not resolve alias in ORDER BY clause
> -----------------------------------------------------
>
> Key: HHH-892
> URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-892
> Project: Hibernate Core
> Issue Type: Bug
> Components: query-hql
> Affects Versions: 3.0.5
> Environment: Hibernate 3.0.5, MySQL, Tomcat
> Reporter: Guido Laures
> Priority: Minor
> Attachments: HibernateHavingAliasTest.java
>
>
> When using an alias for an ORDER BY clause this is not always correctly resolved. Example:
> SELECT SUM(A.x) AS mySum FROM MyClass AS A GROUP BY A.y ORDER BY mySum
> does not work because "mySum" is not resolved in the ORDER BY clause which results in an exception telling that mySum is an unknown column.
> Workaround (not to say "hack") is using:
> SELECT SUM(A.x) AS mySum FROM MyClass AS A GROUP BY A.y ORDER BY col_0_0_
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators....
-
For more information on JIRA, see: http://www.atlassian.com/software/jira
14 years, 6 months