Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Cannot remove an entity with @ManyToOne association without fetching its contents
Cannot remove an entity with @ManyToOne association without fetching its contents [message #1769894] Fri, 04 August 2017 17:56 Go to next message
Stéphane Appercel is currently offline Stéphane AppercelFriend
Messages: 1
Registered: August 2017
Junior Member
I'm running EclipseLink 2.6.4 with static weaving on Java 8 SE, and I have the following entities:
@Entity
public class MyObject {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @ManyToOne(fetch = FetchType.LAZY)
    private final MySource source;

    public MyObject(MySource source) {
        this.source = source;
    }

    protected MyObject() {
        this.source = null;
    }

    public long id() {
        return this.id;
    }

    public MySource source() {
        return this.source;
    }

    @Override
    public boolean equals(Object obj) {
        boolean equals = this == obj;

        if (!equals && obj instanceof MyObject) {
            MyObject other = (MyObject) obj;
            equals = other.id() == id;
        }

        return equals;
    }

    @Override
    public int hashCode() {
        return (int) id % 16;
    }

}


@Entity
public class MySource {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    public MySource(String name) {
        this.name = name;
    }

    protected MySource() {
        this.name = "";
    }

    @Column
    private final String name;

    public long id() {
        return id;
    }

    public String name() {
        return this.name;
    }

    @Override
    public boolean equals(Object obj) {
        boolean equals = this == obj;

        if (!equals && obj instanceof MySource) {
            MySource other = (MySource) obj;
            equals = other.id() == id;
        }

        return equals;
    }

    @Override
    public int hashCode() {
        return (int) id % 16;
    }

}


When I execute the following transaction:
public void removeById(long id) {
    em.getTransaction().begin();
    MyObject myObjectRef = em.getReference(MyObject.class, id);
    em.remove(myObjectRef);
    em.getTransaction().commit();
    em.clear();
}


I can see the following queries:
SELECT ID, SOURCE_ID FROM MYOBJECT WHERE (ID = ?)
SELECT ID, NAME FROM MYSOURCE WHERE (ID = ?)
DELETE FROM MYOBJECT WHERE (ID = ?)

And more surprisingly, if I change the fetch type from LAZY to EAGER in the MyObject -> MySource association, then I obtain:
DELETE FROM MYOBJECT WHERE (ID = ?)

What's wrong with LAZY loading when a proxy is used to remove an entity?
Re: Cannot remove an entity with @ManyToOne association without fetching its contents [message #1770179 is a reply to message #1769894] Tue, 08 August 2017 21:09 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1275
Registered: July 2009
Senior Member
More information is posted here https://stackoverflow.com/questions/45511919/how-to-remove-an-entity-without-loading-its-contents?noredirect=1#comment78113471_45511919
Previous Topic:Can fetch size be made to propagate to subqueries made for BatchFetchType.JOIN
Next Topic:Getting NullPointerException in FullIdentityMap.getCacheKey() upon query.getResultList()
Goto Forum:
  


Current Time: Mon Dec 18 01:34:41 GMT 2017

Powered by FUDForum. Page generated in 0.01302 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software