Dear Sir,
I am very impressed about your object database. It is excellent. I have a small problem. I will be grateful to you if you can solve that.
When I save an object, which contain a reference to a previously saved (and retrieved) object, it also saves the referred object as a new one.
I have a Country class with String country attribute. I also have a City class with String City attribute and Country country attribute. First I saved a Country Object. Then I retrieve the saved object with odb.getObjects and assign it as the country in a new City object. When I save the city object, it create a new country object. That is not the expected behavior. I have copied the codes here and the full net beans project with out your library.
I will be grateful to you if you can help me in this regard.
Thanks
Buddhika
********************* Country Class ******************
package cities;
public class Country {
String country;
public Country(String country) {
this.country = country;
}
public String getCountry() {
return country;
}
@Override
public String toString() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
******************** City CLass **************************
package cities;
public class City {
String city;
Country country;
public City(String city, Country country) {
this.city = city;
this.country = country;
}
@Override
public String toString() {
return city ;
}
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
public City(String city) {
this.city = city;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
******************** Manager Class ***********************
package cities;
import javax.swing.JList;
import org.neodatis.odb.ODB;
import org.neodatis.odb.ODBFactory;
import org.neodatis.odb.Objects;
public class Manager {
static String odbDB = "city.odb";
public static void fillList(Class fillClass, JList jList){
ODB odb = ODBFactory.open(odbDB);
Objects<Object> dbObjects = odb.getObjects(fillClass);
odb.close();
Object[] objects = dbObjects.toArray();
jList.removeAll();
jList.setListData(objects);
}
public static void saveObject(Object object){
ODB odb = ODBFactory.open(odbDB);
odb.store(object);
odb.close();
}
}
******************* frmCity class ********************
package cities;
import com.sun.org.apache.bcel.internal.generic.LSTORE;
import org.neodatis.odb.ODB;
import org.neodatis.odb.ODBFactory;
import org.neodatis.odb.Objects;
import org.neodatis.odb.core.query.IQuery;
import org.neodatis.odb.impl.core.query.criteria.CriteriaQuery;
public class frmCity extends javax.swing.JFrame {
public frmCity() {
initComponents();
}
public static void fillList(){
ODB odb = ODBFactory.open("city.odb");
Objects<Object> objects = odb.getObjects(Country.class);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
txtCity = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
lstCountry = new javax.swing.JList();
Country = new javax.swing.JLabel();
btnAddCity = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
txtCountry = new javax.swing.JTextField();
btnAddCountry = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent evt) {
formWindowOpened(evt);
}
});
jLabel1.setText("City");
lstCountry.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
jScrollPane1.setViewportView(lstCountry);
Country.setText("jLabel2");
btnAddCity.setText("Add City");
btnAddCity.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAddCityActionPerformed(evt);
}
});
jLabel2.setText("Country");
btnAddCountry.setText("Add Country");
btnAddCountry.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAddCountryActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(btnAddCountry)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Country)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txtCountry, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jScrollPane1)
.addComponent(txtCity, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)))
.addComponent(btnAddCity, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(32, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(27, 27, 27)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(txtCity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Country))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnAddCity)
.addGap(43, 43, 43)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtCountry, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnAddCountry)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void btnAddCountryActionPerformed(java.awt.event.ActionEvent evt) {
Country country = new Country(txtCountry.getText());
Manager.saveObject(country);
Manager.fillList(Country.class, lstCountry);
}
private void btnAddCityActionPerformed(java.awt.event.ActionEvent evt) {
City city = new City(txtCity.getText());
city.setCountry((Country) lstCountry.getSelectedValue());
Manager.saveObject(city);
Manager.fillList(Country.class, lstCountry);
}
private void formWindowOpened(java.awt.event.WindowEvent evt) {
Manager.fillList(Country.class, lstCountry);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new frmCity().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel Country;
private javax.swing.JButton btnAddCity;
private javax.swing.JButton btnAddCountry;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JList lstCountry;
private javax.swing.JTextField txtCity;
private javax.swing.JTextField txtCountry;
// End of variables declaration
}
******************************************* Main Class ********************
package cities;
public class Main {
public static void main(String[] args) {
frmCity city = new frmCity();
city.setVisible(true);
}
}
Netbeans Project with out library
CdhCKE I am so grateful for your post.Really thank you! Cool.
Hi Buddhika,
If references are not loaded, NeoDatis will consider they are new objects. So you need to load the references before 'updating them'.
Thanks,
Olivier