JAVA DAO/Hibernate: persist & retrieve generic hashmap into/from Database field - java

I'm new to hibernate and I would like to store a hashmap of primitive objects into a oracle DB field.
In fact, depending on the process, different keys are put inside the map so I cannot create a DB field for each key (as I don't know them upfront. also I would like to put all the information in a single DB field).
Is it possible to do so? should data be compressed?

Related

Best datatype to store a java object to Oracle database

I want to save a java object (say a Person object) into a column in a table in Oracle 12c database. What is the best datatype suitable for this?
You can store complex objects in Oracle in an XMLType or JSON format.
However it is probably the wrong tool for that job. If you are going to always access an object via a key, go for a dedicated Key-Value database product rather than an RDBMS. If you need to access objects via attributes, such as the person's name, then having that attribute buried in an object will be painful.
If the requirement is a single field in an Oracle database table, I'd suggest a CLOB (assuming it's only ascii and no binary data). When storing binary data, use a BLOB.
BLOB= Binary Large OBject
CLOB = Character Large OBject.

What is the best possible structure for a db to store configurations date

I have a requirement where i need to store different application config data , so just wanted to know the best possible way to to store it.
Currently I am using below fields for my db
id, name ,category ,city,value(text field), int_value, float_value, string_value,date_value,bool_value
value field will be used to store complex data which in turn are json object capable of storing different key value pair.
for eg.
value:
{ "is_enabled" : true,
"list_of_applicable_ids" : ["123","345","567","890"]
}
And the reason I have added different value data type field(int_value, float_value) because it will be easy to query on that fields and index will make this even faster.
So just wanted to know the better approach to store these kind of data in db.
will using only value fields enough for my requirements ?
Frequency of config changes are very less( once or twice in a month)
Take a look a file based databases.
An example would be MongoDB.
Mongo uses a data form that closely resembles JSON (called BSON), and could in your case store the entire config file without the need to predefined it's fields.

how hibernate manage serialization id while saving/fetching object in/from DB

As per my understanding to manage consistency serialization algo maintain one serialization id as private static final long serialVersionUID = 3170588813843556321L; in class while saving/fetching object from stream. and that id will be saved in that file/stream.
But with DB while we are NOT saving this in DB at all, so how hibernate check consistency during deserialization and throw java.io.InvalidClassException.
Please correct me if i am wrong at my understanding....
hibernate does not use serialization to store your classes into the database. it works by way of object relational mapping and (simplistically) maps every attribute of your object to a column in a table.
only if you have specific types of attributes (say public Image getImage()) will it store "real" binary - it might map this attribute to a blob to use serialization/deserialization to store/load the image attribute of your mapped class.
checking for data consistency in hibernate is done either via explicit locking (it locks the rows that make up your entity as long as your entity is in use) or via optimistic locking - where it will add a "version" field to your entity, update it every time you modify the entity, and use sql of the form "update table set ... where version=:myVersion" to ensure that no 2 processes modify the same entity at the same time.

Java - Store HashSet in mysql

How would I go about storing a HashSet with an unknown size in a mysql table. I know I can loop through it and store it into a longtext field.
However when I retrieve that field and store it into a string temporarily it will use extra memory to store that huge string of info.
Is there any easy way to store a HashSet?
A SQL table with indexed columns is basically a hash set.
You shouldn't try to store (persist) a binary representation of a HashSet in a table. You should store (persist) the data of your HashSet as rows and columns and then read that data into your HashSet on the Java side.
In other words, you should be using a database to store your data directly rather than saving a serialized representation of a Java collection that holds your data. That's what a database is meant to do... store/persist data in a structured, consistent manner. If all you really need to do is to serialize a HashSet then why bother with a database at all?
I would turn it into JSON and store it as text.
There are libraries out there that convert to and from JSON with ease - eg googles gson
Yes. Each element in a separate record in a table with foreign key to the 'parent' table. Yhay is how JPA handles it.
You can serialize it and save it as BLOB type. Because HashSet implements Serializable. http://download.oracle.com/javase/1.4.2/docs/api/java/util/HashSet.html
http://download.oracle.com/javase/1.4.2/docs/api/serialized-form.html#java.util.HashSet

db4o to preserve identity of objects

Is there a way to preserve an object identity in db4o.
Suppose I store a BigDecimal in embedded db4o.
When I read it twice I get two distinct objects with the same value (which is quite obvious).
Is there any setting to force db4o to cache query results so that two queries would return reference to the same instance, or do I have to do it myself ?
From my experience, running the same query twice on the same ObjectContainer should return the same (identical) objects each time.
You should not close and reopen the ObjectContainer between the queries, if you need the objects' identity.
Db4o does use IDs and UUIDs internally and you can access those if needed. Also worth reading is this.
you can make an id for each object of yours by using it's UUIDs, I mean add an attribute ID for the object and give it UUIDs value and store it, to update an object you can retrieve it by that Id and update it

Categories