Db4o Learning (7)-inheritance Association

Source: Internet
Author: User
1. Save inheritance
 // C # & JAVA Employee e1 = New Employee (" Michael "," 1234 "," Michael ", 101 ," 10/5/1975 "); Manager M1 = New Manager (" Sue "," 9876 "," Sue ", 102 ," 3/8/1982 "); Casualemployee C1 = New Casualemployee ("Tim "," 5544 "," Tim ", 103 ," 7/6/1986 "); Customer cu1 = New Customer (" Gary "," 408 123 4567 "," Gary@example.net ", New Address (" 1 First Street "," San Jose "," USA "));
// JavaDB. Set (E1); DB. Set (M1); DB. Set (C1); DB. Set (cu1 );

Save them separately.

 

2. query Manager
 
// JavaManager managerexample =NewManager (Null,Null,Null, 0,Null); Objectset Results = dB. Get (managerexample );

 

Query parent class employee:

 
// JavaEmployee employeeexample =NewEmployee (Null,Null,Null, 0,Null); Objectset Results = dB. Get (employeeexample );

Output itself and the inherited class:

Tim (casualemployee) Sue (manager) MICHAEL (employee)

 

Query abstract classes or interfaces to obtain all objects:

 
// JavaObjectset Results = dB. Get (abstractperson.Class);
 
Or
 
// JavaObjectset Results = dB. Get (person.Class);

 

Output:

 
Gary (customer) Tim (casualemployee) Sue (manager) MICHAEL (employee)

 

3. array attributes
 Public   Class Casualemployee Extends Employee { Double [] _ Timerecord;Final   Int Maxtimerecords = 10; Int Numberoftimerecords; Public Casualemployee (string name, string phonenumber, string email, Int Employeenumber, string DOB ){ Super (Name, phonenumber, email, employeenumber, DOB); _ timerecord = New   Double [Maxtimerecords]; numberoftimerecords = 0 ;} Public   Double [] Gettimerecord (){ Return _ Timerecord ;} Public   Void Settimerecord ( Double [] Timerecord) {_ timerecord = timerecord ;} Public   Void Addtimerecord ( Double Newrecord ){ If (Numberoftimerecords <maxtimerecords) {_ timerecord [numberoftimerecords] = newrecord; numberoftimerecords ++ ;}}Public String tostring (){ Return   Super . Getname () +" (Casualemployee) ";}}

 

Save:

// C # & JAVACasualemployee C1 =NewCasualemployee ("Tim","5544","Tim", 101 ,"7/6/1986"); Casualemployee C2 =NewCasualemployee ("Eva","4433","Eva", 102 ,"11/3/1984"); Round (2.5); c1.addtimerecord (4.0); c1.addtimerecord (1.5); Round (2.0); c2.addtimerecord (3.5); c2.addtimerecord (4.0); c2.addtimerecord (5.0 );
 
// JavaDB. Set (C1); DB. Set (C2 );
 
 

Query:

Casualemployee casempexample =NewCasualemployee (Null,Null,Null, 0,Null); Casempexample. settimerecord (New Double[] {4.0}); objectset Results = dB. Get (casempexample );

Or further:

 
// JavaCasualemployee casempexample =NewCasualemployee (Null,Null,Null, 0,Null); Casempexample. settimerecord (New Double[] {2.5, 4.0}); objectset Results = dB. Get (casempexample );

 

