Hi,
I am setting Eager fetching for OneToOne and ManyToOne relationships, and even though I specify @Fetch(FetchMode.JOIN), it does not work - I mean that even though it is fetching eagerly(at the time of HQL execution) it does not use JOIN, but is still performing additional selects(n+1 selects).
Please help!
To be more concrete the "main" entity is RfqRequest and it links to CoreCurrency (many-to-one) and RfqRequestOciParam(one-to-one).
The HQL is very simple:
Code:
entityManager.createQuery("from RfqRequest req").getResultList()
And it generates many sqls like this:
Code:
18:20:52,715 INFO [STDOUT] Hibernate: /* from RfqRequest req */ select ...{removed the selected columns}... from dbo.RFQ_Request rfqrequest0_
18:20:52,871 INFO [STDOUT] Hibernate: /* load com.ibx.ibxrequest.model.CoreCurrency */ select ...{removed the selected columns}... from dbo.CORE_Currency corecurren0_ where corecurren0_.CurrencyId=?
18:20:52,871 INFO [STDOUT] Hibernate: /* load com.ibx.ibxrequest.model.RfqRequestOciParam */ select ...{removed the selected columns}... from dbo.RFQ_RequestOCIParam rfqrequest0_ where rfqrequest0_.RequestId=?
Here is an excerpt from RfqRequest entity with the 2 links:
Code:
@Entity
@Table(name = "RFQ_Request", uniqueConstraints = {})
public class RfqRequest implements java.io.Serializable {
....
@ManyToOne(cascade = {}, fetch = FetchType.EAGER)
@Fetch(FetchMode.JOIN)
@LazyToOne(LazyToOneOption.FALSE)
@JoinColumn(name = "CurrencyId", unique = false, nullable = true, insertable = true, updatable = true)
public CoreCurrency getRfqCurIso() {
return this.rfqCurIso;
}
@PrimaryKeyJoinColumn
@OneToOne(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
@Fetch(FetchMode.JOIN)
@LazyToOne(LazyToOneOption.FALSE)
public RfqRequestOciParam getOciParams() {
return ociParams;
}
....
}
Here is an excerpt from the referred CoreCurrency entity:
Code:
@Entity
@Table(name = "CORE_Currency", uniqueConstraints = { @UniqueConstraint(columnNames = { "CurISO" }) })
public class CoreCurrency implements java.io.Serializable {
.....
@Id
@GeneratedValue
@Column(name = "CurrencyId", unique = true, nullable = false, insertable = true, updatable = true)
public Integer getCurrencyId() {
return this.currencyId;
}
....
}
And the RfqRequestOciParam entity
Code:
@Entity
@Table(name = "RFQ_RequestOCIParam", uniqueConstraints = {})
public class RfqRequestOciParam implements java.io.Serializable {
...
@Id
@GeneratedValue(generator = "myForeignGenerator")
@GenericGenerator( name = "myForeignGenerator", strategy = "foreign",
parameters = @Parameter(name = "property", value = "rfqRequest") )
@Column(name = "RequestId", unique = true, nullable = false, insertable = true, updatable = true)
public Integer getRequestId() {
return this.requestId;
}
@OneToOne(cascade = {}, fetch = FetchType.LAZY, mappedBy="ociParams" )
public RfqRequest getRfqRequest() {
return this.rfqRequest;
}
...
}
Hibernate version:
[Version] Hibernate Annotations 3.3.0.GA
[Environment] Hibernate 3.2.4.sp1
[Version] Hibernate EntityManager 3.3.1.GA
[Version] Hibernate Validator 3.0.0.GA
[Version] Hibernate Search 3.0.0.GA
Name and version of the database you are using:
[SettingsFactory] RDBMS: Microsoft SQL Server, version: 08.00.2039
[SettingsFactory] JDBC driver: jTDS Type 4 JDBC Driver for MS SQL Server and Sybase, version: 1.2
[Dialect] Using dialect: org.hibernate.dialect.SQLServerDialect