updating Java Objects(Longblob) in mysql databasel problem
i tried searching at google using this key phrase "updating java objects in mysql" but i cant find a problem like what i have,
i have 2 classes, 1 that is serializable(Thesaurus class) and 1 that runs a simple program and saves the object of that class(Thesaurus) in my database.
heres the structure of the Thesaurus class
Code :
public class Thesaurus implements Serializable {
private static final long serialVersionUID = 3781342618719432265L;
private Map<String, Set> thesaurus;
public Thesaurus() {
thesaurus = new TreeMap<String, Set>();
}
public void addThesaurusEntry(String word) {
this.thesaurus.put(word, new TreeSet<String>());
}
public void removeEntry(String word) {
thesaurus.remove(word);
}
public Set getSynonyms(String word) {
return thesaurus.get(word);
}
public Map getThesaurus() {
return thesaurus;
}
}
here is the simple program (only the main method, i removed some of it to lessen the code, ill post the rest if necessary and requested for clearer analization)
Code :
public static void main(String[] args) {
Database db = new Database();
db.connect();
Thesaurus thes = db.getThesaurus();
try {
System.out.println(thes.getSynonyms("Word"));
}
catch (NullPointerException e) {
System.err.println("Word Entry Does not Exist");
e.printStackTrace();
}
Thesaurus newEntry = new Thesaurus();
newEntry.put(thes.getThesaurus());
newEntry.addThesaurusEntry("Word");
newEntry.addSynonym("Word", "Salita");
db.updateThesaurus(newEntry, thes);
}
and here is the update method of my class that udpate's the object in the database
Code :
public void updateThesaurus(Object newValue, Object oldValue) {
sql = "update thesaurus set wordEntry = ? where wordEntry = ?";
try {
PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql);
ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
ObjectOutputStream oos1 = new ObjectOutputStream(baos1);
ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
ObjectOutputStream oos2 = new ObjectOutputStream(baos2);
oos1.writeObject(newValue);
oos1.flush();
oos1.close();
baos1.close();
oos2.writeObject(oldValue);
oos2.flush();
oos2.close();
baos2.close();
ps.setObject(1, baos1.toByteArray());
ps.setObject(2, baos2.toByteArray());
ps.executeUpdate();
}
catch (SQLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
i tried to update the values in the database by 2 objects, the newValue and the oldValue which represents Thesaurus objects, but i dont see any changes, when i try to retrieve those.
i follow the flow step-by-step, but i cant modify the current object inside the database, am i missing something here? thanks in advance
id like to add how i expect with the flow of the program
this will retrieve a Thesaurus object from the database, which is always only one
Code :
Thesaurus thes = db.getThesaurus();
i need to create a new Thesaurus object because it will be used to compare which is the old and new value that will be used to update the database
and the second line newEntry.put retrieves the current map
Code :
Thesaurus newEntry = new Thesaurus();
newEntry.put(thes.getThesaurus()); // it gives a copy of the current map to this new thesaurus object
Code :
// i can add more keys on the map with this statement, in every run of the program, i can update the database
// newEntry.addThesaurusEntry("Different word");
// but i cant add more different objects(String synonym) on the Set object of a certain key(word), database object doesnt update
newEntry.addSynonym("Word", "Salita");
db.updateThesaurus(newEntry, thes);
what i want is, to modify the synonyms(Set) of any word on the Map(i.e i can add more and more, each run of the program)
it seems like the Set object of my Thesaurus' Map instance cannot update its value in the database.. please help, and im sorry for a long statement, im trying to state my problem as possible as i can, please help.. when i learned this interfaces, i found out that this is very handy, and i need to resolve this issue, because i might be using this in some of my projects in the future.
Re: updating Java Objects(Longblob) in mysql databasel problem
i dont think its about serialization, i was able to break down the problem and see that the TreeSet is the thing i need to deal with, i tried to minimize the Thesaurus class into this with only one collection object, a single Set/TreeSet object
Code :
public class Thesaurus implements Serializable {
private static final long serialVersionUID = 3781342618719432265L;
private Set<String> setObject;
public Thesaurus() {
setObject = new TreeSet<String>();
}
public void addValue(String s) {
setObject.add(s);
}
public Set getSet() {
return setObject;
}
public void setSet(Set<String> set) {
setObject = set;
}
}
and the sample main program with this
Code :
public static void main(String[] args) {
Database db = new Database();
db.connect();
Thesaurus thes = db.getThesaurus();
System.out.println(thes.getSet());
Thesaurus newEntry = new Thesaurus();
newEntry.setSet(thes.getSet());
newEntry.addValue("Value 1"); // the next run will try to add "Value 2"
db.updateThesaurus(newEntry, thes);
}
but no matter how i try, it doesnt modify the exisiting TreeSet object, what is the thing im missing?, by the way i just learned this from a book, and i added some practice with saving the things on a database.
Re: updating Java Objects(Longblob) in mysql databasel problem
Quote:
Originally Posted by
chronoz13
i dont think its about serialization
I'm not so sure. Your SQL UPDATE depends on the serialized bytes of your object being identical from run to run - that seems like it should be extremely fragile and unlikely to work very well for retrieval. How many rows does your PreparedStatement.executeUpdate tell you have been updated?
Re: updating Java Objects(Longblob) in mysql databasel problem
Why serialize the object into the database? A relational database is perfectly suited to hold thesaurus data, and scale much better than saving data with a serialized object (which as Sean mentioned, can be not only fragile but not very scalable or efficient)
Re: updating Java Objects(Longblob) in mysql databasel problem
Quote:
Why serialize the object into the database?
oh thank you so much for enlightening me, atleast i know that i have some problem in designing my own program structure, hmmmm so how can i still use these things i learned in my design of thesaurus?
- create a relational database
- retrieve those data and put it all in a map?(sequence it, inside my java program?)
- should i let my database be relational? and let my Java program handle all the collection?
is this a good way?, this is what i was thinking this last night, but im not sure or ill say confused,
and by the way, i was wrong blaming the TreeSet class, im still digging down why my serialized class is not working, i found that even i change those collection interfaces, whether i use List or ArrayList or Set or TreeSet alone in the Thesaurus class they still dont update themselves every retrieval,
ill also leave a vague question and ill search a little bit while waiting for some answers, when and where should i do serializiation in programs? thank you so much again, that was a little relief
Re: updating Java Objects(Longblob) in mysql databasel problem
i manage to solve the problem, this is my first time using this Collection interfaces, i didnt pay attention on my assignment statements,
and i just cant add values on the Map or Set objects, i need to "REMAP", the whole entire map to a new one, before i can add
this was my mistake, im just passing a reference copy from old to new
and i resovle it this way
Code :
newMap.putAll(currentMap);
and for the Map's Set value,
i need to re-iterate and retrieve and set the current Set collection in the Map's value
Code :
public void setMap(Map<String, Set> currentMap) {
Map<String, Set> tempMap = new TreeMap<String, Set>();
for (Map.Entry<String, Set> entry : currentMap.entrySet()) {
Set<String> tempSet = new TreeSet<String>();
tempSet.addAll(currentMap.get(entry.getKey()));
tempMap.put(entry.getKey(), tempSet);
}
this.thesaurusMap.putAll(tempMap);
}
, i manage to resolve the problem, but again, thanks for advising me for a better program structure, i just wanted to solve this one. thanks so much