db4o Developer Community

db4o open source object database, native to Java and .NET
Welcome to db4o Developer Community Sign in | Join
in Search
More Search Options

Use of objectCanDelete() call in server.

Last post 08-26-2008, 02:28 PM by douglascrp. 15 replies.
Page 1 of 2 (16 items)   1 2 Next >
Sort Posts: Previous Next
  •  06-21-2005, 08:02 AM 10718

    Use of objectCanDelete() call in server.

    This message was imported from the former db4o newsgroup.
    
    Hi.
    
    I am making a testing program with two classes: Course and Student.  I am 
    using a server-client connection. I have cascadeOnUpdate(true) in the two 
    classes.
    
    I am using a vector in each class to make the relationship. In the class 
    Student the method addCourse() allow to register a student to a course and 
    this method  use Course.addStudent() to keep synchronize de relationship.
    
    Every thing is working nice until here.
    
    I was trying to use objectCanDelete() on the class student to delete the 
    object student from the vector students in the class Course to keep 
    synchronize the relationship. But, for some reason I could not find the way 
    to make it to work.
    
    I will like you help to understand how to use objectCanDelete() call in 
    server in this little test program.
    
    Thank you.
    
    ----------------------------
    Classes
    
    ----------------------------
    package dataoo7;
    import java.util.*;
    import com.db4o.*;
    
    public class Course {
     private String name;
     private Vector students;
    
     public Course (String name) {
      this.name=name;
      this.students = new Vector();
     }
     public void setName(String name) {
      this.name = name;
     }
     public String getName() {
      return this.name;
     }
     public void addStudent(Student newstudent) {
      this.students.add( (Student)newstudent );
     }
     public Vector getStudents(){
      return this.students;
     }
     public int numStudents(){
      return this.students.size();
     }
    
     public String toString() {
      return this.name+"("+this.students.size()+")";
     }
    }
    
    ----------------------------------
    package dataoo7;
    import java.util.*;
    import com.db4o.*;
    
    public class Student {
     private String name;
     private Vector courses;
    
     public Student (String name) {
      this.name=name;
      this.courses = new Vector();
     }
     public void setName(String name) {
      this.name = name;
     }
     public String getName() {
      return this.name;
     }
     public void addCourse(Course newcourse) {
      newcourse.addStudent( this );
      this.courses.add( (Course)newcourse );
     }
     public Vector getCourses(){
      return this.courses;
     }
     public int numCourses(){
      return this.courses.size();
     }
    
     public String toString() {
      return this.name+"("+this.courses.size()+")";
     }
    }
  •  06-28-2005, 04:18 AM 12228 in reply to 10718

    Re: Use of objectCanDelete() call in server.

    This message was imported from the former db4o newsgroup.
    
    Does any body have a little help for this question?
    
    
    
    In other way: Can some body indicate what is the "question profile" 
    necessary to follow to receive answers?
    
    
    
    Thank you.
    
    
    
    
    "Italo Osorio"  wrote in message 
    news:d99a68$b7s$1@p15090641.pureserver.de...
    > Hi.
    >
    > I am making a testing program with two classes: Course and Student.  I am 
    > using a server-client connection. I have cascadeOnUpdate(true) in the two 
    > classes.
    >
    > I am using a vector in each class to make the relationship. In the class 
    > Student the method addCourse() allow to register a student to a course and 
    > this method  use Course.addStudent() to keep synchronize de relationship.
    >
    > Every thing is working nice until here.
    >
    > I was trying to use objectCanDelete() on the class student to delete the 
    > object student from the vector students in the class Course to keep 
    > synchronize the relationship. But, for some reason I could not find the 
    > way to make it to work.
    >
    > I will like you help to understand how to use objectCanDelete() call in 
    > server in this little test program.
    >
    > Thank you.
    >
    > ----------------------------
    > Classes
    >
    > ----------------------------
    > package dataoo7;
    > import java.util.*;
    > import com.db4o.*;
    >
    > public class Course {
    > private String name;
    > private Vector students;
    >
    > public Course (String name) {
    >  this.name=name;
    >  this.students = new Vector();
    > }
    > public void setName(String name) {
    >  this.name = name;
    > }
    > public String getName() {
    >  return this.name;
    > }
    > public void addStudent(Student newstudent) {
    >  this.students.add( (Student)newstudent );
    > }
    > public Vector getStudents(){
    >  return this.students;
    > }
    > public int numStudents(){
    >  return this.students.size();
    > }
    >
    > public String toString() {
    >  return this.name+"("+this.students.size()+")";
    > }
    > }
    >
    > ----------------------------------
    > package dataoo7;
    > import java.util.*;
    > import com.db4o.*;
    >
    > public class Student {
    > private String name;
    > private Vector courses;
    >
    > public Student (String name) {
    >  this.name=name;
    >  this.courses = new Vector();
    > }
    > public void setName(String name) {
    >  this.name = name;
    > }
    > public String getName() {
    >  return this.name;
    > }
    > public void addCourse(Course newcourse) {
    >  newcourse.addStudent( this );
    >  this.courses.add( (Course)newcourse );
    > }
    > public Vector getCourses(){
    >  return this.courses;
    > }
    > public int numCourses(){
    >  return this.courses.size();
    > }
    >
    > public String toString() {
    >  return this.name+"("+this.courses.size()+")";
    > }
    > }
    >
  •  06-28-2005, 05:35 AM 12913 in reply to 12228

    Re: Use of objectCanDelete() call in server.

    This message was imported from the former db4o newsgroup.
    
    Italo Osorio :
    > Does any body have a little help for this question?
    > 
    > In other way: Can some body indicate what is the "question profile" 
    > necessary to follow to receive answers?
    
    Well, you've said you've tried to use the objectCanDelete() callback, 
    but in your example there is no objectCanDelete() to be found. There is 
    no a method to delete the student at all in your code. So, provide the 
    code which is not working, rather than the code which is working, then 
    somebody might guess what is exactly wrong with your code. Do something 
    like a "unit test", which should work, but apparently don't.
  •  06-28-2005, 07:22 AM 14511 in reply to 12913

    Re: Use of objectCanDelete() call in server.

    This message was imported from the former db4o newsgroup.
    
    Hi Artem, thank you for you answer.
    
    I didn't include my code because I was expecting that some body can propose 
    one to see a "clear" approach to the problem with out my "inexperience code". 
    Sorry for that.
    
    Well my request is that:
    
    > I will like you help to understand how to use objectCanDelete() call in 
    > server in this little test program.
    
    I was trying to do something like this:
    
    Add to the Student Class this method.
    
    public void delCourse(Course course){
     this.courses.remove(course);
    }
    
    
    Add to the Course Class this method.
    
    public void objectCanDelete(ObjectContainer container    ) {
     Student tmpStudent;
     for( int i=0; i < this.students.size(); i++ ) {
      tmpStudent = (Student) this.students.get(i);
      tmpStudent.delCourse(this);
      container.set(tmpStudent);
    }
    return true;
    }
    
    
    This code works with our errors but it does not delete a course from the 
    vector in student class when I deleted a course.
    
    Thank you for you help.
    
    Italo.
    
    
    "Artem Gr"  wrote in message 
    news:d9rg7a$k1u$1@p15090641.pureserver.de...
    > Italo Osorio :
    >> Does any body have a little help for this question?
    >>
    >> In other way: Can some body indicate what is the "question profile" 
    >> necessary to follow to receive answers?
    >
    > Well, you've said you've tried to use the objectCanDelete() callback, but 
    > in your example there is no objectCanDelete() to be found. There is no a 
    > method to delete the student at all in your code. So, provide the code 
    > which is not working, rather than the code which is working, then somebody 
    > might guess what is exactly wrong with your code. Do something like a 
    > "unit test", which should work, but apparently don't.
  •  06-28-2005, 08:29 AM 15178 in reply to 14511

    Re: Use of objectCanDelete() call in server.

    This message was imported from the former db4o newsgroup.
    
    I suspect the following problem: since in client-server environment 
    'objectCanDelete' callback is executed on the server, the 'course' is 
    probably removed on the server, but remains in the 'student' instances 
    on the client.
    
    You can check if this is the case by using ExtObjectContainer#refresh 
    method on the student.
    
    The simplest solution to this problem, i suppose, is to move the 
    'course' deletion into the 'delCourse' method. Consider, also, using 
    Db4oList instead of Vector.
    http://db4objects.com/community/ontheroad/apidocumentation/com/db4o/types/Db4oList.html
    
    > 
    > I was trying to do something like this:
    > 
    > Add to the Student Class this method.
    > 
    > public void delCourse(Course course){
    >  this.courses.remove(course);
    > }
    > 
    > 
    > Add to the Course Class this method.
    > 
    > public void objectCanDelete(ObjectContainer container    ) {
    >  Student tmpStudent;
    >  for( int i=0; i < this.students.size(); i++ ) {
    >   tmpStudent = (Student) this.students.get(i);
    >   tmpStudent.delCourse(this);
    >   container.set(tmpStudent);
    > }
    > return true;
    > }
    > 
    > 
    > This code works with our errors but it does not delete a course from the 
    > vector in student class when I deleted a course.
    >
  •  06-29-2005, 12:58 AM 14512 in reply to 12913

    Re: Use of objectCanDelete() call in server.

    This message was imported from the former db4o newsgroup.
    
    Hi Artem. Thank you for you help.
    
    > I suspect the following problem: since in client-server environment 
    > 'objectCanDelete' callback is executed on the server, the 'course' is 
    > probably removed on the server, but remains in the 'student' instances on 
    > the client.
    
    The documentation about objectCanDelete said: In a client/server setup this 
    callback method will be executed on the server.
    The question is how to access other objects when is "executed on the server" 
    by the Call back methods.
    
    In Student Class I add this method.
    
     public void delCourse(int pos, ObjectContainer container){
      Course course = (Course) this.courses.get(pos);
      course.delStudent(this);
      container.set(course);
      this.courses.removeElementAt( pos );
     }
    
    I use this method to delete a Student Course by passing POS with the vector 
    position of the course which I want to delete and the object container. The 
    goal is to delete a student object from the vector "students" in the Course 
    class. The idea it is not to delete the Course, only to "unregister" a 
    student from this course. This method works fine and maybe is because it is 
    "executed on the client".
    
    
    The other point is that if I whan to deleted a Course, before that, I need 
    to inform the students objects they are not any more register in the Course. 
    I know what student are register in the class by the Course.students Vector. 
    I need to go to each of this students and Delete from thir Course vector the 
    Course with I wanto to delete. It need to be done by objectCanDelete. I can 
    do that in the JSP but it is not the optimal, need to be done in the object.
    
    
    > You can check if this is the case by using ExtObjectContainer#refresh 
    > method on the student.
    
    I am using JSP. I have one jsp which show the courses and students. Another 
    JSP delete the objects.
    
    This are my classes:
    
    
    //----------------------------------------------
    package dataoo7;
    
    import java.util.*;
    import com.db4o.*;
    
    
    public class Course {
     private String id;
     private String name;
     private Vector students;
    
    
     public Course (String id,String name) {
      this.id=id;
      this.name=name;
      this.students = new Vector();
     }
    
     public String getId() {
      return this.id;
     }
    
     public void setName(String name) {
      this.name = name;
     }
    
     public String getName() {
      return this.name;
     }
    
     public void addStudent(Student newstudent) {
      this.students.add( (Student)newstudent );
     }
    
     public Vector getStudents(){
      return this.students;
     }
    
     public int numStudents(){
      return this.students.size();
     }
    
    
     public String toString() {
      return this.name+"("+this.students.size()+")";
     }
    
     public void delStudent(Student student){
      this.students.remove( student );
     }
    
    }
    
    //---------------------------------------------------
    package dataoo7;
    
    import java.util.*;
    import com.db4o.*;
    
    
    public class Student {
     private String id;
     private String name;
     private Vector courses;
    
    
     public Student (String id, String name) {
      this.id=id;
      this.name=name;
      this.courses = new Vector();
     }
    
     public String getId() {
      return this.id;
     }
    
     public void setName(String name) {
      this.name = name;
     }
    
     public String getName() {
      return this.name;
     }
    
     public void addCourse(Course newcourse) {
      newcourse.addStudent( this );
      this.courses.add( (Course)newcourse );
     }
    
     public Vector getCourses(){
      return this.courses;
     }
    
     public int numCourses(){
      return this.courses.size();
     }
    
    
     public String toString() {
      return this.name+"("+this.courses.size()+")";
     }
    
     public void delCourse(int pos, ObjectContainer container){
      Course course = (Course) this.courses.get(pos);
      course.delStudent(this);
      container.set(course);
      this.courses.removeElementAt( pos );
     }
    
     public boolean objectCanDelete(ObjectContainer container) {
      Course course;
      for( int i=0; i < this.courses.size(); i++ ) {
       course = (Course) this.courses.get(i);
       course.delStudent(this);
       container.set(course);
      }
      return true;
     }
    
    }
  •  06-29-2005, 02:29 AM 15179 in reply to 14512

    Re: Use of objectCanDelete() call in server.

    This message was imported from the former db4o newsgroup.
    
    > The other point is that if I whan to deleted a Course, before that, I need 
    > to inform the students objects they are not any more register in the Course. 
    > I know what student are register in the class by the Course.students Vector. 
    > I need to go to each of this students and Delete from thir Course vector the 
    > Course with I wanto to delete. It need to be done by objectCanDelete. I can 
    > do that in the JSP but it is not the optimal, need to be done in the object.
    
    If i am not mistaken, you can't, currently, access client objects from 
    the server, only the other way around: you can access server objects 
    from the client. Therefore, you /can/ change something from 
    objectCanDelete, but then you should use ExtObjectContainer#refresh or 
    ExtObjectContainer#peekPersisted or ExtObjectContainer#purge(Object) to 
    refresh client data from the server.
  •  06-29-2005, 03:13 AM 14513 in reply to 12913

    Re: Use of objectCanDelete() call in server.

    This message was imported from the former db4o newsgroup.
    
    Hi.. I am really confuse.
    
    What is the use of "objectCanDelete" if I can not access the information 
    form the object which I am deleting? What is the use of "objectCanDelete" if 
    I can't access data from other classes to keep data integrity?
    
    Sorry if I am missing something in my understanding. I have no problem with 
    the other Callbacks.
    
    Could you give me a code example of how to use a "objectCanDelete"?
    
    Thaks you.
    
    
    
    "Artem Gr"  wrote in message 
    news:d9tplr$1mi$1@p15090641.pureserver.de...
    >
    >> The other point is that if I whan to deleted a Course, before that, I 
    >> need to inform the students objects they are not any more register in the 
    >> Course. I know what student are register in the class by the 
    >> Course.students Vector. I need to go to each of this students and Delete 
    >> from thir Course vector the Course with I wanto to delete. It need to be 
    >> done by objectCanDelete. I can do that in the JSP but it is not the 
    >> optimal, need to be done in the object.
    >
    > If i am not mistaken, you can't, currently, access client objects from the 
    > server, only the other way around: you can access server objects from the 
    > client. Therefore, you /can/ change something from objectCanDelete, but 
    > then you should use ExtObjectContainer#refresh or 
    > ExtObjectContainer#peekPersisted or ExtObjectContainer#purge(Object) to 
    > refresh client data from the server.
  •  06-29-2005, 05:21 AM 15180 in reply to 14513

    Re: Use of objectCanDelete() call in server.

    This message was imported from the former db4o newsgroup.
    
    Obviously, objectCanDelete callback should be used to prevent some 
    objects from being deleted.
    
    > Hi.. I am really confuse.
    > 
    > What is the use of "objectCanDelete" if I can not access the information 
    > form the object which I am deleting? What is the use of "objectCanDelete" if 
    > I can't access data from other classes to keep data integrity?
    > 
    > Sorry if I am missing something in my understanding. I have no problem with 
    > the other Callbacks.
    > 
    > Could you give me a code example of how to use a "objectCanDelete"?
  •  11-21-2005, 03:14 PM 17089 in reply to 15180

    Re: Use of objectCanDelete() call in server.

    hi <br />
    I just don't get it. the use for objectCanDelete is quite simple to understand but, the use of objectOnDelere I don't get:
    <p />
    I would like to delete some children of a Object which is about to be deleted.
    <p />
    The problem here is that when I enter the objectOnDelete function all child objects are NULL, and I can't make my custom deletion logic
    <p />
    Ie. <br />
    The Object which is about to be deleted has a List of child objects. When the object is deleted I want to run through the list and delete each element, or more complex determin if the element has to be deleted or the reference just should be deleted..



    <p />
    Can anybody explain how to handle more complex treestructure deletion...

    <p />
    TIA<br />
    /Kasper
  •  11-22-2005, 02:12 PM 17132 in reply to 17089

    Re: Use of objectCanDelete() call in server.

     f_kasper wrote:
    I just don't get it. the use for objectCanDelete is quite simple to understand but, the use of objectOnDelere I don't get:

    I would like to delete some children of a Object which is about to be deleted.

    The problem here is that when I enter the objectOnDelete function all child objects are NULL, and I can't make my custom deletion logic

    Did you try activating the object in the objectOnDelete() callback method?

     

  •  11-22-2005, 02:45 PM 17136 in reply to 17132

    Re: Use of objectCanDelete() call in server.

    Hi
    Thanks for the suggestion. I now have tried to activate the object in following code, without any change. All child objects (objectList) are still NULL.

    (All child members are present when retriving the object from db4o but in the following 2 functions all child objects are null)

    I just don't get it. All examples in the forum and the documentation suggest that this is just the way to go. I am fairly new to db4o so there might be some obvious setting i am missing?


    public class MyObject{
    private List objectList; //this is my childObject
    private String name;

    public boolean objectCanDelete(ObjectContainer container){
    container.activate(this,2);
    System.out.println("canDelete object: "+objectList);
    for(Object object:objectList)
    container.delete(object);
    return true;
    }

    public void objectOnDelete(ObjectContainer container){
    // called after an object was deleted.
    container.activate(this,2);
    System.out.println("onDelete: "+objectList);
    for(Object object:objectList)
    container.delete(object);
    }
    ...
    }


    When run the program returns:
    canDelete object: null
    onDelete: null
  •  11-23-2005, 08:36 AM 17157 in reply to 17136

    Re: Use of objectCanDelete() call in server.

    I'm not sure, but there might be a safety check in there which would prevent activate(this) from working in a callback. Carl or Patrick would know for sure. From what I see, that looks correct. It's possible that the bug is elsewhere in your code. Maybe the list isn't getting stored in the first place?
    Eric Falsken ยป db4objects
  •  07-28-2007, 03:23 PM 40063 in reply to 17157

    Re: Use of objectCanDelete() call in server.

    The Problem still exists, here is a simple TestCase:

    import com.db4o.*;
    import com.db4o.ext.*;
    
    import java.io.*;
    
    public class Test {
        long i;
        String s;
    
    
        public static void main(String[] args) throws IOException, InterruptedException {
            new File("test.yap").delete();
            ObjectServer server = Db4o.openServer("test.yap", 0);
            ExtClient c = (ExtClient) server.openClient();
            Test t = new Test();
            t.i= 1;
            t.s= "foo";
            c.set(t);
            c.commit();
    
            c.delete(t);
            c.commit();
    
            while (!c.close());
            while (!server.close());
        }
    
        public void objectOnDelete(ObjectContainer container) throws IOException {
            System.out.println(this.i + ", "+ this.s);
            // bug:
            //  expected    1, foo
            //  result:     0, null
        }
    }
    even with activation in the objectOnDelete method the Object values are not set up correctly.
    cheers
    Andreas 
    Filed under: ,
  •  07-28-2007, 04:57 PM 40064 in reply to 40063

    Re: Use of objectCanDelete() call in server.

    I supplied a patch which fixes this bug:

    http://tracker.db4o.com/browse/COR-741 

Page 1 of 2 (16 items)   1 2 Next >
View as RSS news feed in XML