Want to store Object in MySQL database
Asked Answered
A

4

7

I have a variable in java which return type is Object(java.lang.Object). I want to store this variable value in MySQL database without casting in any other primitive data type. Is there any data type available in MySQL related to Object?

Annisannissa answered 30/4, 2010 at 19:44 Comment(2)
Note that it's generally not a good idea to store a serialized object in a database (other than maybe temporarily) because a change to the class of the object could make the serialized object stored in the database incompatible with it.Digester
That sure would make things easy. Look into ORM or OODBMS. en.wikipedia.org/wiki/Object-relational_mapping and en.wikipedia.org/wiki/Object_databaseTriumvir
A
6

You can use a BLOB to store the raw data, but otherwise no, MySQL does not have a datatype specifically for a java object.

As a side note: You probably shouldn't be storing a raw object into the database, that kind of prevents you from doing any sort of queries on it.

Aldredge answered 30/4, 2010 at 19:46 Comment(0)
S
3

You must serialize the Object anyway, so you could serialize to XML or JSON aswell. A human readable storage form is what I would prefer. Have a look at Xstream for example. A great, threadsafe tool for marshalling/unmarshalling.

I assume of course, that your Object is a Bean/POJO.

Siloa answered 30/4, 2010 at 20:3 Comment(0)
W
2

BLOB could do it. Serialize the object into a byte array and INSERT that as a BLOB.

Waggoner answered 30/4, 2010 at 19:46 Comment(0)
G
0

I'm not sure what do you mean by Object(java.lang.Object). but I think if you tried JSON datatype in MySQL it'll work fine.

create table person (name JSON );
INSERT INTO person VALUES ('{"pid": 101, "name": "name1"}');
INSERT INTO person VALUES ('{"pid": 102, "name": "name2"}');

or

INSERT INTO person VALUES (JSON_OBJECT("pid",103,"name", "name3"));
Galton answered 21/12, 2021 at 14:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.