简体   繁体   English

休眠HQL删除查询使单列无效

[英]hibernate HQL delete query nullifies single column

I am working on spring hibernate application and trying to delete from a table using non-id many-to-one relationship based column. 我正在研究Spring Hibernate应用程序,并尝试使用基于非ID多对一关系的列从表中删除。 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. 我正在尝试使用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. 在控制台中,我正在获取正确的删除查询,但是在检查数据库时发现行仍然存在,但是现在假日表中的DAY_ID列为空。 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?? 我的主要问题是为什么DAY_ID列更改为空值?

I'm not sure that this is your problem, but in your query you say "DELETE FROM Holidays ...", but your Class name is Holiday . 我不确定这是否是您的问题,但是在查询中您说“从假期中删除...”,但您的班级名称是Holiday In HQL you should be using Class names rather than table names or anything else. 在HQL中,您应该使用类名而不是表名或其他任何名称。 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. 要对其进行分解,请使用类名称“ Holiday”,为其分配别名“ h”,然后引用Holiday对象的day字段(“ h.day”),并将其与实际的Day对象进行比较。

What is your ONDELETE foreign key constrain? 您的ONDELETE外键约束是什么? Might it that other part of your application inserting a row? 可以在应用程序的其他部分插入行吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM