0 Replies Latest reply on Jan 12, 2006 10:43 AM by pi kap

    Fail to get the related entity ID for a Lazy ManyToOne Relat

    pi kap Newbie

      Hi all,

      I have two entities, City and Country, with accessType=AccessType.FIELD, having ManyToOne relationship:

      COUNTRY ENTITY BEAN

      package test.entity.bean;
      import javax.persistence.*;
      
      @Entity(access=AccessType.FIELD)
      @Table(name = "COUNTRY")
      public class Country implements java.io.Serializable
      {
       @Id
       @Column(name="ID")
      
       private int id;
       @Column(name="NAME")
       private String name;
      
       public int getId() { return this.id; }
       public void setId(int id) { this.id = id; }
      
       public String getName() { return this.name; }
       public void setName(String name) { this.name = name; }
      }
      


      CITY ENTITY BEAN
      package test.entity.bean;
      import javax.persistence.*;
      
      @Entity(access=AccessType.FIELD)
      @Table(name = "CITY")
      public class City implements java.io.Serializable
      {
      
       @Id
       @Column(name="ID")
       private int id;
      
       @Column(name="NAME")
       private String name;
      
       @ManyToOne( targetEntity=Country.class, fetch=FetchType.LAZY )
       @JoinColumn(name = "COUNTRY", nullable = true)
       private Country country;
      
       public City() {}
      
       public int getId() { return this.id; }
       public void setId(int id) { this.id = id; }
      
       public String getName() { return this.name; }
       public void setName(String name) { this.name = name; }
      
       public Country getCountry() { return this.country; }
       public void setCountry(Country country) { this.country = country; }
      
      }
      



      and a client that reads city.id, city.name and city.countryId only:

      REMOTE CLIENT
       ...
       City city = sb.getCity(cityId);
       System.out.println(city.getId());
       System.out.println(city.getName());
       System.out.println(city.getCountry().getId()); // <-- throws LazyInitializationException
       ...
      



      So, when I try to access city.getCountry().getId() from remote client, I get a
      org.hibernate.LazyInitializationException: could not initialize proxy - no Session Exception

      The strange thing is that, if I change the entity beans accessType to AccessType.PROPERTY, then this example seems to work fine!

      So my question is:
      - Is this the right way to get the ID from a related entity bean in a Lazy relationship?
      - If yes, why doesn't it work for AccessType=FIELD entity beans? Is it a bug?
      - If no, which is the right way to do it? (of course, loading the entire related entity is not an option, since my real classes are much more complicated. I just want to get the related entity's ID only)

      Note: I have some reasons to use AccessType=FIELD, so changing my EntityBeans to AccessType=PROPERTY is not an option either