问题描述
我有mySql查询,需要将它传递给hibernate查询。我已将简单的查询转换为休眠状态。但我很难转换更新查询。下面是MySql查询,
StringBuilder query = new StringBuilder();
query.append(UPDATE bus_transport.trip_calendar a,);
query.append(bus_transport.trip_resource_allocator b,);
query.append(bus_transport.vehicle_calendar c,);
query.append(bus_transport.driver_calendar d);
query.append(SET c.vehicle_status_code ='Available',);
query.append(a.route_code ='+ jsonObjInside.getString(rCode)+',);
query.append(a.trip_from_date_time ='+ finalStartDateTime +',);
query.append(a.trip_to_date_time ='+ finalEndDateTime +',);
query.append(b.emp_id ='+ jsonObjInside.getString(driverId)+',);
query.append(b.vehicle_id ='+ jsonObjInside.getString(vehId)+',);
query.append(c.vehicle_id ='+ jsonObjInside.getString(vehId)+',);
query.append(d.emp_id ='+ jsonObjInside.getString(driverId)+');
query.append(where a.trip_id ='+ jsonObjInside.getString(tripId)+');
query.append(and b.trip_id ='+ jsonObjInside.getString(tripId)+');
query.append(and c.trip_id ='+ jsonObjInside.getString(tripId)+');
query.append(and d.trip_id ='+ jsonObjInside.getString(tripId)+');
int var = stmt.executeUpdate(query);
请帮助我转换成Hibernate Update查询。
$ b $
将更新语句转换为HQL与将任何SQL语句转换为HQL非常相似。假设你已经正确地设置了你的对象映射,你只需要编写一个正确的HQL查询。您需要使用HQL()句法。你替换桌子&列名与您设置的对象名称。
这:
UPDATE bus_transport.trip_calendar a,bus_transport.trip_resource_allocator b,
bus_transport.vehicle_calendar c,bus_transport.driver_calendar d
$
$ b
更新TripCalendar a,TripResourceAllocator b,
VehicleCalendar c ,DriverCalendar d
您以类似的方式引用列
这:
SET c.vehicle_status_code ='可用'
可能会变成这样:
SET c .vehicleStatusCode ='Available'
更多关于Hiberate DML的具体内容,请查看此链接:
I am having mySql query and need to transfer it in to hibernate query. I have transferred simple queries into hibernate. But its difficult for me to convert "UPDATE" query. Below is the MySql query,
StringBuilder query = new StringBuilder(); query.append("UPDATE bus_transport.trip_calendar a ,"); query.append("bus_transport.trip_resource_allocator b,"); query.append("bus_transport.vehicle_calendar c ,"); query.append("bus_transport.driver_calendar d "); query.append("SET c.vehicle_status_code='Available',"); query.append("a.route_code='" + jsonObjInside.getString("rCode") + "',"); query.append("a.trip_from_date_time='" + finalStartDateTime + "',"); query.append("a.trip_to_date_time='" + finalEndDateTime + "',"); query.append("b.emp_id='" + jsonObjInside.getString("driverId") + "' ,"); query.append("b.vehicle_id='" + jsonObjInside.getString("vehId") + "',"); query.append("c.vehicle_id='" + jsonObjInside.getString("vehId")+ "' ,"); query.append("d.emp_id='" + jsonObjInside.getString("driverId") + "'"); query.append("where a.trip_id='" + jsonObjInside.getString("tripId")+ "'"); query.append(" and b.trip_id='" + jsonObjInside.getString("tripId") + "'"); query.append("and c.trip_id='" + jsonObjInside.getString("tripId")+ "'"); query.append("and d.trip_id='" + jsonObjInside.getString("tripId")+ "' "); int var = stmt.executeUpdate(query);
Please help me to convert into Hibernate Update query.
Thanks in advance.
解决方案Converting an update statement to HQL would be very similar to converting any SQL statement to HQL. Assuming you have your object mapping set-up correctly already you'd just need to write a correct HQL QUERY. You'll need to use the HQL(HQL Docs) syntax. You replace the table & column names with the object names you have set up.
This:
UPDATE bus_transport.trip_calendar a , bus_transport.trip_resource_allocator b, bus_transport.vehicle_calendar c , bus_transport.driver_calendar d "
could be come something like this:
UPDATE TripCalendar a , TripResourceAllocator b, VehicleCalendar c , DriverCalendar d "
You reference columns in a similar fashion
This:
SET c.vehicle_status_code='Available'
Could become something like this:
SET c.vehicleStatusCode = 'Available'
For more on the Hiberate DML specific stuff check out this link: HQL DML Docs
这篇关于如何在休眠中更新多个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!