[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-3891) one-to-one with property-ref always non-lazy
Sandeep Vaid (JIRA)
noreply at atlassian.com
Wed May 6 02:14:18 EDT 2009
[ http://opensource.atlassian.com/projects/hibernate/browse/HHH-3891?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=33107#action_33107 ]
Sandeep Vaid commented on HHH-3891:
-----------------------------------
Diego,
I read the links provided by you.. but i have some more to day :)
Consider this scenatio :
I have one-to-one relation between Product and ProductBasic BUT at database level i have one-to-many between
PRODUCT and PRODUCTBASIC tables as history is mainted in the same table :
PRODUCT ----> PID (PK)
PRODUCTBASIC ---> PID (FK), USECODE , STARTTIMESTAMP forms Composite-key
PRODUCT
PID PNAME
1 Pen
PRODUCTBASIC
PID USECODE STARTTIME ENDTIME
1 100 06-MAY-2009 11:00 07-MAY-2009 10:00
1 100 07-MAY-2009 10:00 31-DEC-9999 00:00
Now while querying for ProductBasic, i will always fetch the row which satisfies the condition STARTTIME<CURRENTTIME<ENDTIME
and this will surely return me only one row.
Ideally the generated sql query should be :
select * from PRODUCT product left outer join PRODUCTBASIC productbasic on '100'=productbasic.USETYPE
and product.PID=productbasic.PID
and (
productbasic.STARTTIME<'09-MAY-2009 10:00'
and '09-MAY-2009 10:00'<productbasic.ENDTIME
)
where
product.PID='1'
PLEASE NOTE THAT i am not using the composite-key to establish the relationship. Instead i am using
1) product.PID=productbasic.PID
2) productbasic.USETYPE='100'
3) productbasic.STARTTIME<'09-MAY-2009 10:00' and '09-MAY-2009 10:00'<productbasic.ENDTIME
This last condition can only be provided at runtime as we will get the currenTime value at runtime only.
Now, if we allow productBasic to be fetched non-lazyly compulsary, it will form the query using only 1) & 2) conditions
and it will not take condition 3) as it is not provide in .hbm.xml file..
The incorrect query thus formed will be
select * from PRODUCT product left outer join PRODUCTBASIC productbasic on '100'=productbasic.USETYPE
and product.PID=productbasic.PID where product.PID='1'
This incorrect query will fetch more than one rows and throw an error:
org.hibernate.HibernateException: More than one row with the given identifier was found: for class ProductBasic.
So now 2 things are must needed
1) one-to-one with property-ref should be allowed to be loaded lazily.
2) one-to-one should allow the use of filters (so that runtime conditions can be specified, 3) condition in my case)
> one-to-one with property-ref always non-lazy
> --------------------------------------------
>
> Key: HHH-3891
> URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-3891
> Project: Hibernate Core
> Issue Type: Bug
> Components: core
> Reporter: Sandeep Vaid
> Assignee: Diego Plentz
>
> I have one-to-one relationship betnwee Product and ProductBasic.
> <one-to-one name="productBasic" cascade="save-update" property-ref="activeProduct" lazy="proxy">
> <formula>'1'</formula>
> <formula>PRODUCTID</formula>
> </one-to-one>
>
>
> ProductBasic.hbm.xml as :
> <properties name="activeProduct">
> <property name="Code" column="CODE"></property>
> <property name="pId" column="PID" insert="false" update="false"></property>
> </properties>
>
>
> When i am using property-ref, and try to load Product, it eagerly fetches ProductBasic also as :
>
> select *
> from PRODUCT productbo0_ left outer join PRODUCTPERIOD productbas1_ on '18'=productbas1_.CODE and productbo0_.PID=productbas1_.PID where productbo0_.PID=?
>
> NOTE: Along with Product, ProductBasic is also fetched in a single query.
>
> Even if i add constrained="true" in one-to-one mapping, the query gets fired as :
> select * from PRODUCT productbo0_ where productbo0_.PID=?
> select * from PRODUCTBASIC productbas0_ where productbas0_.CODE=? and productbas0_.PID=?
>
> NOTE: Along with Product, ProductBasic is also fetched in a different query.
>
> BUT in both these cases, ProductBasic is fetched always (non-lazy)..
>
> How can i fetch ProductBasic in a Non-Lazy manner in this case.
--
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