-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Default FetchType for ManyToOne relation?
PostPosted: Wed Mar 28, 2007 10:55 am 
Newbie

Joined: Wed Mar 28, 2007 7:57 am
Posts: 3
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=?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 28, 2007 11:40 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
That's because some implementation in the EJB 3 Expert Group were weak in this area

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.