How to fetch jsonb column through jpa springboot? - spring-boot

I have one table which contains one column 'details' as jsonb data type. The content is
{
"name": "username",
"value": "user1",
"is_required": true
}
for one row.
I want to write one jpa hibernate query in spring boot to fetch this record when name == username for details column.
Something like this:
select details->>'value' from table where details->>'name' = 'username';
This syntax does not work in spring boot hibernate query.

I got the answer. Use
#Query(value="select details->>'value' from table where details->>'name' = 'username'", nativeQuery=true)

Related

How can I write a custom query with spring in mongo?

I'm trying to make a query and fetch data over mongodb with Spring. But I don't know mongo and spring very well. I have these fields in my table. How can I write a custom query with this query in SQL? I mean give the qp value of the record whose id is this. How can I write this please help..
SELECT qp
FROM tableName
WHERE id = request.getId();
my custom query method is it true?
public Object findQueryParams(ProcessInfo processInfo ){
Query query = Query.query(Criteria.where("id").is(processInfo .getId()).is(processInfo .getQueryParams()));
return query;
}

Spring Data JPA - how to filter returned data based on a fields values?

I am trying to figure out how do I filter what data is being returned to me in Spring JPA.
I know that with Spring JDBC, I get full controll and I can basically write a query like:
SELECT * FROM CAR
WHERE ACCIDENT_DATE IS NULL
OR BUY_DATE >= CURRENT_DATE
ORDER BY CAR_NUMBER
But, with Spring JPA, we dont write queries, instead we write entities like
#Entity
#Table(name = "CAR", schema = "MY_SCHEMA")
public class Car {
#Id
public Long carNumber;
...
}
What is the way to filter which Cars are returned based on weather the
ACCIDENT_DATE is NULL and
BUY_DATE is greater than CURRENT_DATE
, ordered by CAR_NUMBER in Spring JPA?
With the help of #DirkDayne, figured out how to do this. Thank you Dirk
#Query("select c from CarEntity c where c.accidentDate is null or c.buyDate >= CURRENT_DATE")
List<CarEntity> getAllAvailableCars(Sort sort);
, then call it in service as:
List<CarEntity> cars= (List<CarEntity>) carRepository.getAllAvailableCars(Sort.by("carNumber"));

Entity Graph for nested associations of tables in Spring JPA

There are 3 tables in my db:
SalesOrderMaster
SalesOrderDetail
CustomerMaster
I have repository class SalesOrderDetailRespository.java which has this method findAll()
#EntityGraph(attributePaths = {"SalesOrderMaster"}, type = EntityGraph.EntityGraphType.FETCH)
List<SalesOrderInfo> findAll(#Nullable Specification<SalesOrderInfo> spec);
This is till now fetching the SalesOrderMaster record and associated SalesOrderDetails records. Now my requirement is in SalesOrderInfo class , I have added a new property for showing the Customer Information like Name , country which are there in CustomerMaster table.
The customerMaster table is related to SalesOrderMaster by customerId column. How do I add CustomerMaster table in the existing EntityGraph so that I can get this info?

How to get column name from JPA

My code:
empDet emp = repository.findByActiveFlag("Y");
Here empDet is a entity with 3 columns:
empid
empName
ActiveFlag
Table with 10 rows so i will use foreach
emp.forEach(e-> {
---mycode---
---and here i got values--
});
Now my question is how to get Column name from Jpa or foreach loop
If you have object of Entity class (empDet) in JPA then from that object you can get the all properties of that class.
As you know in JPA class represent table in database and properties represent columns of that table.
if you have an object of empDet e.i emp
empDet emp=repository.findByActiveFlag("Y");
Field[] members = emp.getClass().getDeclaredFields();
for(Field member:members){
System.out.println(member.getName());
}

Spring jpa column defaultvalue

I'm trying to do the following
#ManyToOne
#JoinColumn(name="tier_id", columnDefinition = "INT default 1", nullable = false)
public Tier getTier() {
return tier;
}
but while inserting the record I get
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'tier_id' cannot be null
I want default tier_id to be 1 if it's not set, but it's not working. How can I correctly set this up?
The default SQL value is only used if the column is not present at all in the insert statement.
But Hibernate doesn't omit columns in insert statements. If you create an entity instance with a null tier, and persist it, you're effectively saying Hibernate that you want to persist an entity with a null tier, and Hibernate explicitely sets the tier_id column to null, since that's what you told it to do.
So, if you want your entity's tier to be the tier with ID 1, then do it explicitely in your application code:
SomeEntity e = new SomeEntity();
e.setTier(em.getReference(Tier.class, 1));
em.persist(e);

Resources