0
@OneToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER, orphanRemoval = true)
@JoinColumn(name = "FacilityId", referencedColumnName = "Id")
@ForeignKey(name = "FK_Schedule_Facility")
@Cascade(value = {org.hibernate.annotations.CascadeType.ALL})
@OrderBy("fromDay asc")
private Set<Schedule> schedules = new HashSet<Schedule>();

I have this set in Entity Location.

In my application I construct a new set using AJAX. This has not given me any problems. I can change values in Schedules, even add more. But I CANNOT delete one. It stays in the database and in the set.

I use this method in my DAO: private SessionFactory sessionFactory;

public Facility saveOrUpdate(Facility facility){
    sessionFactory.getCurrentSession().saveOrUpdate(facility);
    return facility;
}

So I'm getting this in the log:

2013-08-06 17:44:43,444 [1219450701@qtp-169872652-2] DEBUG com.firstbankpr.os.common.data.dao.hibernate.FacilityHibernateDAO - Obtaining Session
2013-08-06 17:44:43,450 [1219450701@qtp-169872652-2] WARN  org.hibernate.util.JDBCExceptionReporter - SQL Error: 515, SQLState: 23000
2013-08-06 17:44:43,450 [1219450701@qtp-169872652-2] ERROR org.hibernate.util.JDBCExceptionReporter - Cannot insert the value NULL into column 'FacilityId', table 'OLS.dbo.Schedule'; column does not allow nulls. UPDATE fails.

After entering nullable = false in the joinColumn annotation, the error goes away. But it still does not correctly remove the Schedule I want to remove.

So what am I doing wrong? I've done a lot of googling. Found bugs in Hibernate 3.5 which do not apply to me since i'm using 3.6.7. Found that hashcode and equals have to be implemented correctly and found that JPA and Hibernate annotations often clash with each other. I've also found that bi-directional relations have problems with cascading, but again it does not apply to me. But aside from all those searches, I haven't quite found a solution for this one. Or perhaps I'm overlooking something.

Please help me out and thank you!

Community
  • 1
  • 1
Nimchip
  • 1,685
  • 7
  • 25
  • 50
  • it does not, it's unidirectional as it says in the title – Nimchip Aug 06 '13 at 21:57
  • 1
    There seems to be some suggestions out there that you need to call `merge()` and not `saveOrUpdate()`. That doesn't make sense to me but it's worth a shot. In addition, you might try removing the `@Cascade` annotation, it doesn't make sense to have that when you're using the cascade element in `@OneToMany`. Finally, are you sure you're checking for removal after the transaction has been committed? – Pace Aug 07 '13 at 03:27
  • try this: remove the "cascade" attribute inside the `@OneToMany` annotation and keep the `@Cascade` one. Besides, OrphanRemoval is about remove the children when the parent is deleted, this is not what you're trying to do. – MaVVamaldo Aug 07 '13 at 07:23
  • I tried using just one cascade annotation and nothing worked. I will try the merge I guess. – Nimchip Aug 07 '13 at 15:27
  • merge did work, thanks! It worked even with both annotations on the set definition. – Nimchip Aug 07 '13 at 16:09
  • @Pace please put the merge thing as an answer so I can mark it as one. And thank you! – Nimchip Aug 08 '13 at 19:10

1 Answers1

1

For some reason saveOrUpdate() doesn't properly process delete-orphans. You have to call merge() instead.

Pace
  • 41,875
  • 13
  • 113
  • 156