like query on long datatype in hibernate - oracle

When I execute like query on oracle on number datatype , it works fine i.e
select * from customer c where c.customer_number like '%400%'
Here customer_number is of 'number' datatype in oracle.I have to execute same query from hibernate but in my POJO this customer_number maps to a Long type. I am able to do this by HQL
i.e
from Customer c where c.customerNumber like %400%
How can I do it with criteria query?
Thanks in advance

criteria.add(Restrictions.like('c.customerNumber', '400', MatchMode.ANYWHERE));
see http://docs.jboss.org/hibernate/orm/3.3/api/org/hibernate/criterion/Restrictions.html#like%28java.lang.String,%20java.lang.String,%20org.hibernate.criterion.MatchMode%29

Related

How to get specific columns from relation table in laravel?

I am trying to fetch soecific columns data from relational table but it is giving me null
$allConsignments = Consignment::query();
$allConsignments->select(['id','customer_reference'])->with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get();
When I don't use select() then it gives correct data .
like this
$allConsignments = Consignment::query();
$allConsignments->with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get()
it is working but I also need specific columns from Consignment Table. what could be the reason?
You can also do like this.
$allConsignments = Consignment::query();
$allConsignments::with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get(['id','customer_reference']);
Actually, I also need to select the foreign key column from the table on which relationship is based. for example in my case I have customer_id in consignment table so it should be like that
$allConsignments = Consignment::query();
$allConsignments->select('id','customer_reference','customer_id')->with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get();
I need to select customer_id as well

how to get Null records from oracle database if column is Number type and nullable

I have Oracle database table with three columns i.e Id,RTOName,VehicleCode. my table looks like below
RTOName is the varchar2 type and VehicleCode is NUMBER(2,0) and is nullable field.
So I have the data like below and I want to fetch the records with Some VehicleCode and with null value. The table design like this already done so changing that will impact a lot in my application. I have a JPA Native Query that I used like this and I want to fetch the records with null values.
Query query = createNativeQuery("select RTOName,VehicleCode from tbl_vehiclecodes WHERE VehicleCode=#vCode");
query.setParameter("vCode", vehicleCode);
From above Query I will get only Non null valued record. Eg. for vCode parameter 61 I will get
Marathalli,61. If my vCode is null I have a problem and I wont get any record.
How to achieve this in Native Query?
I know that we can use IS NULL in the Query in where clause. Since I have some numbers here In my case how to solve this? Any help
Thanks
We can use OR here,
Following query will give you records with matched records for parameter vCode along with rows having null and in case of vCode is null you get the records only with null values.
Query query = createNativeQuery("select RTOName,VehicleCode from tbl_vehiclecodes WHERE (VehicleCode is null or VehicleCode=#vCode)");
Edit: considering the doubts from #Ranagal
If you want like in case of null value passed to vCode you want all the records having value in vehiclecode and also with null then we need to change the query like,
Query query = createNativeQuery("select RTOName,VehicleCode from tbl_vehiclecodes WHERE (VehicleCode is null or VehicleCode=coalesce(#vCode,VehicleCode))");

How to query for valid date which is in VARCHAR field using JPQL or Spring data jpa?

I have to get the min date from varchar field type.
I am able to do it using native query like below:
select * from license where TO_DAYS(STR_TO_DATE(license_date, '%Y-%m-%d')) IS NOT NULL ORDER BY STR_TO_DATE(verified_license_issued, '%Y-%m-%d') ASC LIMIT 1;
But I need to do it using JPA.
How to do it ?

How to validate nulls - oracle etl testing?

I am new to oracle and I would like to know how do we check the nulls in non null columns in oracle tables as part of the ETL testing process. (The two tables could be T1 and T2). Please let me know a sample query.
I have already tried
select count(*) from T2 where T2.column is Null;
Thanks, Santosh
'=' operator can not be used while comparing to null. Replace '=' to 'is' as per below query.
select count(*) from T2 where T2.column is Null;
Assume there is column name: id in the table record, and id should not contain any null value. Query to check this is:
select id from record where id is null;

How to create a blank/empty column with SELECT query in oracle?

I want to generate an output with blank/empty column with a "Select" query in oracle. I'm able to achieve this with below sql query:
SELECT CustomerName AS Customer, "" AS Contact
FROM Customers;
So when I run above sql query it returns a table with two columns "Customer" column with content in it and "Contact" column with no content or blank column.
I want to achieve same with oracle query. Any help will be highly appreciated.
I think you should use null
SELECT CustomerName AS Customer, null AS Contact
FROM Customers;
And Remember that Oracle
treats a character value with a length of zero as null.
In DB2, using single quotes instead of your double quotes will work. So that could translate the same in Oracle..
SELECT CustomerName AS Customer, '' AS Contact
FROM Customers;
I guess you will get ORA-01741: illegal zero-length identifier if you use the following
SELECT "" AS Contact FROM Customers;
And if you use the following 2 statements, you will be getting the same null value populated in the column.
SELECT '' AS Contact FROM Customers; OR SELECT null AS Contact FROM Customers;

Resources