javajpatoplink-essentials

Toplink trying to persist null object


I have an object "Instance" with another object "Course" inside. When trying to persist a new Instance object, I get the following error if Course is null:

java.lang.IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST: null

Here is my annotation for course:

@OneToOne(cascade = CascadeType.REFRESH,optional=true)
    public Course getCourse() {
        return course;
    }

I don't see why it's trying to persist something that's null. If it's null, it should just leave that field null in the database right?


Solution

  • I don't see why it's trying to persist something that's null. If it's null, it should just leave that field null in the database right?

    By default, TopLink JPA assumes that all the fields of an entity are persistent. If you want to specify a field or property of an entity that is not persistent, you need to mark it with @Transient (but this is not what you want here).

    So, to me, this error is the result of not having specified the proper Cascade type for the OneToOne end of the relation in the Instance Entity. I can't say why you're not using ALL but you need at least to add PERSIST.