Java - Store HashSet in mysql - java

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

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.

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

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?

Import nested Json into cassandra

I have list of nested Json objects which I want to save into cassandra (1.2.15).
However the constraint I have is that I do not know the column family's column data types before hand i.e each Json object has got a different structure with fields of different datatypes.
So I am planning to use dynamic composite type for creating a column family.
So I would like to know if there is an API or suggest some ideas on how to save such Json object list into cassandra.
Thanks
If you don't need to be able to query individual items from the json structure, just store the whole serialized string into one column.
If you do need to be able to query individual items, I suggest using one of the collection types: list, set, or map. As far as typing goes, I would leave the value as text or blob and rely on json to handle the typing. In other words, json encode the values before inserting and then json decode the values when reading.

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