How to call custom insert Query using JPA in Spring Boot - spring

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;

Related

FMDB, Swift 3, executeUpdate - compiler build failed

I'm using FMDB with Swift 3.
Everything worked fine on Swift 2 but after making the Swift upgrade I'm getting:
"Command failed due to signal: Killed: 9"
After investigating the cause of it I've found that doing "executeUpdate" with about 24 arguments in the ArgumentsArray cause the compiler to be very slow and finally return compilation error.
When decreasing the number of arguments in the array to 20, the compiler build is still slow but succeed to finish successfully.
Any idea why/help will be welcome... !
Here is my code:
(Build succeed but uncommenting the 4 lines below will make the compilation build failed. Any other 4 lines will have same result of course)
func insertLocalization(_ localization: Localization) -> Bool {
print ("Insert Localization: \(localization.localization_object_id!)#\(localization.spot_object_id!)#\(localization.language_code!)")
sharedInstance.database!.open()
let isInserted = sharedInstance.database!.executeUpdate(
"INSERT INTO localizations (" +
"localization_object_id, " +
"spot_object_id, " +
"language_code, " +
"current_location_enabled, " +
"spot_title, " +
"spot_desc, " +
"local_assistant_phone, " +
"orientation_360_enabled, " +
"direction_n_title, " +
"direction_n_desc, " +
"direction_ne_title, " +
"direction_ne_desc, " +
"direction_e_title, " +
"direction_e_desc," +
"direction_se_title, " +
"direction_se_desc, " +
"direction_s_title, " +
"direction_s_desc, " +
"direction_sw_title, " +
"direction_sw_desc, " +
"direction_w_title, " +
"direction_w_desc, " +
"direction_nw_title, " +
"direction_nw_desc) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
withArgumentsIn: [
// localization.localization_object_id!,
// localization.spot_object_id!,
// localization.language_code!,
// localization.current_location_enabled!,
localization.spot_title!,
localization.spot_desc!,
localization.local_assistant_phone!,
localization.orientation_360_enabled!,
localization.direction_n_title!,
localization.direction_n_desc!,
localization.direction_ne_title!,
localization.direction_ne_desc!,
localization.direction_e_title!,
localization.direction_e_desc!,
localization.direction_se_title!,
localization.direction_se_desc!,
localization.direction_s_title!,
localization.direction_s_desc!,
localization.direction_sw_title!,
localization.direction_sw_desc!,
localization.direction_w_title!,
localization.direction_w_desc!,
localization.direction_nw_title!,
localization.direction_nw_desc!
])
sharedInstance.database!.close()
return isInserted
}
Thanks!
This code compiles without incident for me in Xcode 8.0 (8A218a). But if this isn't working for you, I'd suggest splitting the line up, for example
let values = [localization.localization_object_id!, ..., localization.direction_nw_desc!]
let isInserted = sharedInstance.database!.executeUpdate(
"INSERT INTO localizations (...) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
withArgumentsIn: values)

Two wherehas in a Laravel Eloquent query

I have the following query, simplified below.
$property = Property::whereHas('features', function ($q1) use ($features) {
$q1->where(function ($sub) use ($features) {
$sub->whereIn('features.FeatureID', $features);
});
$q1->groupBy('PropertyID');
$q1->having('count(*)', '=', count($features));
})
->whereHas('Week', function ($q) use ($nights) {
$q->where(function ($sub) use ($nights) {
$sub->whereIn('priceAvailDate', $nights);
});
$q->groupBy('PropertyID');
$q->having('count(*)', '=', count($nights));
})
->get();
If I take one whereHas out, I get the expected collection returned. If I take the other out I get the expected result.It is when they are together I have nothing returned. I assume it is down to having two count(*). I know I have data where both whereHas is true.
SQL output is
select * from `tblproperties` where (select count(*) from `features` inner
join `propertyfeatures` on `features`.`featureid` =
`propertyfeatures`.`FeatureID` where `propertyfeatures`.`PropertyID` =
`tblproperties`.`PropertyID` and (`features`.`FeatureID` in (?)) group by
`PropertyID` having `count(*)` = ?) >= 1 and (select count(*) from
`tblpriceavail` where `tblpriceavail`.`propertyID` = `tblproperties`.`PropertyID`
and (`priceAvailDate` in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)) group by
`PropertyID` having `count(*)` = ?) >= 1
Fixed, although not 100% sure why. Using havingRaw instead of having worked.
i.e. changing the $q->having('count(*)', '=', count($nights));
to $q->havingRaw('count(*) = ' . count($nights));

Insert timestamp with JdbcTemplate in Oracle database ( ORA-01858 )

