Results 1 to 3 of 3
  1. #1
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    312
    Rep Power
    11

    Default OpenJPA - One to Many within a One to Many, merge problems

    Hello all,

    I'm facing a problem when trying to use OpenJPA to update an Entity that contains a One to Many relationship to another Entity, that has a One to Many relationship to another. Here's a quick example of what I'm talking about:

    Java Code:
    	@Entity
    	@Table(name = "school")
    	public class School {
    		
    		@Column(name = "id")
    		protected Long id;
    		
    		@Column(name = "name")
    		protected String name;
    		
    		@OneToMany(mappedBy = "school", orphanRemoval = true, cascade = CascadeType.ALL)
    		protected Collection<ClassRoom> classRooms;
    	}
    	
    	@Entity
    	@Table(name = "classroom")
    	public class ClassRoom {
    		
    		@Column(name = "id")
    		protected Long id;
    		
    		@Column(name = "room_number")
    		protected String roomNumber;
    		
    		@ManyToOne
    		@JoinColumn(name = "school_id")
    		protected School school;
    		
    		@OneToMany(mappedBy = "classRoom", orphanRemoval = true, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    		protected Collection<Desk> desks;
    		
    	}
    	
    	@Entity
    	@Table(name = "desk")
    	public class Desk {
    		
    		@Column(name = "id")
    		protected Long id;
    		
    		@ManyToOne
    		@JoinColumn(name = "classroom_id")
    		protected ClassRoom classRoom;
    		
    	}
    In the SchoolService class, I have the following update method:

    Java Code:
    		@Transactional
    		public void update(School school) {
    			em.merge(school);
    		}

    I'm trying to remove a Class Room from the School. I remove it from the classRooms collection and call update. I'm noticing if the Class Room has no desks, there are no issues. But if the Class Room has desks, it throws a constraint error as it seems to try to delete the Class Room first, then the Desks. (There is a foreign key constraint for the classroom_id column)

    Am I going about this the wrong way? Is there some setting I'm missing to get it to delete the interior "Desk" instances first before deleting the Class Room instance that was removed?

    Any help would be appreciated. If you need any more info, please just let me know.
    Thanks,
    Last edited by StormyWaters; 08-16-2016 at 08:06 PM. Reason: Added missing fetch

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    312
    Rep Power
    11

    Default Re: OpenJPA - One to Many within a One to Many, merge problems

    Quick update on this, I switched over to eclipselink and it seems to handle this case better, I'm thinking of switching over and using that but need to make sure everything else will work as expected and get load time weaving working with it.

  3. #3
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    312
    Rep Power
    11

    Default Re: OpenJPA - One to Many within a One to Many, merge problems

    Cross posting this to StackOverflow to see if I can get any responses there: java - OpenJPA - Nested OneToMany relationships merge issue - Stack Overflow

Similar Threads

  1. Merge sort
    By Aivy in forum New To Java
    Replies: 3
    Last Post: 12-14-2011, 11:29 AM
  2. Merge Sorting
    By blutao in forum New To Java
    Replies: 0
    Last Post: 03-23-2011, 06:54 AM
  3. OpenJPA + DB2 + WebSphere
    By mrblack in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 02-27-2011, 02:45 PM
  4. Merge XML using XSL
    By palanikumark in forum XML
    Replies: 1
    Last Post: 08-16-2008, 12:14 AM
  5. Replies: 1
    Last Post: 03-27-2008, 05:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •