Hello,

I experienced some Issues using ToString and AutoClone on Entities (Spring JPA).

Both can lead to endless loops / a stackoverflow, when classes reference each other.

While it's fine for AutoClone, I think the generated toString-Method should recognize it was already called once and return a dummy value or just the object reference.


Anyway, I got bigger issues using AutoClone:

1. I use an abstract class to add auditing columns to all my tables. All my Entities inherit from that class (AbstractEvent in this example). Since these fields should be empty/null on cloning, I'd like to omit the AutoClone annotation on that class. But this leads to an error: No signature of method: Event.cloneOrCopyMembers() is applicable for argument types: (Event) values: [Event(null, null, [], Event@54d9d12d)].

2. SubEvent objects within Event.subEvents were not cloned.

import groovy.transform.AutoClone
import groovy.transform.Canonical
import groovy.transform.ToString
import static groovy.transform.AutoCloneStyle.SIMPLE

@AutoClone(style=SIMPLE) //1. error when you remove this
abstract class AbstractEvent {

    Date created
    String createdBy

    Date modified
    String modifiedBy
}

@AutoClone(style=SIMPLE)
@ToString(includeSuper = true)
class Event extends AbstractEvent {
    Long id
    String someText

    ArrayList<SubEvent> subEvents = new ArrayList();
}

@AutoClone(style=SIMPLE)
@ToString(includeSuper = true, excludes = ['event'])
class SubEvent extends AbstractEvent {
    Long id
    String someText

    Event event;
}

public static void main(String[] args){
    Event event = new Event(
        id: 1,
        someText: "Event 1",
        created: new Date(),
        createdBy: "me");

    SubEvent subEvent1 = new SubEvent(
        id: 1,
        someText: "SubEvent 1",
        event: event);

    event.subEvents += subEvent1


    Event clonedEvent = event.clone()
    assert !event.is(clonedEvent)
    assert !event.subEvents.is(clonedEvent.subEvents)
    assert !event.subEvents.first().is(clonedEvent.subEvents.first()) // 2. fails
}


Hope, you can help me here.



Kind Regards

Nikolai

Reply via email to