Skip to main content


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: 1389
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:How do I insert into the bridging table for many to many tables ?
Next Topic:Version 2.6.4 Exceptin: SOAPMessage request format error - java.lang.NullPointerException
Goto Forum:
  


Current Time: Fri Apr 26 19:19:54 GMT 2024

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

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

Back to the top