0

I am working on spring hibernate application and trying to delete from a table using non-id many-to-one relationship based column. Entity classes are:

@Entity  
public class Day {  
@id(name = "DAY_ID")  
dayId;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "DAY_ID")
List<Holiday> holidayList;  
...  
}

@Entity  
public class Holiday {  
@id(name="HOLIDAY_ID")
holidayId;  
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "DAY_ID")
Day day;
...
}

I am trying to delete a row from holiday table using hql.

String query = "DELETE FROM Holiday WHERE day.dayId = " + dayObject.getdayId();
Query holidayDeleteQuery = getSession().createQuery(query);
holidayDeleteQuery.executeUpdate();

In the console i am getting proper delete query but on checking DB found out that the row is still there but now the DAY_ID column in holiday table is null. I am not able to figure out why is this happening?
EDIT: help!!! My main problem is why DAY_ID column is changing to null value??

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
parbi
  • 533
  • 1
  • 10
  • 19
  • When you say you're getting a proper delete query, can you post the SQL? I sincerely doubt the delete query you get is right because that would mean your database is failing to do a simple delete correctly. – Vala Oct 12 '11 at 10:04
  • hi the generate query is Hibernate: delete from HOLIDAYS where DAY_ID=1 – parbi Oct 12 '11 at 10:05
  • That's what Hibernate prints out in the log when you have show_sql = true or are you just assuming that if the String query is that it's fine? – Vala Oct 12 '11 at 10:09
  • ya this is what print outs in console when the query runs. – parbi Oct 12 '11 at 10:15

2 Answers2

0

I'm not sure that this is your problem, but in your query you say "DELETE FROM Holidays ...", but your Class name is Holiday. In HQL you should be using Class names rather than table names or anything else. Is this typo in your code or just on here?

Actually after looking further there are a few more problems. This is how I'd write it:

String query = "DELETE FROM Holiday h WHERE h.day = :day";
Query holidayDeleteQuery = getSession().createQuery(query);
query.setParameter("day", dayObject);
holidayDeleteQuery.executeUpdate();

To break it down - use the Class name "Holiday", assign it an alias "h" then reference the day field of the Holiday object ("h.day") and compare it to the actual Day object you have.

Vala
  • 5,628
  • 1
  • 29
  • 55
  • Ya i am using classname. Its a typo. – parbi Oct 12 '11 at 10:16
  • Tried passing the object, still getting same result, only day_id changes to null. I was wondering is this happening because of spring? – parbi Oct 12 '11 at 10:31
  • The above should work fine in Spring, you definitely used an alias and referenced it as above? I'd also try copy/pasting the SQL printed by hibernate and use it against your database directly to see the result there. – Vala Oct 12 '11 at 10:38
  • Ya i tried alias as you have mentioned. Query generated by hibernate is working fine in database. I don't know what's happening here and moreover why is the column value changing to null? – parbi Oct 12 '11 at 10:44
  • Hm. I'm running out of ideas then. I don't know what database you use, but it may be possible to get a history of queries called (there usually is). I'd check that history, run the code, then check it again to see it's receiving what you're seeing in the log. – Vala Oct 12 '11 at 11:07
0

What is your ONDELETE foreign key constrain? Might it that other part of your application inserting a row?

Vijay Thakre
  • 81
  • 1
  • 8