[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-1901) Filtering on superclass property problem

Juan Ignacio Cidre (JIRA) noreply at atlassian.com
Wed Aug 30 15:08:25 EDT 2006


    [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-1901?page=comments#action_24237 ] 

Juan Ignacio Cidre commented on HHH-1901:
-----------------------------------------

Here I have a minimum test case:

Class files:

package test;

import java.util.Set;

public class MyBean {

	private Long id;
	private String name;
	private Set<MySubclass> subclasses;
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Set<MySubclass> getSubclasses() {
		return subclasses;
	}
	public void setSubclasses(Set<MySubclass> subclasses) {
		this.subclasses = subclasses;
	}	
}

package test;

public class MySuperclass {

	private Long id;
	private String name;
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

package test;

public class MySubclass extends MySuperclass {

	private String anotherProperty;

	public String getAnotherProperty() {
		return anotherProperty;
	}

	public void setAnotherProperty(String anotherProperty) {
		this.anotherProperty = anotherProperty;
	}
	
}

Mapping files:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

	<class name="test.MyBean"
		   table="mybean">
		
        <id name="id" type="long" unsaved-value="null" >
            <column name="ID" sql-type="NUMBER(4)" not-null="true"/>
        </id>
		<property name="name" />

		<set name="subclasses" >
			<key column="myBeanId"/>
			<one-to-many class="test.MySubclass"/>
			<filter name="nameFilter"/>
		</set>		
   </class>
   
   <filter-def name="nameFilter" condition="name = 'TheOne'"/>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
	<class name="test.MySuperclass" table="mysuperclass">
			
        <id name="id" type="long" unsaved-value="null" >
            <column name="ID" sql-type="NUMBER(4)" not-null="true"/>
        </id>

   		<property name="name"/>

   </class>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

		<joined-subclass name="test.MySubclass"
						table="mySubclass"
						extends="test.MySuperclass">
						
			<key column="id" />
            <property name="anotherProperty"/>
		</joined-subclass>
		
</hibernate-mapping>

DB scripts (for oracle):

CREATE TABLE mybean(
	id		NUMBER(10) NOT NULL,
	name	VARCHAR2(128) NOT NULL
);

ALTER TABLE mybean
ADD CONSTRAINT mybeanid
PRIMARY KEY(id);

CREATE TABLE mysuperclass(
	id		NUMBER(10) NOT NULL,
	name	VARCHAR2(10) NOT NULL
);

ALTER TABLE mysuperclass
ADD CONSTRAINT mysuperclassid
PRIMARY KEY(id);

CREATE TABLE mysubclass(
	id				NUMBER(10) NOT NULL,
	anotherProperty VARCHAR2(10) NOT NULL,
	myBeanId		NUMBER(10) NOT NULL
);

ALTER TABLE mysubclass
ADD CONSTRAINT mysubclassid
PRIMARY KEY(id);

ALTER TABLE mysubclass
ADD CONSTRAINT mysubclassidfk
FOREIGN KEY (id) REFERENCES mysuperclass(id);

ALTER TABLE mysubclass
ADD CONSTRAINT mysubclassbeanidfk
FOREIGN KEY (myBeanId) REFERENCES mybean(id);

INSERT INTO mybean VALUES(1, 'myBean');

INSERT INTO mysuperclass VALUES(1, 'sclass 1');
INSERT INTO mysubclass VALUES(1, 'property 1', 1);

INSERT INTO mysuperclass VALUES(2, 'sclass 2');
INSERT INTO mysubclass VALUES(2, 'property 2', 1);

Test code:

//First the session factory must be obtained

//Open session
Session session = factory.openSession();
//enables the filter
session.enableFilter("nameFilter");

MyBean myBean = (MyBean)session.load(MyBean.class.getName(), new Long(1));

//Here the exception occurs		
myBean.getSubclasses().size();


When the set is initialized the SQL exception is thrown, here is the malformed query:

select subclasses0_.myBeanId as myBeanId1_, 
            subclasses0_.id as id1_, 
            subclasses0_.id as ID136_0_, 
            subclasses0_1_.name as name136_0_, 
            subclasses0_.anotherProperty as anotherP2_137_0_ 
from mySubclass subclasses0_ 
inner join mysuperclass subclasses0_1_ on subclasses0_.id=subclasses0_1_.ID 
where  subclasses0_.name = 'TheOne' and subclasses0_.myBeanId=?

The problem here is in the first condition of the where clause, as you can see the column name does not exist in mySubclass table, but in mySuperclass table.

Hope this is clear enough, otherwise let me know.

> Filtering on superclass property problem
> ----------------------------------------
>
>          Key: HHH-1901
>          URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-1901
>      Project: Hibernate3
>         Type: Bug

>     Versions: 3.1.3
>  Environment: Hibernate 3.1.3  on Oracle 9i
>     Reporter: Juan Ignacio Cidre

>
>
> I have three classes Order, Advertisements and Creatives.
> Advertisements is subclass of Creatives
> Order has a one-to-many to Advertisements
> That one-to-many has a filter. The condition has to do with a property of Creative, the Advertisements superclass.
> This is the Order to Advertisements mapping with a filter on deletionStatusId that is a field of Creative.
>         <set name="ads" inverse="true" lazy="true">
>             <key column="orderId"/>
>             <one-to-many class="com.inceptor.domain.msn.Advertisement"/>
>             <filter name="deletionStatusFilter" condition="deletionStatusId = 0"/>
>         </set>       
> This is the Creative mapping with the deletionStatusId, which is mapped as a component. It is a Enum in Java.
>            <component name="deletionStatus" 
> class="com.inceptor.domain.core.DeletionStatusType">
>                <property name="number" column="deletionStatusId" />
>            </component>
> I would expected an sql that appends a where condition like [superclass (Creative) table alias].deletionStatusId  instead I received [subclass (Advertisements) table alias].deletionStatusId
> This generates a DB error.
> Follows the generated query
> Note ads0_ instead of ads0_1_ 
> select ads0_.orderId as orderId1_, ads0_.id as id1_, ads0_.id as ID324_0_, ads0_1_.version as version324_0_, ads0_1_.searchEngineAccountId as searchEn3_324_0_, ads0_1_.creationTime as creation4_324_0_, ads0_1_.creatorId as creatorId324_0_, ads0_1_.modificationTime as modifica6_324_0_, ads0_1_.synchTime as synchTime324_0_, ads0_1_.modifierId as modifierId324_0_, ads0_1_.deletionStatusId as deletion9_324_0_, ads0_1_.deleterId as deleterId324_0_, ads0_1_.pushError as pushError324_0_, ads0_.seId as seId418_0_, ads0_.title as title418_0_, ads0_.description as descript4_418_0_, ads0_.displayURL as displayURL418_0_, ads0_.destinationURL as destinat6_418_0_, ads0_.originalDestinationURL as original7_418_0_, ads0_.orderId as orderId418_0_ from MSN_Advertisements ads0_ inner join GTK_Creatives ads0_1_ on ads0_.id=ads0_1_.ID where  ads0_.deletionStatusId = 0 and ads0_.orderId=?

-- 
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.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira




More information about the hibernate-issues mailing list