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



View the original post : 
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3917149#3917149

Reply to the post : 
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3917149


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
JBoss-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to