Pre-update and post-update in Hibernate - java

I want to use preupdate and postupdate concepts in my application.
The purpose is i want to get all values of a particulra record which is being updated and insert these values as a record along with new values in a table named audit.
Can any one give some suggestions or pointers on this.
Thanks,
Narendra

There are 2 ways to achieve this:
Hibernate Interceptors. See http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/events.html
Database level triggers/stored procedures.
You use the first if the pre/post functionality is business logic related -- i.e. your application is going to act on the update.
You use the second if you are only doing data level changes and the application doesn't need to know about it.

0 You can use HibernateInterceptors
1 You can do it programatically at service layer
2 You can either implement this things by database TRIGGERS then your DB will handle this.

You should have a look to Hibernate Envers. It does the Audit for you.
http://docs.jboss.org/envers/docs/index.html

Related

Is there a way to get data from table without repository and entity?

We have 2 projects with the same database using JAVA Spring-boot. The main projects hold the entity and repository files. Which is different to the other project. Since we don't understand the whole system of the main project we create ours. The problem is I need to get the data from a table without using repositories function like get, save, etc. I just need to query the table. Is this possible?
Thank you.
Depends on what you want
Only simple Data
A complete Object representing the data or part of the data
For the first one I would take JDBC(https://www.javatpoint.com/java-jdbc)
For the second one I would take JPA/Hibernate.
You can create a Entity which represents only the data you need and make it read-only(How to make an Entity read-only?).
And then you can create a simple CrudRepository/JpaRepository where you fetch the data.
Here's some pointers for your help:
DB Name/Host/User/Pass should be found in one of the *.properties files.
In Repositories, the table names are usually with #Table annotation.
To know what Queries a particular repository can execute, you can check with #Query annotation. Or the methods inside the repository may donate queries, like findByStudentId means select from student where id=.
Now with the above clues, you can just write a simple JDBC connection (it really depends on what db it is in the backend), connect to the DB above, and execute the queries you may want to.

Oracle ADF 11g Validate each entity impl before commiting changes

The code I'm working on is directly manipulating an Entity Object (EO) to insert 5-10k records into the EO's backing table. Yes, it is a bad practice but I don't have time to rewrite the application.
Is there a way, immediately before committing changes, to test to see if a primary key already exists?
Better yet, is there a way to customize the query executed when the transaction is committed? I'd like to add something like INSERT WHERE NOT EXISTS.
My sloppy alternative is to commit each change one at a time which will create undesired app-to-DB traffic.
Please let me know if I can provide any additional information.
Thank you VERY much for reading!
Better yet, is there a way to customize the query executed when the
transaction is committed? I'd like to add something like INSERT WHERE
NOT EXISTS.
You can control SQL Statements Entity Objects generate by overriding SQLBuilder class as below:
http://www.jobinesh.com/2013/02/customizing-sql-builder-class.html

Creating a Jooq Caching Layer

So, I'm working on using Jooq to create a caching layer over Postgres. I've been using the MockConnection/MockDataProvider objects to intercept every query, and this is working, but I'm having a few issues.
First, how do I determine between reads and writes? That is, how do I tell whether a query is an insert/update/etc or a select, given only the MockExecuteContext that's passed into the execute method in MockDataProvider?
And I'm a bit confused on how I can do invalidations. The basic scheme I'm implementing right now is that whenever a "write" query is made to a table, I invalidate all cached queries that involve that table. This goes back to my first question, on telling different types of queries from each other, but also brings up another issue: how would I identify the tables used in a query given only the sql string and the bindings (both are attributes of MockExecuteContext)?
Also, is this a correct approach at caching? My first thought was to override the fetch() method, but that method is final, and I'd rather not change something already embedded in Jooq itself. This is the only other way I could think of to intercept all requests made so I could create a separate, persistent caching layer.
I have seen this (https://groups.google.com/forum/#!topic/jooq-user/xSjrvnmcDHw) question, but I'm still not clear on how Lukas recommended to identify tables from the object. I can try to implement a Postgres NOTIFY, but I wanted something native in Jooq first. I've seen this issue (https://github.com/jOOQ/jOOQ/issues/2665) pop up a lot too, but I'm not sure how it applies.
Keep in mind that I'm new to Jooq, so it's quite possible that I'm missing something obvious.
Thanks!

How to implement database functionality effectively?

I am developing a Java Desktop Application which uses MySQL database. The DB has 6 tables. Every table, as usual, should allow CRUD (Create, Read, Update and Delete) operations.
I have designed 6*4 = 24 JPanels, 4 JPanels for each tables. Each JPanel have Components to take user input and perform the CRUD operation for which it is designed. For instance, a JPanel3 is designed for Create operation for Table1.
Now I want to know the following:
Q1. Would it be better to write 24 functions, each performing a specific function for a specific table?
Q2. This kind of situation is very normal as every application generally has many tables. So, Are all those applications use this approach of writing each function for each operation for each table?
Q3. As it is a Swing Application and every CRUD operation need the database connection, so Would it be better to make a connection to the database when the user starts the application?
or
Would it be better to make the database connection at the time when user clicks on the "save" or "edit" or "delete" or "create" button?
Q4. Would it be better to refer the connection from an instance variable which is shared by all the 24 functions? or Would it be better to have every function its own Connection?
Any other suggestions are also welcomed.
See this article about the DAO pattern, and then
see the Don't repeat the DAO! article so that you make a generic, reusable DAO.
In short - wrap your database access functionality in a single class and reuse that class from everywhere, thus effectively making your application not explicitly dependent on database operations - only on the DAO class (interface).
A1. No. The design should be guided by the requirements and the domain logic, not by technical considerations. Usually, it does not make sense to have all CRUD operations separately accessible to the user for each and every table. Write functions to perform those operations together that belong together.
A2. No. Nowadays, most applications use an Object/Relational mapper like Hibernate for this kind of thing. But still, there should be application logic on top that executes related operations together.
A3/4. Use a DB connection pool. O/R mappers generally do that automatically.
It would be great if you could join a project with experienced people. Your questions are understandable but... most project already have solutions for this.
I believe there are many more problems than the ones you describe, and you could work for months just to discover the questions ;-)
There are so many suggestions you might need that we can't even start.
Could you consider a framework such as Hibernate?
Although it would be complex for you to learn, in the process (and the recommandations) you would learn a lot about the database layer problems and solutions.
But to answer some of your questions :
Q1 : no, writing 24 functions would be a lot of duplication.
Q2 : certainly not.
Q3 : A database connection typically times out. I suggest to ask for one at appropriate times..., for example in the cases you describe.
Q4 : obtaining a connection should be shared code.
Q1: Could you have fewer JPanels and use a JComboBox to let the user pick wich table to operate on? That will probably save you some code.
Q2: In some way, yes. But see my answer for Q4.
Q3: Do the connection when the user clicks, and leave it open as short time as you need. Database connections takes resources.
Q4: It would be much better if you do all the Database-code and interacting in a single class, called Data Access Object, then is it much easier to change database from MySQL if you would like. See http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javadb/
What does the application do?
I hope you're using more descriptive names for your objects than "JPanel3" and "Table1"?
It would be better if you write no code.
Yes! Just drag and drop.
Data validation, reports and graphs.
Create a complete database program without writing a single line of code.
Common things such as new/edit/delete/search/update.
Use JDeveloper.

JPA - map entity to sql query?

I'm writing a JPA connector to a legacy sistem. The problem is, DB design in the system is.. well.. horrible.
I need to implement an entity that maps to three tables. I can do it using named sql queries, but the probem is, I have to use this entity in a OneToMany relation (this entity is on the many side). So how do I tell JPA it needs to use a specific named query with specific parameter?
And a subquestion: does any1 have any good on-line JPA documentation? I can't find anything decent :-/
I haven't actually found a way to do this with JPA. To solve problems like this I ended up using a named query.
As far as documentation, I have been using TopLink's and Hibernate's.
If you find a better way, please post it here!
Could you make a database view and then create an entity that maps onto that view? I don't know if this is possible, just a thought.
Take a look at #SecondaryTable(s). There's some examples of this in the Pro EJB 3 Java Peristance API book, page 237.

Categories