Spring-Data JPA: save new entity referencing existing one

I had a similar issue where I was trying to save an new entity object with an already saved entity object inside.

What I did was implemented Persistable< T > and implemented isNew() accordingly.

public class MyEntity implements Persistable<Long> {

    public boolean isNew() {
        return null == getId() &&
            subEntity.getId() == null;
    }

Or you could use AbstractPersistable and override the isNew there ofcourse.

I don’t know if this will be considered a good way of handling this issue but it worked out quite good for me and besides feels very natural to do.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)