I've read a lot of stuff about this error, and still not found the mistake.
I'm using JdbcTemplate to insert a row in some table with some timestamp column
I'm pretty sure the timestamp is the problem, as if delete from the insert it works fine)
My code:
private static final String INSERT_CITAS = "INSERT INTO CITAS ("
+ "idCita, idServicio, " + "fechaCita, "
+ "idEstado, idUsuarioInicial) " + "VALUES (?, ?, ?, ?, ?)";
Object[] params = {
idCita,
citaQuenda.getIdServicio(),
getDateToDBFormat(citaQuenda.getFechaCita()),
ESTADO_INICIAL,
USUARIO_INICIAL };
String queryCitas = INSERT_CITAS;
super.getJdbcTemplate().update(queryCitas, params);
protected String getDateToDBFormat(Date fechaCreacion){
return "TO_TIMESTAMP('" +
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(fechaCreacion)
+ "', 'yyyy-mm-dd hh24:mi:ss')" ;
}
And having the next error:
org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL [INSERT INTO citas_55 (idCita, idServicio, fechaCita, idEstado, idUsuarioInicial) VALUES (?, ?, ?, ?, ?)];
ORA-01858: a non-numeric character was found where a numeric was expected
I've tried to execute the sql in some SQL editor having success, so I can't be more confused.
Being my params: [461, 100, TO_TIMESTAMP('2015-01-28 00:00:01', 'yyyy-mm-dd hh24:mi:ss'), 1, 8888] This actually works.
INSERT INTO citas (idCita, idServicio, fechaCita, idEstado, idUsuarioInicial) VALUES (457, 100, TO_TIMESTAMP('2015-01-28 00:00:01', 'yyyy-mm-dd hh24:mi:ss') , 1, 8888);
Any kind of help would be appreciated. Thanks in advance!
Don't convert back and forth between dates/timestamps and Strings.
Just pass a java.sql.Timestamp instance as a parameter:
Object[] params = {
idCita,
citaQuenda.getIdServicio(),
new java.sql.Timestamp(citaQuenda.getFechaCita()),
ESTADO_INICIAL,
USUARIO_INICIAL };
String queryCitas = INSERT_CITAS;
super.getJdbcTemplate().update(queryCitas, params);
I will go out on a limb here, and think I may see the problem. getDateToDBFormat() method is returning a String type, which contains the text, "TO_TIMESTAMP(...)". That is not a date or timestamp parameter. It is a string parameter. You need to do this instead:
Remove the TO_TIMESTAMP stuff from getDateToDBFormat() and have it just return the formatted DATE/TIME value (the format you show is not an oracle timestamp, but a DATE type).
change your insert to:
"INSERT INTO CITAS ... VALUES (?, ?, TO_DATE(?,?) , ?, ?)"
Where the parameters to the TO_DATE call are the return from getDateToDBFormat() and the second parameter is the date format mask. However, can't you just get rid of that mess and bind a Java Date type (or jdbc sql equivalent) directly?
That should work.

Inconsistent datatypes: expected DATE got NUMBER - when inserting into DB

I'm trying to insert a row in a DB table and I keep getting
ORA-00932: inconsistent datatypes: expected DATE got NUMBER
I don't know why this is happening since all dates are SYSDATE
String insertNewAlarmStat =
"insert into alarmes (id_alarm, alarm_key, id_notif, sever, urgency, date_hour_start, date_hour_modif, date_hour_end, " +
"state, state_rec, date_hour_rec, id_user_rec, id_system_rec, " +
"type, cause, " +
"num_events, id_entity_g, type_entity_g, " +
"desc_entity_g, problem, " +
"time_urg_act, max_urg_act, time_end, time_arq, lim, rec_oblig, dn, num_events_ps, id_alarm_o, id_notif_o, text_ad, domain, date_hour_reg) " +
"values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, SYSDATE, SYSDATE, SYSDATE, SYSDATE, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, SYSDATE)";
PreparedStatement prpstmt = null ;
try {
prpstmt = conn.prepareStatement(insertNewAlarmStat);
prpstmt.setInt(1, randomNumberGenerator());
prpstmt.setString(2, UUID.randomUUID().toString());
prpstmt.setString(3, UUID.randomUUID().toString());
prpstmt.setInt(4, randomNumberGenerator());
prpstmt.setInt(5, 8);
prpstmt.setInt(6, 8524);
prpstmt.setString(7, UUID.randomUUID().toString());
prpstmt.setString(8, UUID.randomUUID().toString());
prpstmt.setString(9, UUID.randomUUID().toString());
prpstmt.setString(10, UUID.randomUUID().toString());
prpstmt.setString(11, "KABOOM");
prpstmt.setInt(12, 8);
prpstmt.setInt(13, 43);
prpstmt.setString(14, UUID.randomUUID().toString());
prpstmt.setString(15, UUID.randomUUID().toString());
prpstmt.setString(16, UUID.randomUUID().toString());
prpstmt.setString(17, UUID.randomUUID().toString());
prpstmt.setInt(18, 2);
prpstmt.setInt(19, 224);
prpstmt.setInt(20, 2);
prpstmt.setInt(21, 224);
prpstmt.setInt(22, 2);
prpstmt.setInt(23, 224);
prpstmt.setInt(24, 2);
prpstmt.setString(25, UUID.randomUUID().toString());
prpstmt.setString(26, UUID.randomUUID().toString());
prpstmt.setString(27, UUID.randomUUID().toString());
prpstmt.setInt(28, 2);
prpstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I've tryed this way and I've tryed to remove the SYSDATE's and add some:
prpstmt.setDate(number, getCurrentDate()); //getCurrent date returns sql.Date
However, the error is the same
I've been debugging for some time now and I can't seem to find the problem, what am I missing?
Check the position of your columns compared to the values you are sending for datatype agreement.

VB6, RDO AND QUERYSTRING

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.

Resources