You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@geronimo.apache.org by Fredrik Jonson <fr...@myrealbox.com> on 2009/03/17 16:18:19 UTC

Unexpected behaviour from @OneToMany with FetchType.LAZY

Hello everyone,

I can't get my @OneToMany references with FetchType.LAZY to become
populated by openjpa in geronimo. Using FetchType.EAGER works as
expected. Using OpenJPA standalone (in unit tests) and
transaction-type=LOCAL works too. But when I deploy the code in
geronimo my LAZY-fetched OneToMany relations are not populated.

For example, using the code below when I call customer.getAccounts()
the Set is always null, but customer.getProducts() works as expected
and that Set is populated by openjpa.

@Entity
@Table(name="customer")
public class Customer implements Serializable
{
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;
    private String name;
    @OneToMany(mappedBy="customer", fetch=FetchType.LAZY,
        cascade=CascadeType.ALL)
    private Set<Account> accounts;
    @OneToMany(mappedBy="customer", fetch=FetchType.EAGER,
        cascade=CascadeType.ALL)
    private Set<Product> products;

    public Set<Account> getAccounts() {
        return this.accounts;
    }

    public Set<Product> getProducts() {
        return this.products;
    }

    // More regular getter/setters omitted...
}

The product and account entity have the same implementation apart
from their name:

@Entity
@Table(name="account")
public class Account implements Serializable
{
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;
    @ManyToOne
    @JoinColumn(name="customer_id", nullable = false)
    private Customer customer;

    // Getters/setters omitted...
}

I'm using geronimo 2.1.3. Though I get the same result with the
last daily build of 2.1.4-SNAPSHOT (20090310).

Is this expected behaviour from FetchType.LAZY or is it a matter
of configuration (and PEBCAK ;)? I can provide the application
descriptors, but for brewity I've left them out here.

-- 
Fredrik Jonson


Re: Unexpected behaviour from @OneToMany with FetchType.LAZY (SOLVED)

Posted by Fredrik Jonson <fr...@myrealbox.com>.
I wrote:

>  I can't get my @OneToMany references with FetchType.LAZY to become
>  populated by openjpa in geronimo. Using FetchType.EAGER works as
>  expected. Using OpenJPA standalone (in unit tests) and
>  transaction-type=LOCAL works too. But when I deploy the code in
>  geronimo my LAZY-fetched OneToMany relations are not populated.

False alarm. After poking around a bit more I noticed that the calling
code wasn't in a transaction. The customer entity was detached from the
entity manager long before the lazy-loaded resource had been populated.

BTW, while pondering I found a excellent introduction to JPA transactions:

 http://openejb.apache.org/3.0/jpa-concepts.html

Sorry for the noise.

-- 
Fredrik Jonson