I am working with Java EE and I try to create a web application. I want to store the session data into the database.
The ways that I have considered are:
Create tables to be able to store the data (I did not like this approach, because for every different web app we need to create different tables on database, and I think if you have complex session data this will be painful, to have all the relations etc.).
Create Java class for holding the data, and store the JSON representation to the database. So when you retrieve the session data, you convert it back to Java object, with Jackson for instance.
Store the serialized Java class object, and after deserialize it and use it.
I think that approach 2 and 3 is somehow more generic and don't need too much effort.
Are these good approaches? Is there some other approaches better that that?
What do you suggest me?
You should use Hibernate framework. It will automatically reduce lot of your work.
Refer https://www.javatpoint.com/hibernate-tutorial.
Related
How to convert from json into java objects data and save into database? As I understand the only one way is using DTO? Or I'm wrong?
There is application which returns something like "dump" of database. My aim is to deserialize it and save into database. The problems: in some "object" fields there are ids, there are some exceptions regarding constraints during saving into database.
You need to use google Gson Api, following links show how-tos:
http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
Make sure DTO implements Serializable interface before persisting object into database... i would rather serialize on file system and store path into database because i do not like storing in CLOB or BLOB columns... too expensive queries.
Another option is to use Jackson, some examples could be found here. We use it in production for almost two years and find it powerful and handy.
I'm fairly new to java web applications and I am undertaking the task of learning JPA. However, it is not explicitly clear what it means for an entity object to persist. I think I have an idea, but I would rather not assume its meaning.
I am referencing the Oracle JPA Doc, but they continue to use the words like "persist" or "persistence" when describing persistent fields/properties. Can someone shed some light on this idea of persistence? And maybe define what it means for an instance of an entity to be persistent?
And if you could not use the word "persistent" (or any form of the word) in your definition that would be much appreciated. A simple answer would be great, but more in-depth explanations are definitely welcome! Thanks so much!
Persistence simply means to Store Permanently.
In JAVA we work with Objects and try to store Object's values into database(RDBMS mostly).
JPA provides implementation for Object Relation Mapping(ORM) ,so that we can directly store Object into Database as a new Tuple.
Object, in JPA, are converted to Entity for mapping it to the Table in Database.
So Persisting an Entity means Permanently Storing Object(Entity) into Database.
Hope this Helps!!
"Persist" means "lives on after the application is shut down". The object is not just in volatile memory; it's in more permanent storage on disk. If the application is shut down, or the user ends their session and begins a new one, the old data is still available from permanent storage on disk.
Databases store information on disks, unless they are in-memory versions that give you the advantage of using SQL but little else. If you use a relational SQL database, you get a query language that makes it easy to Create/Read/Update/Delete information without having to worry about how it's stored on the disk.
SQL databases store relations on disk using different data structures (e.g. B-Tree). Relations are defined in terms of tables and columns. Each record in a table consists of a tuple of row values. Objects have to map tables and columns to objects and attributes using object-relational mapping. JPA generalizes this idea and builds it into Java EE, following the example of implementations like TopLink and Hibernate.
NoSQL databases, like MongoDB, also store information on disk as documents rather than relations.
Object databases serialize an object and all its children using formats like Java serialization, XML, JSON, or custom formats (e.g. Google protocol buffers).
Graph databases, like Neo4J, can be thought of as more general cases of object databases.
I seem to keep tripping over this problem. And I can't seem to get a straight answer on the way to approach it.
I have an MVC web app using Java, Tomcat, JSP, MySQL, etc. I'm not using a framework. I have Model Entities representing each table in the database. I'm using DAOs to access the database.
Say you have a Person, a Job, and an Application.
In the View, you need to relate those together to produce a row for a listing.
So far, I've been doing JOINs in the DAO and returning a long SQL Result from the ResultSet using ResultSupport. Then I loop through it in the JSP using a JSTL forEach tag.
I understand this is not the best way to do it.
But then, what is it I need to create in the DAO to pass back each row of data? Do I define a new object with all the field names I need in the view? Is this a DTO? Or do I stuff the original objects in a HashMap and pass back that?
When I ask the database, I assume that doing a join is going to be more efficient than doing a bunch of small queries to return the individual objects.
There must be an accepted pattern for doing this? I would prefer to just follow an accepted solution.
Can someone please point me in the right direction?
Thanks
If you only have one single view, then creating a plain old java object (POJO) works fine. I would create an POJO which contains all the fields you need for a view. Then create a collection (E.G. ArrayList). The collection of POJO's is your Data Transfer Object (DTO).
The database connections are costly. So you want to open the database, get the data and store it your POJO collection. You then either close database connection or return to a pool (if you are using database connection pool). The point is, don't try to create an html display while holding on to a live database connection. Get the data, close the database connection (or return to a pool), then perform your display logic (I.E. the View).
Yes, you want to select all the information at once (do a database join). Same idea, use the database connection and then close it as quickly as possible. You want to avoid multiple open/close, open/close, etc.
Hopefully that helps. You might want to narrow down your question to something more specific. It's really difficult for folks to answer broad architectural questions . Good Luck.
I am developing a new web application with Struts2, Spring and Hibernate as its core building blocks.
We have created POJO classes with respect to hibernate mapping files.There will be some inputs from users which needs to be updated in to the underlying database
e.g registration or updation.
We have few option like creating new POJO/DTO for action classes which will be filled by the Struts2 and than we can transfer them to the service layer where we can convert those DTO to the respected hibernate POJO else we can expose same POJO to struts2 so that the framework can fill them with the user input and we need not to do the work for conversion and creating extra set of classes.
Application will be not big in size and will have a medium size application tag.
My question is what is the best way to transfer this user input to underlying hibernate layer to perform data base specific work.
Thanks in advance
I'd prefer the "DTO" approach in this case since you then can validate the input first and trigger updates only when wanted.
However, you could use detached entities as DTOs and reattach them when you want to create or update them. If you don't want the web part of your application to depend on Hibernate and/or JPA you might need to create another set of classes (unless you don't use a single annotation).
You'll get both answers on this.
With Struts 2 I tend to use normal S2 action properties to gather form values/etc. and use BeanUtils to copy them to the Hibernate objects. The problem with exposing the Hibernate objects to the form, like with ModelDriven etc. is that you need to define whitelists/blacklists if you have columns that should not be set directly by the user. (Or handle the problem in a different way.)
That said, I'm not fundamentally opposed to the idea like a lot of people are, and they're arguably correct.
I am using Java and SOAP.
I have two webservices. One (A), which generates some data, and one (B), which will update that data given specific parameters.
My question is: How can I save the data after it is generated from A for B to use?
I have read that using stateful webservices is not preferable. Instead, can I just write the XML response to a file and then get B to open and parse that file? This seems like a lot of work. What would be the 'normal' method to use here?
Thank you!
The usual enterprisey thing to do is to have a persistence layer (e.g. database) to save the data. You would map the XML to a relational model and store that, then regenerate the XML when B requires it.
Saving a file directly is pretty simple and might be the best solution - you'll need to manage locking etc. yourself. Or you could do a very simple DB with the XML in a column.