openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Patrick Linskey" <plins...@gmail.com>
Subject Re: Lazy load for detached entities
Date Tue, 11 Sep 2007 07:54:18 GMT
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 <eshepelyuk@gmail.com> 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

Mime
View raw message