I need to store java object in H2 database so that I can call the object's method in a easy way.
I read data type of H2. The only way I can store an java Object is to use type "OTHER Type".
However this data type employs serialization and deserialization. If I store a very large object, it means it gonna take much time and memory to do serialization stuff.
I just want to refer the object. Is there anyway to achieve it?
If H2 cannot achieve it, is there any other in memory database can achieve it?
Related
I have some Java objects stored in an Oracle database. I wish to know the structure and content of the objects. So, I want to retrieve the blob and convert them to xml/any other displayable form.
Is this possible? If yes, how?
I would extract the binary object from the database, create back the java object in memory (so you can also ensure the data is valid) and after that I would use a library like Protostuff to quickly serialize the object in XML.
The advantage of using Protostuff is that you don't need anything but the java object. The object "schema" is calculated at runtime if needed.
Consider also that Protostuff supports a lot of different formats, like JSON, Protobuffer, YAML, etc...
I am aware of what Serialization is however I have not found any real practical example describing the latter one (saving an object in a database taking advantage of the JAVA_OBJECT mapping).
Do I have first to serialize the object and then save it to the database?
In the case of MySQL, you don't have to serialize the object first, the driver will do it for you. Just use the PreparedStatement.setObject method.
For example, first in MySQL create the table:
create table blobs (b blob);
Then in a Java program create a prepared statement, set the parameters, and execute:
PreparedStatement preps;
preps = connection.prepareStatement("insert into blobs (b) values (?)");
preps.setObject(1, new CustomObject());
preps.execute();
Don't forget that the class of the object that you want to store has to implement the Serializable interface.
Serialization is used to save the state of an object and marshall it to a stream and share it with a remote process. The other process just need to have the same class version to deserialize the stream back to an object.
The problem with the database approach is that you will need to expose the databse even to the remote process. This is generally not done due to various reasons, mainly security.
Requirement: I need to dump the object graph of a Java application at various points of time during the application's execution. But most importantly, the dump needs to contain metadata for all objects in the graph e.g. the reference ids as obtained from System.identityHashCode(object).
What I tried: I found out serialization is a goto solution for obtaining the object graph.
I tried a couple of different solutions, including the built in Serialization facility in Java, JAXB and XStream. But I have no clue on how to get any of these libraries to spit out the objects' metadata such as the original reference IDs for the objects getting serialized.
Question: I was wondering if there was an easy way to include additional information/metadata about each object in an object graph that gets serialized?
While i am interested in specifically storing the objects' reference id, as obtained by System.identityHashCode(), any pointers on storing any data in general will be acceptable.
In case adding such metadata is not possible, that information will be useful as well.
NOTE: I ask this question despite the knowledge that the reference ids obtained from the System.identityHashCode() method are not required to be unique across different objects.
P.S. I am a newbie with Java serialization.
I'm writing an application which needs to write an object into database.
For simplicity, I want to serialize the object.
But ObjectOuputStream needed for the same purpose has only one constructor which takes any subclass of OutputStream as parameter.
What parameter should be passed to it?
You can pass a ByteArrayOutputStream and then store the resulting stream.toByteArray() in the database as blob.
Make sure you specify a serialVersionUID for the class, because otherwise you'll have hard time when you add/remove a field.
Also consider the xml version for object serialization - XMLEncoder, if you need a bit more human-readable data.
And ultimately, you may want to translate your object model to the relational model via an ORM framework. JPA (Hibernate/EclipseLink/OpenJPA) provide object-relational mapping so that you work with objects, but their fields and relations are persisted in a RDBMS.
Using ByteArrayOutputStream should be a simple enough way to convert to a byte[] (call toByteArray after you've flushed). Alternatively there is Blob.setBinaryStream (which actually returns an OutputStream).
You might also want to reconsider using the database as a database...
e.g. create ByteArrayOutputStream and pass it to ObjectOuputStream constructor
One thing to add to this. java serialization is a good, general use tool. however, it can be a bit verbose. you might want to try gzipping the serialized data. you can do this by putting a GZIP stream between the object stream and the byte stream. this will use a small amount of extra cpu, but that is often a worthy tradeoff to shipping the extra bytes over the network and shoving them in a db.
During serializing objects, can we assign name to different objects? So, that on the time of reading objects, i can call any object by its name and later on can access its members.
I can do it by assigning a unique field to each object and later compare it against that field but that will cost - O(n).
Is there any other way to fast access any particular object, serialized in a file of suppose 100 objects.
Thanks you
Put them in a map and serialise the map instead?
Why do all of the objects have to be in one stream? What if you just save each object in its own file and access the object by file name? If you really need a single file, you could archive them all in a ZIP file, and let it handle the indexing function for you.
Maybe it's overkill, but for that use case i would use a database. Something like Berkeley DB sounds appropiate:
http://download.oracle.com/docs/cd/E17277_01/html/GettingStartedGuide/index.html