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.
Related
I have one table which has all the api audit information - Table name : api_audit
I have one table which has extra information about every api call - Table name : api_audit_info
Inside api_audit I have primary key as "transaction_id".
I want all the data from api_audit table and some data from api_audit_info table.
I have written a custom query like -
#Query(select c from ApiAudit c INNER JOIN ApiAudiInfo t ON c.transactionId = t.msgId)
But the issue is that the result type that I am getting this way contains only ApiAudit type data.
What shall I do to get data from both the tables. Please help.
Note: I am using JpaRepository as I need paginated data.
I am fairly new to Spring boot and JPA so not sure exactly which direction to look to.
Whenever I need to join data from more than 1 table, I am using Jdbi.
Here you have the official documentation:
Remember to include the required dependencies and configure a bean for Jdbi in your project.
Then I create a repository class, POJO and query with all of the information which I need. For example:
select c.transaction_id as transactionId, t.name as name from ApiAudit c INNER JOIN ApiAudiInfo t ON c.transactionId = t.msgId
Here you have some code samples from official documentation
After you map your data to POJO, you can use
public PageImpl(List<T> content, Pageable pageable, long total)
to return paginated data.
There is a big chance that there is a better solution, but this works for me every time.
I'm doing an exercise with Springboot that basically consists of a simple application with a Rest Controller who has to store an object received from POST request to a mysql db using JPA/Hibernate.
My problem is the following one:
the table has this structure:
And I have this pojo which has to map the table as an object:
If you pay attention, the table has a column named "CARD_HOLDER_FULL_NAME", but in the app, the card holder must be a separate object:
so, how do I specify that the fullName attribuite in the CaldHolderInfo class represents that column in the table?
I'm very rusty with Springboot/JPA/hibernate so I don't know how to proceed
You need to make CaldHolderInfo an Embeddable.
See JPA #Embedded And #Embeddable
And please, no images of code on this site.
Here is what I am trying to do:
1. Send an HQL to populate "User" Hibernate object.
2. Send a native SQL to retrieve a smaller dataset from a very large data column from "User" table.
3. Combine the mapped Hibernate object with the column result from step 2.
I read that ResultTransformer can be used to map the resultset from 2 to a Hibernate entity, which in my case the "User" entity. Is there a way to insert the results of the ResultTransformer mapping to my original User entity?
Here's some example: 1. HQL - From User. We use Hibernate mapping file for User and using Bytecode Enhancement, we set "xmlStringColumn lazy=true". String hql = "FROM User";
List users = session.createQuery(hql).list(); 2. we send a query
List resultXML = s.createSQLQuery(
"XML SQL to get specific data")
.setResultTransformer( Transformers.aliasToBean(User.class))
.list();
User dto =(User) resultWithAliasedBean.get(0); //Will need code to combine the User dto from the SQL to the original
ResultTransformer bundles one to many relationship objects into single object.
For example,
Class A{
Set b=new Hashset();
}
So if you start query from class A and add join on B it will end up in creating multipe objects of A.
ResultTransformer bundles data into single A object.
Can you please post code so we can understand what you are tyring to acheive!!!
I am new in hibernate. I am using SesssionFactory to get the session of the transaction and one way which I found after searching is used for setting few fields using set parameter i.e
Query query = getCurrentSession().createSQLQuery(
"UPDATE table_name set field1=:f1 where ID=:id");
query.setParameter("f1", f1);
query.setParameter("id", id);
but I want to update the whole row. I have already set the values in the entity class but is there a way to pass the values of the whole entity class to the database based on the id the id is the primary key for the table which I want to update.
you already have all data present in the hibernate entity object? Then just call the session directly:
getCurrentSession().save(myEntity);
to create a new object, or
getCurrentSession().update(myEntity);
to update an existing row.
If not sure, you can use:
getCurrentSession().saveOrUpdate(myEntity);
Take a look at Session#update (or saveOrUpdate). This will allow you to persist a complete, mapped, object to the database.
To be as OO as you can, you can get entity by session.get(entityClass, id);
And then after modifying object by setters/getters, you can save it back to the DB using update method :session.update(entity);
I am using hibernate retrieve results from my MySQL database into my Java project. Recently, I had a lot of redundant data and had to manually clean up the database by copying the required data into new tables and then renaming the newly created table to old table.
But, now querying the database with hibernate gives only one row as the result. I have manually checked the database and there are several different rows in the database. My query to Hibernate is something like this:
Criteria c = session.createCriteria(UserDto.class);
c.setMaxResults(100);
List<UserDto> users = c.list();
users contains 100 elements but all are the same.
The mapping of userDto is here.
Any idea what is happening here?
If your UserDto class has ToMany relations, then this is quite possible that outer join on them results in many records which all contain one and the same user data. You should use
session.createCriteria(UserDto.class).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
Double-check your mapping of the UserDto class to the database.
My guess is that you don't have it mapped to the table that you think you do.
As Sebastien mentioned, setting hibernate.show.sql to true should make this obvious.
Did you deleted the old tables? And in the configuration file what is the value for "hibernate.hbm2ddl.auto"?
I think the reason is these records have same id, so Hibernate treat them as the same record. You can check it.
I had same problem. In my case, the problem detected when I created a table in MySQL manually and I tried to read data from that table using hibernate and a dto class. After checking my dto class fields and database table, I figured out that there is a difference between table column named "id" and the class field which named dbId. The code was something like this:
#Id
#GeneratedValue
#Column(name="db_id", unique = true)
private long dbId;
So I edited the name and changed the code:
#Id
#GeneratedValue
#Column(name="id", unique = true)
private long dbId;
Which "id" was the correct name of databse table column and the problem has been solved.