NQ query:

 
List <casualemployee> casemps = dB. Query (NewPredicate <casualemployee> (){Public BooleanMatch (casualemployee casemp ){ReturnArrays. binarysearch (casemp. gettimerecord (), 2.5)> = 0 & arrays. binarysearch (casemp. gettimerecord (), 4.0)> = 0 ;}

 

Soda query:

 
// JavaQuery query = dB. Query (); query. constrain (casualemployee.Class); Query valuequery = query. Descend ("_ Timerecord"); Valuequery. constrain (2.5); valuequery. constrain (4.0); objectset Results = query.exe cute ();

 

4. Update and delete array attributes

Update:

// JavaCasualemployee casempexample =NewCasualemployee ("Tim",Null,Null, 0,Null); Objectset Results = dB. get (casempexample); casualemployee Ce = (casualemployee) results. next (); ce. addtimerecord (6.0); ce. gettimerecord () [0] = 7.0; dB. set (CE );
 
 

For a numeric array, it is not necessary to set cascadeonupdate for this to work.

5. Set Operations:
 Public   Class Project {string _ name; string _ costcode; List <employee> _ employees; Public Project (string name, string costcode) {_ name = Name; _ costcode = costcode; _ employees = New Arraylist <employee> ();} Public Project (string name, string costcode, list <employee> employees) {_ name = Name; _ costcode = costcode; _ employees = employees ;} Public   Void Assignemployee (employee newemployee) {_ employees. Add (newemployee ); If (! Newemployee. Projects (). Contains ( This ) Newemployee. assigntoproject ( This );}Public List <employee> employees (){ Return _ Employees ;} Public String tostring (){ Return _ Name +" (Project) ";}}

 

Storage:

 // C # & JAVA Employee e1 = New Employee (" Michael "," 1234 "," Michael ", 101 ," 10/5/1975 "); Employee e2 = New Employee (" Anne "," 5432 "," Anne ", 102 ," 8/5/1980 "); Casualemployee C1 = New Casualemployee (" Tim "," 5544 "," Tim ", 103 ," 7/6/1986 "); Casualemployee C2 = New Casualemployee (" Eva ","4433 "," Eva ", 104 ," 11/3/1984 "); Project p1 = New Project (" Finance System "," P01 "); Project P2 = New Project (" Web Site "," PO2 ");
 
// JavaP1.assignemployee (E1); p1.assignemployee (C1); p2.assignemployee (C2); DB. Set (P1); DB. Set (P2 );

By storing a project, the employee is automatically stored.

 

Query:

When you retrieve a structured object, then by default you also get associated objects. This
Includes objects that are contained in a collection, so when you retrieve a project, you shoshould
Also retrieve the employees, if the activation depth allows it.

If the activation depth permits, You can query the employee ID at the same time.

 

 
Project projectexample =NewProject (Null,"P01"); Objectsset Results = dB. Get (projectexample );While(Results. hasnext () {project proj = (Project) results. Next (); system. Out. println (proj );For(Employee EMPL: proj. Employees) {system. Out. println (EMPL );}}
 
 
 
Query by set:
// JavaEmployee EMP =NewEmployee ("Anne",Null,Null, 0,Null); List <employee> emplist =NewArraylist <employee> (); emplist. Add (EMP); Project projectexample =NewProject (Null,Null, Emplist); objectset Results = dB. Get (projectexample );
 
 

If you only need to access the employee in the same project without other project attributes, you can:

Employee EMP =NewEmployee ("Anne",Null,Null, 0,Null); List <employee> emplist =NewArraylist <employee> (); emplist. Add (EMP); objectset Results = dB. Get (emplist );
 
 
 
NQ version:
List <project> projects = dB. Query (NewPredicate <project> (){Public BooleanMatch (Project proj ){For(Employee EMPL: proj. Employees ()){If(EMPL. getname (). Equals ("Michael")){Return True;}}Return False;}});

 

Soda version:

// JavaQuery query = dB. Query (); query. constrain (project.Class); Query empquery = query. Descend ("_ Employees"); Empquery. constrain (employee.Class); Query namequery = empquery. Descend ("_ Name"); Namequery. constrain ("Michael"); Objectset Results = query.exe cute ();
 
 
6. Update and delete collections
// JavaProject projectexample =NewProject (Null,"P02"); Objectset Results = dB. Get (projectexample );While(Results. hasnext () {project proj = (Project) results. Next ();For(Employee EMPL: proj. Employees () {EMPL. setemail (EMPL. getemail () +". Web");} DB. Set (proj );}
 
// JavaDB. Delete (proj. Employees (); DB. Delete (proj );

If only the project is deleted, and cascade deletion is not set, the employee will remain in the database.

 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.