[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-4982) JPA OneToOne(optional=true) not supported, supported alternative NotFound() is hibernate-proprietary!

Manson Yeung (JIRA) noreply at atlassian.com
Wed Nov 3 12:36:48 EDT 2010


    [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-4982?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=38996#action_38996 ] 

Manson Yeung commented on HHH-4982:
-----------------------------------

I also have such problem after upgrading from 3.3.2 to 3.5.6. In 3.3.2, the OneToOne relationship generate the left outer join. In 3.5.6 it becomes inner join. However in the document OneToOne annotation default is optional=true so I think hibernate should not use inner join for such case. Futher testing show that the use of @PrimaryKeyJoinColumn force the sql become inner join. My current workaround is to put the @PrimaryKeyJoinColumn to another side.

For querying TestFare:

Criteria lvCriteria = this.getSession().createCriteria(TestFare.class);
lvCriteria.add(Restrictions.in("id", fareIds));
lvCriteria.addOrder(Order.asc("lastUpdateTime"));
return lvCriteria.list();

My original settings (doesn't work in 3.5.6)
@Entity
@Table(name = "TB_FARE")
public class TestFare implements Serializable {

	@OneToOne(optional=true, fetch = FetchType.EAGER)
	@PrimaryKeyJoinColumn
	private TestRemark fareRemarks;
...
}

@Entity
@Table(name = "TB_FARE_REMARK")
public class TestRemark {
	@OneToOne(mappedBy="fareRemarks")
	private TestFare fare;
...
}

Generated SQL:
select this_.id as id81_1_, this_.is_special_private_fare as is2_81_1_, this_.last_migrated_date as last3_81_1_, this_.last_update_date as last4_81_1_, testremark2_.id as id82_0_, testremark2_.endorsement_remarks as endorsem2_82_0_, testremark2_.flight_modification_remarks as flight3_82_0_, testremark2_.mileage_accrual_remarks as mileage4_82_0_, testremark2_.misc_remarks as misc5_82_0_, testremark2_.refund_remarks as refund6_82_0_, testremark2_.remarks_variables as remarks7_82_0_, testremark2_.ticket_validity_remarks as ticket8_82_0_ from TZ_FLIGHT.TB_FARE this_ inner join TZ_FLIGHT.TB_FARE_REMARK testremark2_ on this_.id=testremark2_.id where this_.id in (?, ?) order by this_.last_update_date asc


Changed Settings (work in 3.5.6)

@Entity
@Table(name = "TB_FARE")
public class TestFare implements Serializable {
	@OneToOne(fetch = FetchType.EAGER, mappedBy="fare")
	private TestRemark fareRemarks;
}


@Entity
@Table(name = "TB_FARE_REMARK")
public class TestRemark {
	@OneToOne
	@PrimaryKeyJoinColumn
	private TestFare fare;
...
}


Generated SQL:
select this_.id as id81_1_, this_.is_special_private_fare as is2_81_1_, this_.last_migrated_date as last3_81_1_, this_.last_update_date as last4_81_1_, testremark2_.id as id82_0_, testremark2_.endorsement_remarks as endorsem2_82_0_, testremark2_.flight_modification_remarks as flight3_82_0_, testremark2_.mileage_accrual_remarks as mileage4_82_0_, testremark2_.misc_remarks as misc5_82_0_, testremark2_.refund_remarks as refund6_82_0_, testremark2_.remarks_variables as remarks7_82_0_, testremark2_.ticket_validity_remarks as ticket8_82_0_ from TZ_FLIGHT.TB_FARE this_ left outer join TZ_FLIGHT.TB_FARE_REMARK testremark2_ on this_.id=testremark2_.id where this_.id in (?, ?) order by this_.last_update_date asc

I am not sure if this is a bug or a designed behaviour on @PrimaryKeyJoinColumn
>From the javadoc, it stated that

It is used to join the primary table of an entity subclass in the JOINED mapping strategy to the primary table of its superclass; it is used within a SecondaryTable annotation to join a secondary table to a primary table; and it may be used in a OneToOne mapping in which the primary key of the referencing entity is used as a foreign key to the referenced entity. 

So it would be normal to use inner join if use in superclass-subclass relationship but cause troubles when using in OneToOne mapping. So I think it would be better to allow optional=true to override the inner join behaviour of @PrimaryKeyJoinColumn


> JPA OneToOne(optional=true) not supported, supported alternative NotFound() is hibernate-proprietary!
> -----------------------------------------------------------------------------------------------------
>
>                 Key: HHH-4982
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-4982
>             Project: Hibernate Core
>          Issue Type: Bug
>    Affects Versions: 3.5.5
>         Environment: Hibernate 3.3.1, annotations 3.4.0, MsSQL DB.
>            Reporter: Bruno Melloni
>
> Rejected issue ANN-725 covers this partially.  I might have misunderstood, but it seems the reason for rejecting is that @NotFound provides the same capability.
> All information I could find for JPA and other ORM tools indicates that this capability is supplied by @OneToOne(optional=true).  Hibernate seems to be the lone ORM tool that requires the use of a proprietary annotation: @NotFound.
> @NotFound works, but the problem is that it makes the application no longer be pure-JPA.  Not supporting pure-JPA outer joins would seem to be a major bug.

-- 
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