问题
How can i write to HQL Query? This is My Original scenario I have two hbm files ADVMAgencyMaster.hbm.xml,ADVRoheader.hbm.xml corresponding pojo classes are ADVMAgencyMaster.java,ADVroheader.java.
<hibernate-mapping>
<class name="com.adv.hibernatebean.ADVMAgencyMaster" table="ADVMAGENCYMASTER" catalog="MEDIAERP">
<id name="mamaid" type="java.lang.String">
<column name="MAMAID" length="10" />
<generator class="assigned"></generator>
</id>
<property name="mamaname" type="java.lang.String">
<column name="MAMANAME" length="50" />
</property>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.adv.hibernatebean.ADVRoheader" table="ADVTROHEADER" schema="MEDIAERP">
<id name="trohiono" type="java.lang.String">
<column name="TROHIONO" />
<generator class="assigned"></generator>
</id>
<many-to-one name="advmagencymaster" class="com.adv.hibernatebean.ADVMAgencyMaster" fetch="select">
<column name="TROHAMAID" />
</many-to-one>
</hibernate-mapping>
My oracle query is
SELECT MAMAID,MAMANAME,TROHIONO
FROM ADVAGENCYMASTER,ADVROHEADER
WHERE MAMAID(+)=TROHAMAID
Match records from both table that condition is MAMAID(+)=TROHAMAID
in oracle. Pls tell how writer in HQL.
回答1:
Corresponding is HQL
select header.advmagencymaster.mamaid,
header.advmagencymaster.mamaname,header.trohiono
from ADVRoheader header
But In HQL it is better to retrieve entire object so that we can use the other properties also
from ADVRoheader header
inner join
header.advmagencymaster master
HQL is not depending on the underlying database.It will be same for all databases(Oracle,Mysql,SQL server etc).only We need to change the Database connection in the main configuration file.
Your model classes will look like this
class ADVMAgencyMaster{
private String mamaid;
private String mamaname; //getters and setters
}
class ADVRoheader{
private String trohiono;
private ADVMAgencyMaster advmagencymaster; // Reference to the ADVMAgencyMaster
//getters and setters}
You Already defined a many to one relation in xml
<many-to-one name="advmagencymaster" class="com.adv.hibernatebean.ADVMAgencyMaster" fetch="select"> <column name="TROHAMAID" /></many-to-one>
So while Loading ADVRoheader object hibernate will load the inner object reference "advmagencymaster" also.
for this you need to specify lazy="false" instead of fetch="select".
lazy="true" - lazy loading it will load only parent object
lazy-"false" - eager loading it will load child(inner) object also with parent object.
add both hbm.xml files in to main configuration file (hibernate.cfg.xml) as follows
<mapping resource="com/ADVRoheader.hbm.xml"></mapping>
<mapping resource="com/ADVRomaster.hbm.xml"></mapping>
just before session factory closing tag here the mapping is happening with Database.
Usually for generator elements we need to use type integer and make the change in database also.
Extra filtering we can add 'where' clause after the main query.Here only joining condition is required and it is already handled by hibernate.
回答2:
Please check this,
from ADVMAgencyMaster master
inner join
ADVRoheader header
Where master.mamaid = (userInputId)
With on Clause
from ADVMAgencyMaster master
inner join
ADVRoheader header on master.mamaid = header.trohamaid
Where master.mamaid = (userInputId) //optional, it will filter row as per user input
When the join type is inner , On Clause automatically acts/performs Where filter as well.
来源:https://stackoverflow.com/questions/18566206/joins-with-conditon-in-hql-query