Hi all,
I'm trying to construct a Native SQL Query from Hibernate where the REAL sql is:
select pch.*
from product_category pc inner join product_category_hierarchy pch on pch.child_id = pc.id
where pc.aid = 4 and pch.parent_id=0;
My best attempt at this is:
Session ssn = TestSessionFactory.currentSession();
Query sqlQuery = ssn.createSQLQuery(
"select {pch.*} from product_category {pc} " +
"inner join product_category_hierarchy {pch} on {pch}.child_id = {pc}.id " +
"where {pc}.aid = 4 and {pch}.parent_id=0l",
"pch", ProductCategoryHierarchy.class);
sqlQuery.setMaxResults(50);
List list = sqlQuery.list();
Here are the significant aspects to my problem domain:
Hibernate version: 2.1.8
database: MySql v.4.1
ProductCategory.hbm.xml
<hibernate-mapping>
<class name="com.tll.valueobject.ProductCategory" table="product_category">
<meta attribute="extends">TimeStampValueObject</meta>
<id name="id" unsaved-value="null" column="id" type="long">
<generator class="hilo"/>
</id>
<property name="dateCreated" column="date_created" type="date" not-null="true"/>
<property name="dateLastModified" column="date_last_modified" type="date" not-null="true"/>
<property name="name" column="name" type="string" length="128" not-null="true"/>
<property name="description" column="description" type="string" length="128"/>
<property name="image" column="image" type="string" length="64"/>
<many-to-one name="account" class="com.tll.valueobject.Account" column="aid" foreign-key="fk_pc_a"/>
</class>
</hibernate-mapping>
ProductCategoryHierarchy.hbm.xml:
<hibernate-mapping>
<class name="com.tll.valueobject.ProductCategoryHierarchy" table="product_category_hierarchy">
<meta attribute="extends">ValueObject</meta>
<id name="id" unsaved-value="null" column="id" type="long">
<generator class="hilo"/>
</id>
<many-to-one name="parentId" class="com.tll.valueobject.ProductCategory" column="parent_id"/>
<many-to-one name="childId" class="com.tll.valueobject.ProductCategory" column="child_id"/>
</class>
</hibernate-mapping>
DB STRUCT:
create table product_category (
id bigint not null,
date_created date not null,
date_last_modified date not null,
name varchar(128) not null,
description varchar(128),
image varchar(64),
aid bigint,
primary key (id)
);
create table product_category_hierarchy (
child_id bigint not null,
parent_id bigint not null
);
I know this is verbose. Any guidance would be appreciated. TIA!
jon
jopaki@yahoo.com