When I try to get all the rows from a table using my JPA repository the query Hibernate is generating is like this:
Select field1, field2, ... from ... where (field1, field2) IN (select f1, f2 from ....)
It is working properly when my db is PostgreSQL. Now I am about to migrate to Ms SQL Server. My repository looks like this
#Override
public List<VesselVisit> getAllVisits() {
return this.visitsRepository.findAll();
}
The data model has a parent entity that and has a relationship OneToMany to the children using a composite key.
It is not possible to implement this using SQL Server?
You can Use Inner Join Instead of In Operator in SQL Server For achieving it.
Related
I want to aggregate over a table that has a boolean column which I want to combine with a logical OR.
In plain SQL (postgres) I would write something like:
sql
SELECT t.agg_column, bool_or(t.bool_column) FROM mytable t GROUP BY t.agg_column;
How can I reflect that in a (non-native) Spring Data #Query annotation? I am looking for something on the lines of:
java
#Query("t.agg_column, bool_or(t.bool_column) FROM mytable t GROUP BY t.agg_column;")
but that doesn't work, since it doesn't know bool_or.
I have this SQL query:
select ts.scorename from content_package cp
join content_package_content_package_components cpcps on cpcps.content_package = cp.id
join content_package_component cpc on cpc.id = cpcps.content_package_components
join tests t on t.id = cpc.assessment
join test_scores ts on ts.tests_id = t.id
where cp.tag = 'C_TS_EN_ABSA_G_'
And want to convert it to JPA, ideally Specifications - is this possible?
you can write this query in JPQL but firs you need to create POJO class of your models. if you are using Intelij idea you can create your models in it by going to persistence section ,right click on your data source and select generate persistence mapping by (hibernate or database schema). after creating models you should change your table names to pojo classes in query and so on ....
Currently i am using the CreateSQLQuery query model to read the data from database using HIbernate. Now, I want to modify my query by using either HQL or Hibernate Criteria. My query looks like as follows.
select concat(d.AREA,' ',d.CITY) as location, a.TRANSFERRED_DATE as ActualTransferDate, concat(c.SCAN_CODE,',',c.SERIAL_NO) as ScanserialCode, c.MODEL_NO as ModelNum, c.ASSET_NAME as AssetName from table_transfer a, table_category b, table_asset c, table_location d where a.ASSET_ID = c.ASSET_ID and b.ASSET_CATEGORY_ID = c.ASSET_CATEGORY_ID and a.TRANSFER_TO_LOCATION=d.LOCATION_ID"
I am not sure how can i can convert this to Hibernate SQL or Criterion based query. Can any one help me?
You can introduce the fields for location and scanserialCode in your entity and mark them as #Formula
e.g.
#Formula("concat(d.AREA,' ',d.CITY)")
private String location;
See an example here or here
Then use join and where in HQL or Hibernate Criteria
I am new to OpenJPA
I am trying to insert data into a table some of which comes from another table. Below is the scenario.
Table1: id, app_name, app_version, app_active
Table2: id, app_name, app_version, dev_name, dev_Lastname, dev_shortname,
Pojo1 maps fields to column of table1
Pojo2 maps fields to column of table2
Query:
insert into table2 ("dev_name","dev_lastname","dev_shortname") select t1.app_name, t1.app_version from Table1 t1 where t1.app_name = ?
i dont know how to run this query using Openjpa and how to map these fields to each other in two pojos.
if I use the Query object then what about the pojos?? will they come in use?? I mean will i need to do transaction.save??
Any help with a sample code appreciated.
Your query manipulates data directly in the database while JPA handles the object relationships.
Im not sure what problem you are trying to solve but can you read the Pojo1 convert it to a Pojo2 object that you just save?
Edit:
In you java application first retrieve your Pojo1 from the entity manager with some query.
Then Construct corresponding Pojo2 objects with the fields you want from Pojo1. Then just to entityManager.persist with your new Pojo2 objects.
This is quite complex to perform what you showed could be made in a one line sql statement.
I have a sql query like this
Select * from DB1.Table1 a left join DB2.Table2 b on a.dCode = b.dCode where bID = 123;
How can I create jpa code for the above query? what would be the params of JpaRepository
public interface TestRepository extends JpaRepository<???, ???> {
#Query("???")
??? myTestQueryCOde(Integer bID);
}
What would be at ???
I am not sure if Spring JPA Data support cross DB queries, but have a look at Cross database joins in JPA