Hi,

i am also working in a highly detached environment because of auto- detachment when giving data to a different layer (questionable approach i know, but i dont need to inject persistenceManagers everywhere then). So i also have this "lazy loading" problem. I circumvent this kind of issue by using fetch-groups and submit them when querying... But in some way you dont get the original idea of lazy-loading where the idea is that you dont need to know what you need afterwards.

--
regards
Marc Logemann
[blog] http://www.logemann.org
[busn] http://www.logentis.de


Am 11.09.2007 um 09:54 schrieb Patrick Linskey:

Hi,

Lazy loading is not available for detached entities -- by definition,
they are no longer associated with the database.

You can, however, control whether OpenJPA returns null or throws an
exception when an unloaded field is accessed.

If you want to be able to lazily access a field after a transaction
completes, you might want to consider keeping the entity manager open
for the duration of your request.

-Patrick

On 9/11/07, Evgeny Shepelyuk <[EMAIL PROTECTED]> wrote:
        Hello !

Is there any way to access fields marked with lazy fetching for detached
entities.
Im having some code like

@Entity
@Table(schema = "public", name = "users")
public class UsersEntity {
        private Integer userid;
        private List<OrdersEntity> orders;

@OneToMany(cascade = CascadeType.PERSIST, mappedBy = "user", fetch =
FetchType.LAZY)
        public List<OrdersEntity> getOrders() {
                return orders;
        }

        public void setOrders(List<OrdersEntity> orders) {
                this.orders = orders;
        }

        @Id
        @Column(name = "userid", nullable = false)
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        public Integer getUserid() {
                return userid;
        }

        public void setUserid(Integer userid) {
                this.userid = userid;
        }
}


@Entity
@Table(schema = "public", name = "orders")
public class OrdersEntity {
        private Integer orderid;
        private UsersEntity user;

        @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumn(name = "userid", nullable = false)
        public UsersEntity getUser() {
                return user;
        }

        public void setUser(UsersEntity user) {
                this.user = user;
        }

        @Id
        @Column(name = "orderid", nullable = false, length = 10)
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        public Integer getOrderid() {
                return orderid;
        }

        public void setOrderid(Integer orderid) {
                this.orderid = orderid;
        }
}

And my program code

EntityManagerFactory emf = Persistence.createEntityManagerFactory ("pu1");
EntityManager em = emf.createEntityManager();
OrdersEntity order = em.find(OrdersEntity.class, .....);
em.close();
System.out.println(order.getUser());
emf.close();

And it shows null.
--
Best Regards
Evgeny K. Shepelyuk



--
Patrick Linskey
202 669 5907

Reply via email to