I had two entiy class having same structure (temp entity and permanent entity) I want to create a view so that it can query from both the table in case of if I apply logic for search or advance search etc.
I am thinking about trying " select * from temp union select * from permanent " using #Immutable annotations
Related
I have the following query using which I want to create an entity class for the columns which I am retrieving from the query.
select e.emp_id,
c.company_code,
mg.emp_id as mangaer_code
from employee e
left join company c on e.emp_id = c.emp_id
left join manager mg on e.emp_id = c.emp_id = mg.emp_id
How to create an entity class from these columns and what variables are needed to take it in entity class to refer to these columns?
View is a virtual table based on the result-set of an SQL statement or a function and JPA treats it as a regular table. Create a entity and use JPA annotations as below
#Entity
#Immutable
#Table(name = "employee_view")
public class EmployeeView{
//define the required columns from view here
}
For more details, refer to this article that I found https://medium.com/#jonathan.turnock/exposing-subset-view-of-the-database-with-a-jpa-repository-over-rest-5b9d6e07344b
Hi I am using a spring boot app with Hibernate using Oracle as DB.
I have 5 classes named
1.Reqest -> Mapped with Request Table
2.Team -> Mapped with Team Table
3.Partner -Mapped with Partner Table
4.Customer -> Mapped with Customer Table
Now I want to Display a Summary of Request on summary screen of the app where all the information from above-mentioned tables is needed.
Suppose I create a bean class as follows.
public class SummaryBean{
Request req;
Team team;
Customer cust;
Partner part;
//Getter setters;
}
Now since I have all the tables mapped with Java classes I can use hql join query to fetch data.
I don't want use plain SQL query with join and then iterate the resulting Object[] list from hibernate query and stub data into SummaryBean manually.
All the above-mentioned tables have REQ_ID as joining column
My question is How can I make hibernate map the result of that query to SummaryBean object?
Is it possible at all?
You can use constructor query.
Something like
"select new SummaryBean(req, team, cust, part) from (here you join your tables)"
You need to provide a constructor for the SummaryBean with those 4 types.
Note that the SummaryBean class doesn't have to be mapped, but you might have to use fully qualified name in the query (packageName.className).
I have an Archive database where there are 2 tables with the same structure, EvntSumaryT which is active and ArchiveEvntSumaryT which is the archived. I want to change my current criteria api code which only queries EvntSumary so that it will fetch data from both tables based on the user selected dates and return the results into a list.
My problem is that their is no Union feature in hibernate-criteria so how would I be able to query both tables at once and combine the results into a list?
Below is the code for my jpa entity classes. I've set up the active table's entity as the parent class and the child as ArchiveEvntSumaryT which extents from it since they share the same fields.
#Entity
#Table(name="EVNT_SUMARY_T")
#Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class EvntSumaryT {
//fields
//getters and setters
}
#Entity
#Table(name="ARCHIVE_EVNT_SUMARY_T")
public class ArchiveEvntSumaryT extends EvntSumaryT {
}
And this is my criteria api query in the DAO class before archiving the database.
public List<EvntSumaryT> getAllTransaction(SearchCriteria sb) {
//criteria
return ls;
}
How would I be able to query both tables and return the results into a list without the Union function?
I figured it out. Instead of using inhertance to replace the union function I ended up creating a view in the database that's the union of the two tables and mapped the entity to the view.
I'm looking to load data into a hibernate entity by running something like select * from <table> where id = ?. I'm only interested in the data sitting in <table> -- no joins whatsoever.
I can achieve this by using the following:
T result = (T) getSession()
.createSQLQuery("SELECT * FROM " + tableName + " WHERE " + idColumn + " = ?")
.setInteger(0, id)
.setResultTransformer(new AliasToBeanResultTransformer(type))
.uniqueResult();
But this only works for entities that don't have relationships. For any entity with a relationship to another, i get a PropertyNotFoundException when the result transformer is trying to set some <relationshipName>_id property on my entity (eg. org.hibernate.PropertyNotFoundException: Could not find setter for administration_id on class com.xxxxxx.xxxx.model.AdministrationPIN
Are there any other transformers that could automatically handle this. Or possibly some sort of object binder that could be used from an entity map, if I was to use the Criteria.ALIAS_TO_ENTITY_MAP result transformer instead?
Update
It's important that all of the data sitting in the table is loaded into the entity, including the relationship data. So if entity A has a OneToOne with entity B, and I load entity A using this method, a.getB() should return an instance of B, but the B instance should only be populated with its identifier.
I have an Entity. And sometimes I need this object also contains some value, call it 'depth'. The query may look like 'select b.id, b.name, b..., count(c.id) as depth from Entity b, CEntity c where ...'. So I've created class NonHibernateEntity which extends Entity. And then results of query written above are perfectly stored as List of NonHibEntity, which consists of all the fields of Entity (as they are extended), and property 'depth'. I make it by setting aliasToBean results transformer: .setResultTransformer(Transformers.aliasToBean(NHEntity.class)).
But, it is annoying and inconvenient - to specify all the aliases of all the needed fields.
And then, if I want to save one of this object to DB - session.saveOrUpdate((Enity)nHibEntity) - there are an exception about nHibEntity isn't Hibernate Entity.
I heard about storing 'entity' as field in NonHibEntity (aggregation, not inheritance). But it seems this is rather inconvenient too.
What do you think? What is an appropriate solution?
A Formula column mapping may be suitable for your needs. I would try this first.
If this causes performance issues as you fear, you might try a mapped class hierarchy with this field only in the child, and mapping both to the same table. Not sure this will actually work though...
As a last resort, do what you've got now using a non-mapped class, but with the entity as a field in your other class - aggregation instead of inheritance as you say, and make sure there's a way of retrieving the mapped entity from the unmapped one so that you can save. It be sensible to make it a Decorator, so that it's both a subclass and aggregate and you can continue to ignore the distinction in much of your code.
With the non-mapped subclass and/or aggregate, however, you'll have to pull out the entity in order to save.
If somebody want to know - I solved this problem in such way:
just made this calculated field as #Transient, and then
List<BaseEntry> result = new ArrayList<BaseEntry>();
Iterator it = session()
.createQuery("select b, (select count(p.id) as depth from BaseEntry p " +
" where ... ) as d" +
" from BaseEntry b " +
" where ... ")
.list().iterator();
while ( it.hasNext() ) {
Object[] row = (Object[]) it.next();
BaseEntry entry = (BaseEntry) row[0];
Long d = (Long) row[1];
entry.setD(d);
result.add(entry);
}
return result;
It works good, and seems that it can be easily supported in future