|
-
May 26th, 2013, 07:19 PM
#1
Override/Extend EJB3.x
Hello everyone,
I've been having this idea for some time. I doubt it's new but still I've found no related info after an extended search.
The idea is to automate database regs logical status management by abstracting it on the java level. To simplify the gibberish I've just wrote I'll give an example.
Let's suppose we have a database table representing some physical entity, cars for example:
Code:
-----------------------------------------------------------
| id_car | brand | model | year | color | price | deleted |
-----------------------------------------------------------
where "deleted" would be a boolean field representing a logical deletion of a registry.
When I delete some registry in my app I do not want to actually delete it, just change the "deleted" value to "TRUE" to avoid database relations breakdown. It's a pretty common approach for all relational databases when there is a need to maintain some historical data.
The main part of the application woks with "active" data only (deleted=FALSE), since we do not want to visualize the already deleted data. So to achieve this in every single query we have to manually add the WHERE cars.deleted=FALSE clause. This can become a problem when there are constant database modifications (creation of new relations/tables), dozens of similarly controlled tables and a big development team where communication of every change in the database is just impossible.
In theory this problem could be solved by overriding or extending the session beans processors with some logic to add the necessary clauses to the custom/generated JPAQL expressions, so we would not need to specify the logical deletion parameter every time we want to read some data.
Let's see the classical autogenerated findAll method following the car table example:
PHP Code:
@SuppressWarnings("unchecked") public List<Car> findAll() { LogUtil.log("finding all Car instances", Level.INFO, null); try { final String queryString = "select model from Car model"; Query query = entityManager.createQuery(queryString); return query.getResultList(); } catch (RuntimeException re) { LogUtil.log("find all failed", Level.SEVERE, re); throw re; } }
After execution this method returns the whole car table including the "deleted" regs. And the simplest way to avoid the deleted data would be to add the following to the JPAQL query string:
PHP Code:
final String queryString = "select model from Car model where model.deleted=false";
What I want is to avoid the last manual change making the framework to add the clause automatically.
So the question is:
1. Are there any ways I could achieve this without rewriting every session bean method? Maybe it's possible to override the JPA query processor like it's commonly done in Hibernate generic DAOs...
Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?
I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)
//always looking for job opportunities in AU/NZ/US/CA/Europe :P
willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));
USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!
-
June 25th, 2014, 03:54 AM
#2
Re: Override/Extend EJB3.x
I'm still looking for a solution to this one...
Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?
I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)
//always looking for job opportunities in AU/NZ/US/CA/Europe :P
willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));
USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|