Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update multiple tables in hibernate?

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.

like image 396
Sandy Avatar asked Jan 06 '14 04:01

Sandy


People also ask

Can you update multiple tables at once?

We cannot update multiple tables together using SQL Server Update Join.

How do you update data from multiple tables?

To UPDATE a table by joining multiple tables in SQL, let's create the two tables 'order' and 'order_detail. ' We can update the data of a table using conditions of other joined tables. It is possible to join two or more tables in an UPDATE query.

Can we update multiple tables using update command?

It's not possible to update multiple tables in one statement, however, you can use the transaction to make sure that two UPDATE statements must be treated atomically. You can also batch them to avoid a round trip like this.

How do you map an entity to multiple tables in hibernate?

Yes, you can map an entity to 2 database tables in 2 simple steps: You need to annotate your entity with JPA's @Table and @SecondaryTable annotations and provide the names of the first and second table as the value of the name parameters.


1 Answers

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

like image 156
Durandal Avatar answered Sep 30 '22 17:09

Durandal