Hi,
I've ported a set of entities from "classic Hibernate" to JPA. Now it seems that every ManyToOne relation is fetched eagerly by default and not, like in "classic Hibernate", lazily.
This behaviour seems to be related to Annotations and/or Entitymanager. Using just Hibernate Core with the old entities and mappings, the relations are not fetched when reading a class from the database.
Lazy fetching still works, when I set the corresponding annotation ("@ManyToOne(fetch=FetchType.LAZY)"), but I wonder why this is not the default behaviour.
Is there a way to change the default back to 'lazy' or did I do something stupid?
The example contains two simple classes: 'Termine' has a 'Sendungen'. Reading one Termine, the related Sendungen is read as well.
Thanks,
Norbert
Hibernate version: Core 3.2; Annotations 3.3.0 GA; Entitymanager 3.3.0 GA
Mapping documents:
Code:
@Entity
@Table(name = "TERMINE")
public class Termine implements Serializable {
@Id
@Column(name = "ID", nullable = false)
private BigDecimal id;
@ManyToOne()
@JoinColumn(name = "SEND_ID", referencedColumnName = "ID")
private Sendungen sendung;
public Termine() {
}
public BigDecimal getId() {return id;}
public void setId(BigDecimal id) {this.id = id;}
public Sendungen getSendung() {return sendung;}
public void setSendung(Sendungen sendung) {this.sendung = sendung;}
}
@Entity
@Table(name = "SENDUNGEN")
public class Sendungen implements Serializable {
@Id
@Column(name = "ID", nullable = false)
private BigDecimal id;
public Sendungen() {
}
public BigDecimal getId() {return id;}
public void setId(BigDecimal id) {this.id = id;}
}
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="TestPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>test.entity.Sendungen</class>
<class>test.entity.Termine</class>
<properties>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.connection.username" value="rzp2000"/>
<property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
<property name="hibernate.connection.password" value="abc"/>
<property name="hibernate.connection.url" value="jdbc:oracle:thin:@ora.myself.de:1521:Dp42Entw"/>
</properties>
</persistence-unit>
</persistence>
Code for selecting one entity:Code:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("TestPU");
emf.createEntityManager().find(Termine.class, new BigDecimal(11024807L));
Name and version of the database you are using:Oracle 9i
The generated SQL (show_sql=true): Code:
select
termine0_.ID as ID1_1_,
termine0_.SEND_ID as SEND2_1_1_,
sendungen1_.ID as ID0_0_
from
TERMINE termine0_
left outer join
SENDUNGEN sendungen1_
on termine0_.SEND_ID=sendungen1_.ID
where
termine0_.ID=?