Search code examples
javajpaentityone-to-manyopenjpa

JPA - Entity with One-to-many - relationship returns no data


I am using the ReverseMappingTool to create JPA-Sources for an existing database-scheme.

It works fine, with one exception: After the creation, when calling the get-Methods data from One-to-many relationships are not loaded.

The JPA files look like that:

@Entity
@Table(schema="myScheme", name="ORDER")
@IdClass(org.company.OrderId.class)
public class Order {
    @OneToMany(targetEntity=org.company.Orderposition.class, mappedBy="order", cascade=CascadeType.MERGE)
    private Set orderpositions = new HashSet();

public Set getOrderpositions() {
    return bestellpositions;
}

public void setOrderpositions(Set orderpositions) {
    this.orderpositions = orderpositions;
}

public Set getDependantPositions() {
    if (getOrderpositions() != null) {
        return getOrderpositions();
    }
}

and...

@Entity
@Table(schema="myScheme", name="ORDERPOSITION")
@IdClass(org.company.Orderposition.Id.class)
public class Orderposition {
    @ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.MERGE)
    @JoinColumn(name="intid_strbestellung", nullable=false)
    private Order order;

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

The method "getDependantPositions()" is just an example of a method I have placed inside the JPA class. It is necessary to do that, so lets take it as given. The point is, that calling getOrderpositions() does not return any data though the database contains lot of entries for this object. The mapping tool has not added any FetchType for @OneToMany-Relationships... so I guess its LAZY and calling getOrderpositions() should return all relevant values.

Can you help me?

Thanks!

P.S.: Thats the way I calling thr ReverseMappingTool:

Options opts = new Options();
        // Output directory - All generated code and metadata will be written to the directory at this path
        opts.setProperty("directory", PATH_SOURCEGENERATION);
        // True to generate JPA annotations in generated java classes. 
        opts.setProperty("annotations", true);
        // disable orm.xml generation
        opts.setProperty("metadata", "none");
        // The full class name of a org.apache.openjpa.jdbc.meta.ReverseCustomizer customization plugin
        opts.setProperty("customizerClass", JPAPropertiesReverseCustomizer.name);
        // Create built-in application identity classes if possible
        opts.setProperty("useBuiltinIdentityClass", false);
        // Avoid creating classes for many-many join tables
        opts.setProperty("primaryKeyOnJoin", true);
        // Prevent the creation of inverse 1-many/1-1 relations for every many-1/1-1 relation detected
        opts.setProperty("inverseRelations", true);

        opts.setProperty("innerIdentityClasses", true);

Solution

  • You should configure your logs so that you see what queries are sent to DB. Then you will be able to check: 1. what query goes to DB when Order is loaded and 2. what (and if any) query goes to DB when you get contents of Set returned by getOrderpositions(). Note that you have to access the content of the Set, just calling the getter will not invoke any DB query in lazy-loaded association.