Storing objects problem

#1

I want to save this object to database:

@Entity
public class Device {
    private String name;
    private String type;
    private String ipAddress;
    private Device parent;
    @Id
    private String ID;
    private String ID_HZMO;
    @ElementCollection
    private List<Device> children;
}

An object contains a list of other devices which are also type Device. My root device information are saved properly as well as devices in children list, but there is no information for that children devices and they don't have their children devices. I'm new to this object database so I don't know how to solve this problem.

#2

Please follow the posting instructions and provide a sample program that demonstrates what exactly yo do and where is the problem. It is impossible to diagnose the problem based on the information that you have provided.

By the way, an  @ElementCollection annotation should not be used on a collection of entities, but this is probably not related to the problem that you have described.

ObjectDB Support
#3

Main class:

     EntityManagerFactory emf =
             Persistence.createEntityManagerFactory("$objectdb/db/uredaji.odb");
     EntityManager em = emf.createEntityManager();
        
     em.getTransaction().begin();
    
     for(Device dev : deviceList){
      em.persist(dev);
     }
    
     em.getTransaction().commit();
     em.close();
     emf.close();

Database content is attached.

#4

Your code doesn't include any indication that the child devices are not empty.

Please follow the posting instructions and provide a simple complete runnable test case.

ObjectDB Support
#5
import java.util.*;
import javax.persistence.*;

public class Test {
public static void main(String[] args) {
  DeviceT router = new DeviceT();
  router.setID("1");
  router.setIpAddress("10.18.0.1");
  router.setName("Cis2621_St");
  router.setType("router");
 
  DeviceT switch1 = new DeviceT();
  switch1.setID("2");
  switch1.setIpAddress("10.18.255.7");
  switch1.setName("WAN");
  switch1.setType("switch");
  switch1.setInterfaceMy("Fa0/8");
  switch1.setInterfaceParent("Fa0/1");
  switch1.setParent(router);
 
  DeviceT switch2 = new DeviceT();
  switch2.setID("3");
  switch2.setIpAddress("10.18.0.2");
  switch2.setName("Cis3550_St");
  switch2.setType("switch");
  switch2.setInterfaceMy("Gi0/12");
  switch2.setInterfaceParent("Fa0/0");
  switch2.setParent(router);
 
  router.getChildren().add(switch1);
  router.getChildren().add(switch2);
 
  EntityManagerFactory emf =
             Persistence.createEntityManagerFactory("$objectdb/db/test.odb");
     EntityManager em = emf.createEntityManager();
        
     em.getTransaction().begin();
     em.persist(router);
     em.getTransaction().commit();
       
        em.close();
        emf.close();

}
@Entity
public static class DeviceT {
  private String name;
  private String type;
  private String ipAddress;
  private DeviceT parent;
  @Id
  private String ID;
  private List<DeviceT> children;
  private String interfaceParent;
  private String interfaceMy;
 
  public DeviceT() {
   this.children = new ArrayList<DeviceT>();
   this.parent = null;
  }

  public List<DeviceT> getChildren() {
   return children;
  }

  public void setName(String name) {
   this.name = name;
  }

  public void setType(String type) {
   this.type = type;
  }

  public void setIpAddress(String ipAddress) {
   this.ipAddress = ipAddress;
  }

  public void setParent(DeviceT parent) {
   this.parent = parent;
  }

  public void setID(String iD) {
   ID = iD;
  }

  public void setInterfaceParent(String interfaceParent) {
   this.interfaceParent = interfaceParent;
  }

  public void setInterfaceMy(String interfaceMy) {
   this.interfaceMy = interfaceMy;
  }
}
}
#6

Your code persists only the root object. If you want to cascade the persist operation to referenced objects you have to specify it explicitly. For example, by adding an annotation to a reference:

  @OneToMany(cascade=CascadeType.PERSIST)
  private List<DeviceT> children;

You may also specify cascading persist globally as explained in the manual.

ObjectDB Support

Reply