ADF table from View Object without backend Database - java

For a project, I need to create a table to read the data from user in UI..and just need to create an XML document from these values which I want to do via calling a method...there is no back end DB for this...
What I have done is create a Entity object without existing Schema Object and created an view object from that Entity object....
But when I am running it, it gives an error..."View or table does not exist"..
What is wrong with this approach? or can anyone suggest me a better approach for this..

EOs represent a table in the database. So, as Endrik said, if you have no table you do not need an EO. You want a programatic VO. Try here.

Related

When to use #Put Method or straight away using query in Spring Java Data JPA?

When update an existing field in the table, what is the best practice to consider? Like I do write a lot of PUT method which overwrite the existing data when update ( I consider with large objects using a lot of logic and also calculation, it'd be better).
But what about sometimes I just need to update one field? I saw a pattern of using same or similar put method when people update data, in those function, they will check to delete the old one and put the new data into table.
Should it be better if we using an update set JPA query to change exactly what we need?
Like there's an existing function when user create or update the record they have, should I create then add one update set query inside that function so it would update or create into those new set table I create?
Thanks for reading this load of text, I hope to have a clear point of view to become a better programmer.
I did try to google to understand the definition but not so clear. I hope I can get a clearer point of view here.

Filling a table view from different DB tables in JavaFX

So, I need to fill a table-view from different DB tables connected through foreign keys, all I found is how to fill one from the same entity, do I need to create a different entity fill it with the required information, than fill the table view?
I would suggest you look into creating DTOs when sending entities to the view. By using DTOs you can easily create a custom object that contains the exact variables you want to show in your table, for example:
Entity1 entity1 = // your db function here
Entity2 entity2 = ...
TableDTO tableDTO = new TableDTO(
// use a constructor to combine your two entities into one object!
)
// now map your dto to your table located in the view
You wont be able to load multiple db entities at once unless you are using an EntityManager, or have set custom database functions inside your database. Both of these could return the exact object you want for your view, but I'd strongly suggest using the DTO pattern as it keeps your code simple and much eaiser to change. It also allows for much easier communcation between front and back end and keeps any confidential or unwanted db variables out of your application view.
It would help if you included some code from your application but hopefully this will be enough to fix your table views!

processing a jsf datatable

I've a page with a datatable which can be considered a view on a table of a database. Some fields are static, but some others are inputText, which are meant to modify that field (in particular, all the fields of a specific column).
I would like to press a button and save the whole table.
In general, I'd like to understand how to read an entire table in a managed bean.
Thanks!
EDIT:
I'll try to be more clear:
imagine you've a NxM table of only inputText. Outside the table, a button with an action like #{SomeBean.process}. I'd like to have, in that process method, a List with length N and Row is an object with M fields.
If I understand correctly
I think, you need to save not a datatable, you need to save a rows from this table. With JSF you can use binding and when you need to save data, you just get rows from this binding and make data operations.
If you used some kind of object-relatational mapping like JPA it would be quite easy. Then you could create an entity class of your database table and fill your datatable with a list of your entity objects.
Then you could submit the whole datatable and merge all the changes with your database.
Some IDEs like Netbeans even create entity classes automatically with the help of a wizard. If you are on Netbeans, I recommend to read the JSF Crud tutorial. For Eclipse there should be similiar tutorials, but I am not so familiar with this IDE.

keeping the history of table in java

I need the sample program in Java for keeping the history of table if user inserted, updated and deleted on that table. Can anybody help in this?
Thanks in advance.
If you are working with Hibernate you can use Envers to solve this problem.
You have two options for this:
Let the database handle this automatically using triggers. I don't know what database you're using but all of them support triggers that you can use for this.
Write code in your program that does something similar when inserting, updating and deleting a user.
Personally, I prefer the first option. It probably requires less maintenance. There may be multiple places where you update a user, all those places need the code to update the other table. Besides, in the database you have more options for specifying required values and integrity constraints.
Well, we normally have our own history tables which (mostly) look like the original table. Since most of our tables already have the creation date, modification date and the respective users, all we need to do is copy the dataset from the live table to the history table with a creation date of now().
We're using Hibernate so this could be done in an interceptor, but there may be other options as well, e.g. some database trigger executing a script, etc.
How is this a Java question?
This should be moved in Database section.
You need to create a history table. Then create database triggers on the original table for "create or replace trigger before insert or update or delete on table for each row ...."
I think this can be achieved by creating a trigger in the sql-server.
you can create the TRIGGER as follows:
Syntax:
CREATE TRIGGER trigger_name
{BEFORE | AFTER } {INSERT | UPDATE |
DELETE } ON table_name FOR EACH ROW
triggered_statement
you'll have to create 2 triggers one for before the operation is performed and another after the operation is performed.
otherwise it can be achieved through code also but it would be a bit tedious for the code to handle in case of batch processes.
You should try using triggers. You can have a separate table (exact replica of your table of which you need to maintain history) .
This table will then be updated by trigger after every insert/update/delete on your main table.
Then you can write your java code to get these changes from the second history table.
I think you can use the redo log of your underlying database to keep track of the operation performed. Is there any particular reason to go for the program?
You could try creating say a List of the objects from the table (Assuming you have objects for the data). Which will allow you to loop through the list and compare to the current data in the table? You will then be able to see if any changes occurred.
You can even create another list with a object that contains an enumerator that gives you the action (DELETE, UPDATE, CREATE) along with the new data.
Haven't done this before, just a idea.
Like #Ashish mentioned, triggers can be used to insert into a seperate table - this is commonly referred as Audit-Trail table or audit log table.
Below are columns generally defined in such audit trail table : 'Action' (insert,update,delete) , tablename (table into which it was inserted/deleted/updated), key (primary key of that table on need basis) , timestamp (the time at which this action was done)
It is better to audit-log after the entire transaction is through. If not, in case of exception being passed back to code-side, seperate call to update audit tables will be needed. Hope this helps.
If you are talking about db tables you may use either triggers in db or add some extra code within your application - probably using aspects. If you are using JPA you may use entity listeners or perform some extra logic adding some aspect to your DAO object and apply specific aspect to all DAOs which perform CRUD on entities that needs to sustain historical data. If your DAO object is stateless bean you may use Interceptor to achive that in other case use java proxy functionality, cglib or other lib that may provide aspect functionality for you. If you are using Spring instead of EJB you may advise your DAOs within application context config file.
Triggers are not suggestable, when I stored my audit data in file else I didn't use the database...my suggestion is create table "AUDIT" and write java code with help of servlets and store the data in file or DB or another DB also ...

LinkedList with Serialization in Java

I'm getting introduced to serialization and ran into some problems when pairing it with LinkedList
Consider i have the following table:
CREATE TABLE JAVA_OBJECTS (
ID BIGINT NOT NULL UNIQUE AUTO_INCREMENT,
OBJ_NAME VARCHAR(50),
OBJ_VALUE BLOB
);
And i'm planning to store 3 object types - so the table may look like so -
ID OBJ_NAME OBJ_VALUE
============================
1 Class1 BLOB
2 Class2 BLOB
3 Class1 BLOB
4 Class3 BLOB
5 Class3 BLOB
And i'll use 3 different LinkedList's to manage these objects..
I've been able to implement LoadFromTable() and StoreIntoTable(Class1 obj1).
My question is - if i change an attribute for a Class2 object in LinkedList<Class2>, how do i effect the change in the DB for this individual item? Also take into account that the order of the elements in LinkedList may change..
Thanks : )
* EDIT
Yes, i understand that i'll have to delete/update a row in my DB table. But how do i keep track of WHICH row to update? I'm only storing the objects in the List, not their respective IDs in the table.
You'll have to store their IDs in the objects you are storing. However, I would suggest not trying to roll your own ORM system, and instead use something like Hibernate.
If you change an attribute in a an object or the order of items. You will have to delete that row and insert the updated list again.
How do i effect the change in the DB for this individual item?
I hope I get you right. The SQL update and delete statements allow you to add a WHERE clause in which you chose the ID of the row to update.
e.g.
UPDATE JAVA_OBJECTS SET OBJ_NAME ="new name" WHERE ID = 2
EDIT:
To prevent problems with your Ids you could wrap you object
class Wrapper {
int dbId;
Object obj;
}
And add them instead of the 'naked' object into your LinkedList
You can use AUTO_INCREMENT attribute for your table and then use the mysql_insert_id() function to retrieve the id assigned to the row added/updated by the last INSERT/UPDATE statement. Along with this maintain a map (eg a HashMap) from the java object to the Id. Using this map you can keep track of which row to delete/update.
Edit: See the answer to this question as well.
I think the real problem here is, that you mix and match different levels of abstraction. By storing serialized Java objects into a relational database as BLOBs you have to consider several drawbacks:
You loose interoperability. Applications written in other languages than Java are not able to read the data back. Even other Java applications have to have the class files of the serialized classes in their classpath.
Changing the class definitions of the stored classes will end up in maintenance nightmares.
You give up the advantages of a relational database. Serialization hides the actual data from the database. So the database is presented only with a black box. You are unable to execute any meaningfull query against the real data. All what you have is the ID and block of bytes.
You have to implement low level data handling by yourself. Actually the database is made to handle your data effectively, but because of serialization you hinder it doing its job. So you are on your own and you are running into that problem right now.
So in most cases you benifit from separation of concerns and using the right tool for a job.
Here are some suggestions:
Separate the internal data handling inside your application from persistent storage. Design your database schema in a way to enable the built-in database features to handle the data efficently. In case of a relational database like MySQL you can choose from different technologies like plain JDBC, object relational mappers like JPA or simple mappers like MyBatis. Separation here means to avoid to contaminate the database with implementation specific concerns.
If you have for example in your Java application a List of Person instances and each Person consists of a name and an age. Then you would represent that list in a relational database as a table consisting of a VARCHAR field for the name and a numeric field for the age and maybe a third field for a unique key. Then the database is able to do what it can do best: managing large amounts of data.
Inside your application you typically separate the persistent layer from the rest of your program containing the code to communicate with the database.
In some use cases a relational database may not be the appropiate tool. Maybe in a single user desktop application with a small set of data it may be the best to simply serialize your Person list into a plain file and read it back at the next start up.
But there exists other alternatives to persist your data. Maybe some kind of object oriented database is the right tool. In particular I have experiences with Fast Objects. As a simplification it is serialization on steroids. There is no need for a layer like JPA or JDBC between your application and your database. You are able to store the class instances directly into the database. But unlike the relational database with its BLOB field, the OODB knows your classes and the actual data and can benefit from that.
Another alternative may be JDBM or Berkeley DB.
So separation of concerns and choosing the right persistence strategy (and using it the right way) is a key concern for the success of your project. But doing it right is hard even for experienced developers.

Categories