Jin Kwon
Jin Kwon

Reputation: 22027

JPA never fetcheable @OneToMany

I have an entity looks like this.

@Entity
@XmlRootElement
class Parent {

    // No getters nor setters for 'children'
    // Don't attack via reflection!

    @OneToMany(cascade = {CascadeType.REMOVE}, mappedBy = "parent") // lazy, huh?
    @XmlTransient
    private Collection<Child> chilren; // MILLIONS OF THEM, say.
}

I mapped children just for Criteria Query.

@StaticMetamodel(Parent.class)
class Parent_ {

    public static volatile CollectionAttribute<Parent, Child> children;
}

My question is, is it safe to map those children this way? Is it possible that chilren fetched from DB within a Parent for any case?

Upvotes: 0

Views: 132

Answers (1)

James
James

Reputation: 18389

If you never want to access the relationship, I would not map it. You should be able to define most queries using the ManyToOne back instead of the OneToMany.

If you are using EclipseLink, you can also define query keys for relationships that are only used in queries,

http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/Query_Keys

A LAZY OneToMany that you never access is probably safe, but you need to be very careful you don't do anything that will cause it to instantiate. The cascade remove is probably a bad idea, as it will cause it to be instantiated on remove.

Upvotes: 2

Related Questions