I know this question has been asked numerous times but I couldn't find a suiting answer for me.
I've got two entities in a Spring Roo-application which are in a Many-To-Many-relationship, Release and Component.
First I fetch an instance of an existing Release via
selectedRelease = Release.findReleasesByReleaseNumberEquals(version).getSingleResult();
The method above is a Roo-generated finder which looks like this:
public static TypedQuery<Release> Release.findReleasesByReleaseNummerEquals(String releaseNumber) {
EntityManager em = Release.entityManager();
TypedQuery<Release> q = em.createQuery("SELECT o FROM Release AS o WHERE LOWER(o.releaseNumber) LIKE LOWER(:releaseNummer)", Release.class);
q.setParameter("releaseNumber", releaseNumber);
return q;
}
Then I create a new instance of Component and try to assign it to the selected Release
Component component = new Component();
Set<Release> releases = new HashSet<Release>();
releases.add(selectedRelease);
component.setReleases(releases);
component.persist();
Upon trying to execute persist() I get the exception:
TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.Release;
Does anyone have advice regarding this problem?
The mappings look like this:
Release.java:
@ManyToMany(cascade = CascadeType.PERSIST, mappedBy = "releases")
private Set<Component> components = new HashSet<Component>();
Component.java
@ManyToMany(cascade=CascadeType.PERSIST)
private Set<Release> releases = new HashSet<Release>();