Right now I have java program whose classes are currently POJOs and stored in volatile memory. These need to be persisted. As I understand it two popular choices are JDO and the Java Persistence API. For someone who know little about SQL, Torque, etc, which is the easiest way to add persistence to my program's data?
There are many ways to make data persist in Java, including (to name a few): JDBC, serialization, file IO, JCA, object databases, and XML databases. However, the majority of data is persisted in databases, specifically relational databases.
You can use serialization to persist an object's data between instances, which enables you to store values and retrieve them the next time that the object is instantiated. In this walkthrough, you will create a basic Loan object and persist its data to a file.
In Java, all objects are dynamically allocated on Heap. This is different from C++ where objects can be allocated memory either on Stack or on Heap. In JAVA , when we allocate the object using new(), the object is allocated on Heap, otherwise on Stack if not global or static.
The traditional way to serialise to the filesystem is to use Java Serialisation. However you need to implement Serializable everywhere.
A simpler solution is to serialise to XML (and then dump to the filesystem) using XStream. You don't need to implement any interfaces, and most everything serialises and deserialises without further intervention. You can further customise the serialisation if required. The only problem I've ever had is serialising an inner class without intentionally serialising the containing outer class (this is due to the implicit this
reference)
Serialize the objects to the file system if you don't know SQL or relational databases.
You'll have to learn JDBC to use JDO, JPA, Hibernate, or anything else. Unless your POJOs are terribly complex I'd recommend starting there and working your way up.
Make sure you learn about normalization and proper design of indexes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With