[hibernate-issues] [Hibernate-JIRA] Created: (HHH-5827) Cascade persist does not observe correct order and fails

Ralf Pöhlmann (JIRA) noreply at atlassian.com
Fri Jan 7 12:07:05 EST 2011


Cascade persist does not observe correct order and fails
--------------------------------------------------------

                 Key: HHH-5827
                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-5827
             Project: Hibernate Core
          Issue Type: Bug
          Components: core
    Affects Versions: 3.5.6
            Reporter: Ralf Pöhlmann
            Priority: Blocker


Persisting an entity fails sometimes with CascadeType.PERSIST option. This is the case when the order of persisting entities is important. Example:

Container
    |
    -- Element -----------------|
    |                      Association
    -- Opposite Element --------|
    
Persisting Container should persists in the given order:
1. Element 
2. Opposite Element 
3. Association (depends on element an opposite)

Testing this scenario with Hibernate sometimes works and sometimes fails. Same code works fine with EclipseLink as JPA provider.

In order to reproduce the problem create three entities: Container, Element, Association. 
Test Case:

final Container container = new Container();
final Element element = new Element();
container.addElement(element);

final Opposite opposite = new Element();
container.addElement(opposite);

final Association association = new Association(element, opposite);
em.persist(container);


@Entity
public class Container {
	...
	@OneToMany(mappedBy = "container", cascade = { CascadeType.PERSIST, CascadeType.REMOVE })
	private Collection<Element> elements = new HashSet<Element>();
	...
}


@Entity
public class Element {
	...
	@ManyToOne(optional = false)
	@JoinColumn(name = "container_id", referencedColumnName = "id")
	private Container container;

	@OneToMany(mappedBy = "element", cascade = { CascadeType.PERSIST, CascadeType.REMOVE })
	private Collection<Association> outgoing = new HashSet<Association>();

	@OneToMany(mappedBy = "opposite", cascade = { CascadeType.REMOVE })
	private Collection<Association> incoming = new HashSet<Association>();
	...
}

@Entity
public class Association {
	...
	public Association(final Element element, final Element opposite) {
		this.element = element;
		this.opposite = opposite;
		element.addOutgoingAssociation(this);
		opposite.addIncomingAssociation(this);
	}
	
	@ManyToOne(optional = false)
	@JoinColumn(name = "element_id", referencedColumnName = "id")
	private Element element;

	@ManyToOne(optional = false)
	@JoinColumn(name = "opposite_id", referencedColumnName = "id", unique = false)
	private Element opposite;
	...
}




-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

       



More information about the hibernate-issues mailing list