I am trying to call custom insert Query using JPA in Spring Boot.
#Repository
public interface eosRepo extends JpaRepository<EosthirdpartylabelsRequest, Long> {
#Modifying
#Query(
value =
"INSERT INTO public.documents_zpl(\\r\\n\" +\r\n" +
" \" loc_id, order_nbr, sub_order_nbr, carton_id, doc_payload, dt_ent, tm_ent, program_ent, dt_chg, tm_chg, program_chg)\\r\\n\"\r\n" +
" + \" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?",
nativeQuery = true)
void insertData(Integer locID, Integer OrderNum,Integer SubOrderNum,Integer CartonId,String CarrierLabel);
}
How can I call this query in Controller.
First use:
#Autowired
eosRepo eosRepo;
in your controller and then call insertData.
In controller firstly do this
#Autowired
private eosRepo object_name;
and then you can call this method
object_name.insertData
And put ')' in your query which you have forgot.
" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?", you are forgot to give ')' and also call repo from controller use
#Autowired
private eosRepo theEosRepo;
$list = [];
foreach($RoleDetails["Data"]["Permissions"] as $Permission) {
$MyModel = new UserRolePermissionModel();
$MyModel->UserID = $User->UserID;
$MyModel->RolePermissionID = $Permission->RolePermissionID;
$MyModel->IsActive = $Permission->IsActive;
array_push($list, $MyModel);
}
\DB::table('tbluserrolepermission')->insert($list);
Below are the error details
QueryException {#293 ▼
#sql: "insert into `tbluserrolepermission` (`0`, `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `10`, `11`, `12`, `13`, `14`, `15`, `16`, `17`) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
#bindings: array:18 [▶]
#message: "SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: insert into `tbluserrolepermission` (`0`, `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `10`, `11`, `12`, `13`, `14`, `15`, `16`, `17`) values ({"UserID":21,"RolePermissionID":19,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":20,"IsActive":0,"IsProtectionAvailable":0,"IsProtectionReadOnly":1}, {"UserID":21,"RolePermissionID":21,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":22,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":23,"IsActive":0,"IsProtectionAvailable":0,"IsProtectionReadOnly":1}, {"UserID":21,"RolePermissionID":24,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":25,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":26,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":27,"IsActive":0,"IsProtectionAvailable":0,"IsProtectionReadOnly":1}, {"UserID":21,"RolePermissionID":28,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":29,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":30,"IsActive":0,"IsProtectionAvailable":0,"IsProtectionReadOnly":1}, {"UserID":21,"RolePermissionID":31,"IsActive":0,"IsProtectionAvailable":0,"IsProtectionReadOnly":1}, {"UserID":21,"RolePermissionID":32,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":33,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":34,"IsActive":1,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":35,"IsActive":1,"IsProtectionAvailable":0,"IsProtectionReadOnly":1}, {"UserID":21,"RolePermissionID":36,"IsActive":1,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}))"
#code: "42S22"
#file: "C:\xampp\htdocs\AS4\vendor\laravel\framework\src\Illuminate\Database\Connection.php"
#line: 761
-previous: PDOException {#356 ▶}
+errorInfo: array:3 [▶]
+"previous": PDOException {#356 ▶}
-trace: {▶}
}
It is not working, because you are not providing an array of arrays with only the values matching the column names to populate the database with.
Example
Lets say you want to populate an array of users and save them to the database. The following example would do that.
DB::table('users')->insert([
['email' => 'taylor#example.com', 'votes' => 0],
['email' => 'dayle#example.com', 'votes' => 0]
]);
Note that email and votes in the above example are 2 columns in the users table. Note also that only an array of other arrays are passed to the insert method. What you are trying to insert is actually an array of eloquent model objects.
Source: https://laravel.com/docs/5.3/queries#inserts
I fixed it like below.
foreach($RoleDetails["Data"]["RolePermissions"] as $RolePermission) {
$data = [
'UserID' => $User->UserID,
'RolePermissionID' => $RolePermission->RolePermissionID,
'IsActive' => $RolePermission->IsActive,
'IsProtectionAvailable' => $RolePermission->IsProtectionAvailable,
'IsProtectionReadOnly' => $RolePermission->IsProtectionReadOnly
];
array_push($list,$data);
}
\DB::table('tbluserrolepermission')->insert($list);
or it could be like this.
UserRolePermissionModel::insert($list);
Can you try this?
UserRolePermissionModel::insert($list->toArray());
OR
DB::table('table')->insert($list->toArray());
I'm having some trouble figuring out the Active Record syntax for the following Postgres query:
SELECT *
FROM tracks
JOIN displays on displays.track_id = tracks.id
JOIN users on displays.user_id = users.id
WHERE user_id = 1
AND displays.display is true
AND tracks.likes_count > 200
The above query above works, and now I'm trying to convert it to activerecord's syntax but getting the error:
PG::SyntaxError: ERROR: syntax error at or near "'t'" LINE 1:
...ck_id" = "tracks"."id" WHERE (displays.display is 't', track...
Track.joins(:users, :displays).where('displays.display is ?, tracks.likes_count > ?, users.id = ?', true, 200, 1)
It seems like I'm doing something wrong with the displays.display is True statement, but I have tried a few variations to no avail.
I'm using Activerecord-4.2.0.
I would appreciate some insight.
By default ActiveRecord doesn't know how to join tracks and users, so indicate that you want to join through the displays:
Track.joins(:displays => :users).where('displays.display is true, tracks.likes_count > ?, users.id = ?', 200, 1)
Hope I remember syntax well :)
I think the problem is in the joins, you can try this:
Track.joins(displays: :users).where('displays.display = ?, tracks.likes_count > ?, users.id = ?', true, 200, 1)
You can make a join with a third table using the syntax:
Table1.joins(table2: :table3)
Result:
SELECT "table1".*
FROM "table1"
INNER JOIN "table2" ON "table2"."table_id" = "table1"."table1_id"
INNER JOIN "table3" ON "table3"."table2_id" = "table2"."table2_id"
able3" ON "table3"."table2_id" = "table2"."table2_id"
You can read more for ActiveRecord's joins method here: http://guides.rubyonrails.org/active_record_querying.html#joining-tables
Looks like I was actually just missing the ANDs....derp:
Track.joins(:users, :displays).where('displays.display = ? AND tracks.likes_count > ? AND users.id = ? ', true, 200, 1)
UPDATE Also as #Andrius pointed out Track.joins(:users, :displays) and Track.joins(:displays => :users) are two different joins and I want to use the former.
I want to filter my selection of columns in my channels hasmany relation as well filtering it using where method but to no avail. Any idea?
$data['users'] = Users::with(array('Channels' => function($q){
$q->where('is_default','=', 1)->select(array('channel_name', 'channel_id'));
}))->get(array('id'));
But this works perfectly fine.
$data['users'] = Users::with(array('Channels' => function($q){
$q->where('is_default','=', 1)
}))->get(array('id'));
I tried putting the select method first before the where method still does not work
EDIT:
These are SQL queries executed which is really what I wanted however it does not fill up the relations property.
select created_at, id, name, referrer from users where status = ? limit 10 offset 0
select channel_name, channel_id from channels where channels.user_id in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) and is_default = ?
You may try this
$data['users'] = Users::with(array('Channels' => function($q){
$q->select('id, channel_name', 'user_id')->where('is_default','=', 1);
}))->get();
If you want to select columns in get() then make sure you select the associated key for channels table, for example, assuming that user_id is the foreign key in chennels table so you can do it using this:
$data['users'] = Users::with(array('Channels' => function($q){
$q->select('id, channel_name', 'user_id')->where('is_default','=', 1);
}))->get(array('id'));
If you don't select the primary key in the main query and foreign key in the sub query then with will not work. In both querys/select the related keys should be available.
I'm creating a query string as follows ...
gSql = "{ CALL MYSP( ?, ?, ?, ?, ?, ?, {resultset 10, RECORD_OUTPUT} ) }"
the {resultset 1000, RECORD_OUTPUT} is copied it from Microsoft's MSDN
The error displayed after I compile is:
identifier \'RECORD_OUTPUT\' must be declared
What should the {resultset 1000, RECORD_OUTPUT} actually be ?
At this position, The Stored Procedure will return refcursor back to VB6.