[Hibernate-JIRA] Created: (HHH-5719) Applying projection to criteria with restricted subcriterias generates wrong SQL
by Victor Cherkassky (JIRA)
Applying projection to criteria with restricted subcriterias generates wrong SQL
--------------------------------------------------------------------------------
Key: HHH-5719
URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-5719
Project: Hibernate Core
Issue Type: Bug
Components: query-criteria
Affects Versions: 3.2.0.ga
Environment: Hibernate 3.2.0 GA
Reporter: Victor Cherkassky
Priority: Minor
Detailed issue description and solution is here http://facingtech.blogspot.com/2010_09_01_archive.html
I will describe only the technical aspect of the issue here.
Considuer the following model.
There is an entity {{User}}, which is in relation with {{Workplace}}. While {{Workplace}} has an {{Activity}} (the purpose of this workplace). Each {{Activity}} has a collection of {{Description}} entities ({{Description}} entities are just the same {{Activity}} description translated into several languages, thus having locale property).
Result of the query should look like this:
||User.name||Workplace.name||Activity.code||Description.text||
|John|Main office|R|Research|
|Polly|Main office|R|Research|
|Craig|Main office|R|Research|
Here is the criteria for getting that result:
{code}
Criteria criteria = session.createCriteria(User.class);
criteria.createAlias("workplace.activity.descriptions", "descriptionsAlias", Criteria.INNER_JOIN);
criteria.add(Restrictions.eq("descriptionsAlias.locale", "en_US"));
criteria.list();
{code}
It works just fine. Produces the table shown above and generates SQL like this:
{code}
select
this_.id as id18_3_,
this_.name as name18_3_,
this_.workplaceId as workplac3_18_3_,
workplace3_.id as id111_0_,
workplace3_.activityId as activityId111_0_,
workplace3_.name as name111_0_,
activity4_.id as id114_1_,
activity4_.code as code114_1_,
descriptio1_.id as id24_2_,
descriptio1_.locale as locale24_2_,
descriptio1_.text as text24_2_
from
user this_
left outer join
workplace workplace3_
on this_.workplaceId=workplace3_.id
left outer join
activity activity4_
on workplace3_.activityId=activity4_.id
inner join
description descriptio1_
on activity4_.id=descriptio1_.activityId
where
descriptio1_.locale=?
{code}
While if you want paging, you should make a rowcount projection first, so your query will look like this:
{code}
Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(User.class);}}
criteria.createAlias("workplace.activity.descriptions", "descriptionsAlias", criteria.INNER_JOIN);
criteria.add(Restrictions.eq("descriptionsAlias.locale", "en_US"));
criteria.setProjection(Projections.rowCount());
Integer rowCount = (Integer) criteria.uniqueResult();
{code}
And you expect Hibernate to produce this SQL:
{code}
select
count(*) as y0_
from
user this_
left outer join
workplace workplace3_
on this_.workplaceId=workplace3_.id
left outer join
activity activity4_
on workplace3_.activityId=activity4_.id
inner join
description descriptio1_
on activity4_.id=descriptio1_.activityId
where
descriptio1_.locale=?
{code}
But the SQL produced is this:
{code}
select
count(*) as y0_
from
user this_
where
descriptio1_.locale=?
{code}
I suppose that this is a bug, because you expect one behavior, while you get another. I don't think, that this is a big deal, but when you experience such an issue, it is time consuming to get it fixed.
Here is a workaround (or fix) of the issue. You should always assign aliases to all associated "links" of the "entities chain" of the path to an entity you want to restrict if you use projections. In other words, if you have anything in where clause and you are using projections, you should assign aliases to all associations between root entity and the entity, fields of which are used in where clause.
This criteria
{code}
Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(User.class);
criteria.createAlias("workplace", "workplaceAlias");
criteria.createAlias("workplaceAlias.activity", "activityAlias");
criteria.createAlias("activityAlias.descriptions", "descriptionsAlias", Criteria.INNER_JOIN);
criteria.add(Restrictions.eq("descriptionsAlias.locale", "en_US"));
criteria.setProjection(Projections.rowCount());
Integer rowCount = (Integer) criteria.uniqueResult();
{code}
Produces the right result
{code}
select
count(*) as y0_
from
user this_
inner join
workplace workplacea1_
on this_.workplaceId=workplacea1_.id
inner join
activity activityal2_
on workplacea1_.activityId=activityal2_.id
inner join
description descriptio3_
on activityal2_.id=descriptio3_.activityId
where
descriptio3_.locale=?
{code}
--
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
12 years, 5 months
[Hibernate-JIRA] Created: (OGM-112) ConcurrentModificationException while loading associations under load
by Sanne Grinovero (JIRA)
ConcurrentModificationException while loading associations under load
---------------------------------------------------------------------
Key: OGM-112
URL: http://opensource.atlassian.com/projects/hibernate/browse/OGM-112
Project: Hibernate OGM
Issue Type: Bug
Reporter: Sanne Grinovero
{quote}class: class java.util.ConcurrentModificationException
cause: null
java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793)
at java.util.HashMap$KeyIterator.next(HashMap.java:828)
at java.util.AbstractCollection.addAll(AbstractCollection.java:305)
at org.hibernate.ogm.datastore.spi.Association.getKeys(Association.java:132)
at org.hibernate.ogm.loader.OgmLoader.getResultSet(OgmLoader.java:417)
at org.hibernate.ogm.loader.OgmLoader.doQuery(OgmLoader.java:248)
at org.hibernate.ogm.loader.OgmLoader.doQueryAndInitializeNonLazyCollections(OgmLoader.java:215)
at org.hibernate.ogm.loader.OgmLoader.loadCollection(OgmLoader.java:185)
at org.hibernate.ogm.loader.OgmBasicCollectionLoader.initialize(OgmBasicCollectionLoader.java:42)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:622)
at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:82)
at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1606)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:379)
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:112)
at org.hibernate.collection.internal.PersistentSet.iterator(PersistentSet.java:180){quote}
--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira
12 years, 5 months
[Hibernate-JIRA] Created: (HHH-4915) <list-index> base attribute ignored on insert for a bidirectional association (one-to-many)
by Olivier Lafontaine (JIRA)
<list-index> base attribute ignored on insert for a bidirectional association (one-to-many)
-------------------------------------------------------------------------------------------
Key: HHH-4915
URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-4915
Project: Hibernate Core
Issue Type: Bug
Components: core
Affects Versions: 3.5.0-CR-1, 3.3.2
Environment: Hibernate 3.3.2GA and 3.5.0-CR-1
Oracle Database
Reporter: Olivier Lafontaine
Attachments: hibernate-poc-jira.zip
My mapping looks like the following:
{code:xml}
<hibernate-mapping>
<class name="Parent">
<!-- SNIP -->
<list name="children" table="CHILD" cascade="all-delete-orphan">
<key column="ID_PARENT" not-null="true" />
<list-index base="1">
<column name="IDX" check="IDX > 0" />
</list-index>
<one-to-many class="Child" />
</list>
</class>
<class name="Child">
<!-- SNIP -->
<many-to-one name="parent" class="Parent" column="ID_PARENT"
not-null="true" insert="false" update="false" />
</class>
</hibernate-mapping>
{code}
As you can see from the mapping, we have a parent entity ({{Parent}}) referring child entities ({{Child}} using the {{children}} accessor). The index column ({{ORDER}}) is not an attribute in the child entity, so I referred to the second use case in section _6.3.3. Bidirectional associations with indexed collections_ of the reference guide to do my mapping.
Everything works except for the index base, it is always 0 based on insert. I've tried both Hibernate 3.3.2GA and the latest development version (3.5.0-CR-1), the issue can be reproduced in both.
I've joined a test case to reproduce the problem. While working on it, I realized there are updates after the inserts setting the correct index. However, we cannot change the database schema and there's a constraint on the index column enforcing a value > 0. So this issue is really important for us. I could manually handle the index values, but it would be so much easier to let Hibernate do it.
I've traced the problem and it seems that {{StatefulPersistenceContext#getIndexInParent(..)}} does not considers the index base. Why are updates necessary after the inserts? It looks to me like Hibernate could correctly set the index parameter on the insert statements.
--
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
12 years, 5 months
[Hibernate-JIRA] Created: (HHH-5072) Invalid SQL produced when using FetchMode.SUBSELECT with SQLServerDialect
by kabram (JIRA)
Invalid SQL produced when using FetchMode.SUBSELECT with SQLServerDialect
-------------------------------------------------------------------------
Key: HHH-5072
URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-5072
Project: Hibernate Core
Issue Type: Bug
Components: core
Affects Versions: 3.5.0-Final
Environment: Hibernate core 3.5, JPA 2.0, SQL Server 2005, JDK 1.6
Reporter: kabram
Priority: Blocker
I have a parent entity with child entities being loaded using SubSelect and eager fetching. I execute a query to retrieve all parents and get the following query which SQL Server 2005 does not support:
select
childtab0_.CONFIG_KEY as CONFIG5_23_1_,
childtab0_.QUALIFIER as QUALIFIER23_1_,
childtab0_.CONFIG_VALUE_ID as CONFIG1_1_,
childtab0_.CONFIG_VALUE_ID as CONFIG1_24_0_,
childtab0_.CONFIG_KEY as CONFIG5_24_0_,
childtab0_.QUALIFIER as QUALIFIER24_0_,
childtab0_.CONFIG_VALUE as CONFIG2_24_0_,
childtab0_.IS_ENCRYPTED as IS3_24_0_,
childtab0_.LIST_ORDER as LIST4_24_0_
from
TEST_CHILD_TABLE childtab0_
where
(childtab0_.CONFIG_KEY, childtab0_.QUALIFIER) in
(select parenttab0_.CONFIG_KEY, parenttab0_.QUALIFIER
from RSS_NEO_CANYON_CONFIG_KEY parenttab0_ )
order by
childtab0_.LIST_ORDER asc
SQL Server does not like the tuple in the where 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
12 years, 5 months
[Hibernate-JIRA] Created: (HCANN-30) EntityBinder: Incorrect WARN message about dynamicUpdate ("Entity used on a non root entity: ignored for...")
by Shahaf Abileah (JIRA)
EntityBinder: Incorrect WARN message about dynamicUpdate ("Entity used on a non root entity: ignored for...")
-------------------------------------------------------------------------------------------------------------
Key: HCANN-30
URL: http://opensource.atlassian.com/projects/hibernate/browse/HCANN-30
Project: Hibernate Commons Annotations
Issue Type: Bug
Reporter: Shahaf Abileah
Priority: Minor
This my first bug so please excuse any mistakes I make in filing it, and help me correct them.
I'm using PostgreSQL. I have a bunch of domain objects. Some of them are small class hierarchies. I chose to map each class hierarchy to a single table using a discriminator column. Here's one example where I have a base class (Note) and a subclass (TaskNote)...
@Entity
@org.hibernate.annotations.Entity(dynamicUpdate = true)
@Table(name = "notes")
@SequenceGenerator(name = "notes_note_id_seq", sequenceName = "notes_note_id_seq", allocationSize = 1)
@Proxy(lazy = true)
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@SQLDelete(sql="update notes set removed = true where note_id = ?")
@DiscriminatorColumn(
name=Note.noteTypeColumnName,
discriminatorType=DiscriminatorType.INTEGER
)
public abstract class Note implements Persistable {
...
}
@Entity
@org.hibernate.annotations.Entity(dynamicUpdate = true)
@Proxy(lazy = true)
@DiscriminatorValue("1")
public class TaskNote extends Note {
...
}
Notice that I added the following annotation to both: @org.hibernate.annotations.Entity(dynamicUpdate = true)
I did this because I want to keep my updates lean.
After adding this annotation I started seeing warnings in my log:
2010-11-01 10:03:22,210 WARN org.hibernate.cfg.annotations.EntityBinder @org.hibernate.annotations.Entity used on a non root entity: ignored for redfin.core.domain.TaskNote
I spent some time experimenting with this annotation to understand how it works with class hierarchies. I discovered that:
* If I put the annotation on the child class and not on the parent class, then I get the WARN but dynamicUpdate still works
* If I put the annotation on the parent class and not the child class, then the WARN goes away but dynamicUpdate does not work
* If I put the annotation on both, then the WARN is there and dynamicUpdate works
To me this implies that the WARN is incorrect. Do you agree?
I believe this is the code that is generating the message:
http://www.docjar.com/html/api/org/hibernate/cfg/annotations/EntityBinder...
Here are the particular versions of the various hibernate libraries that I'm using:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>3.0.0.ga</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.7.ga</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.3.0.ga</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.3.0.ga</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>antlr</artifactId>
<version>2.7.5H3</version>
</dependency>
--
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
12 years, 5 months
[Hibernate-JIRA] Created: (HHH-2666) subselect fetching ignores max results
by James Roper (JIRA)
subselect fetching ignores max results
--------------------------------------
Key: HHH-2666
URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-2666
Project: Hibernate3
Issue Type: Improvement
Components: query-hql, query-sql
Affects Versions: 3.2.1
Environment: Hibernate 3.2.1
HSQLDB 8.0
Reporter: James Roper
Priority: Minor
When maxResults is set for a query, the hibernate subselect fetching strategy ignores it. For example, I have a class entry, that has a list of comments, which are configured to use the subselect fetching strategy. You can see that in the original query, only the top ? results are fetched:
select
top ? entry0_.id as id0_,
entry0_.title as title0_,
entry0_.entry as entry0_,
entry0_.time as time0_,
entry0_.category_id as category5_0_
from
Entry entry0_
order by
entry0_.time desc
But then when it does the sub select query to lazily load the comments, this is the query it runs:
select
comments0_.entry_id as entry5_1_,
comments0_.id as id1_,
comments0_.id as id1_0_,
comments0_.time as time1_0_,
comments0_.comment as comment1_0_,
comments0_.author as author1_0_
from
Comment comments0_
where
comments0_.entry_id in (
select
entry0_.id
from
Entry entry0_
)
order by
comments0_.time desc
So, it loads every single comment in the database, even though only the comments for the top ? entries are needed. Of course, if there was no order by clause on the first query, the sub select may not return the same results, so it should probably only do this when there is an order by clause in the first query, and it should make sure it includes the order by clause in the sub select.
--
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
12 years, 5 months
[Hibernate-JIRA] Created: (ANN-609) Need a way to specify "unsaved-value" strategy using annotations
by Suji Suresh (JIRA)
Need a way to specify "unsaved-value" strategy using annotations
----------------------------------------------------------------
Key: ANN-609
URL: http://opensource.atlassian.com/projects/hibernate/browse/ANN-609
Project: Hibernate Annotations
Issue Type: Bug
Environment: Hibernate 3.2.2, Hibernate-annotations 3.2.1
Reporter: Suji Suresh
Hibernate has two ways of specifying a value for primary key:
1. Assign a value before handing it over to Hibernate
2. Have Hibernate generate the value before persisting
In my project I have objects of both of the above specified types. In other words I assign value for the primary key for some the objects (lets call these objects "assigned") and for others I have Hibernate generate the value before persisting (lets call these objects "generated"). Since Hibernate annotations does not support "unsaved-value", I do not have a way of specifying "unsaved-value" strategy. While my "generated" objects work perfectly with Hibernate's default "unsaved-value" strategy, I see wrong (but expected) behaviour when I try to persist an "assigned" object that is already present in the database in that, I get StaleStateException instead of DataIntegrityViolationException
--
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
12 years, 5 months