Reputation: 651
For this specific project I need to serialize my entity layer (made of POJO's) to files. As I have the need for updating specific objects I would like to use one file per serialized object.
Example: Customer --ArrayList-> Order --ArrayList-> Product
When I edit a customer, and then serialize it using the java.io.Serializable interface, all fields, and their fields (please correct me if wrong), get serialized.
How would I apply serialization in such a way that only one object per file is used? I already have given each object a uniqe UUID which is used as filename when serializing.
If there are any frameworks that do File based ORM, that would be even better ;)
Upvotes: 0
Views: 737
Reputation: 4137
I'm not familiar with such framework.
What you can do is use other frameworks such as apache BeanUtils in order to perform the following recursive algorithm:
A. For each object gets its properties (assuming the object is a Java bean).
B. For each primitive field , write all primitives to file (you can use reflection to determine if a field is primitive or not).
C. For each non primitive file, write a special section in the file, pointing to the file name that will contain the object that is the value of the field.
D. Call recursively the algorithm for each non primitive field.
Similar approach can be done for collections -
HashMap, ArrayList and others.
The serializing code for the primitive elements can be the code provided by @Anshu
Upvotes: 0
Reputation: 7853
You can always read and write serializable objects using readObject
and writeObject
. Following is the example code:
import java.io.*;
import java.util.*;
import java.util.logging.*;
public class ExerciseSerializable {
public static void main(String... aArguments) {
//create a Serializable List
List<String> quarks = Arrays.asList(
"up", "down", "strange", "charm", "top", "bottom"
);
//serialize the List
//note the use of abstract base class references
try{
//use buffering
OutputStream file = new FileOutputStream( "quarks.ser" );
OutputStream buffer = new BufferedOutputStream( file );
ObjectOutput output = new ObjectOutputStream( buffer );
try{
output.writeObject(quarks);
}
finally{
output.close();
}
}
catch(IOException ex){
fLogger.log(Level.SEVERE, "Cannot perform output.", ex);
}
//deserialize the quarks.ser file
//note the use of abstract base class references
try{
//use buffering
InputStream file = new FileInputStream( "quarks.ser" );
InputStream buffer = new BufferedInputStream( file );
ObjectInput input = new ObjectInputStream ( buffer );
try{
//deserialize the List
List<String> recoveredQuarks = (List<String>)input.readObject();
//display its data
for(String quark: recoveredQuarks){
System.out.println("Recovered Quark: " + quark);
}
}
finally{
input.close();
}
}
catch(ClassNotFoundException ex){
fLogger.log(Level.SEVERE, "Cannot perform input. Class not found.", ex);
}
catch(IOException ex){
fLogger.log(Level.SEVERE, "Cannot perform input.", ex);
}
}
// PRIVATE //
//Use Java's logging facilities to record exceptions.
//The behavior of the logger can be configured through a
//text file, or programmatically through the logging API.
private static final Logger fLogger =
Logger.getLogger(ExerciseSerializable.class.getPackage().getName())
;
}
Upvotes: 0