NomaD
NomaD

Reputation: 111

Java Binding with Intersystems Cache

I'm new to Cache and I'm trying to use java binding and I have some problems with it.

1) I want to add some entry to the database using code

              Database dbconnection = CacheDatabase.getDatabase (url, username, password);

              Patient patient = new Patient(dbconnection);
              patient.setFIO("Antonov Kirill Vladimirovich");

              Diary diary = new Diary(dbconnection);
              diary.setData("Very bad.");
              diary.setDate(new java.sql.Date(2015,11,12));
              diary.setStatus("Unsatisfied");

              ListOfObjects obj = new ListOfObjects(dbconnection);
              obj.add(diary);
              patient.listOfDiariesSetObject(new Oid(obj));

              dbconnection.saveAllObjects();

This code causes

Exception in thread "main" java.lang.ClassCastException: com.intersys.classes.ListOfObjects cannot be cast to com.intersys.jdbc.SysList
    at com.intersys.jdbc.SysListProxy.getBinaryData(SysListProxy.java:516)
    at com.intersys.objects.Oid.getData(Oid.java:101)
    at com.intersys.cache.Dataholder.<init>(Dataholder.java:378)
    at smda.Patient.listOfDiariesSetObject(Patient.java:1565)
    at etu.wollen.cache.DBConnector.main(DBConnector.java:34)

How should I convert ListOfObjects to Oid correctly?

2) How should I remove some entries from database? I have found only save methods in com.intersys.objects.Database using \Dev\java\samples\doc

3) Most of classes, such as com.intersys.objects.Database, com.intersys.objects.CacheException, ... are deprecated. But official docbook still uses theese classes. Should I use deprecated classes?

Patient

    Class smda.Patient Extends %Persistent
{

Property FIO As %String;

Property RegNumber As %String;

Property MedCardNumber As %String;

Property listOfDiaries As list Of Diary;

Property listOfEpisodes As list Of Episode;

Storage Default
{
<Data name="PatientDefaultData">
<Value name="1">
<Value>%%CLASSNAME</Value>
</Value>
<Value name="2">
<Value>FIO</Value>
</Value>
<Value name="3">
<Value>RegNumber</Value>
</Value>
<Value name="4">
<Value>MedCardNumber</Value>
</Value>
<Value name="5">
<Value>listOfDiaries</Value>
</Value>
<Value name="6">
<Value>listOfEpisodes</Value>
</Value>
</Data>
<DataLocation>^smda.PatientD</DataLocation>
<DefaultData>PatientDefaultData</DefaultData>
<IdLocation>^smda.PatientD</IdLocation>
<IndexLocation>^smda.PatientI</IndexLocation>
<StreamLocation>^smda.PatientS</StreamLocation>
<Type>%Library.CacheStorage</Type>
}

Projection PatientJava As %Projection.Java(ROOTDIR = "C:\Projects\Cache\Java");

}

Diary 

    Class smda.Diary Extends %Persistent
{

Property Data As %Text(MAXLEN = 1000);

Property Status As %String;

Property Date As %Date;

Storage Default
{
<Data name="DiaryDefaultData">
<Value name="1">
<Value>%%CLASSNAME</Value>
</Value>
<Value name="2">
<Value>Data</Value>
</Value>
<Value name="3">
<Value>Status</Value>
</Value>
<Value name="4">
<Value>Date</Value>
</Value>
</Data>
<DataLocation>^smda.DiaryD</DataLocation>
<DefaultData>DiaryDefaultData</DefaultData>
<IdLocation>^smda.DiaryD</IdLocation>
<IndexLocation>^smda.DiaryI</IndexLocation>
<StreamLocation>^smda.DiaryS</StreamLocation>
<Type>%Library.CacheStorage</Type>
}

}

Upvotes: 1

Views: 387

Answers (2)

DAiMor
DAiMor

Reputation: 3205

1) Just get the value, for this property, it should contain a value with type java.util.List. And then you can manipulate with this list, such as add your value.
instead of

          ListOfObjects obj = new ListOfObjects(dbconnection);
          obj.add(diary);
          patient.listOfDiariesSetObject(new Oid(obj));

should be

        List diaries = patient.getlistOfDiaries();
        diaries.add(diary);

2) Every Caché class has own method for create (%New), open (%OpenId) and for delete (%DeleteId) by id objects of this classes. And, projected classes, has their own such static methods. Here in documentation you can see some details about projection classes. So, you can call such code, for delete object with Id=1:

Patient.sys_DeleteId(dbconnection, 1);

3) Not sure, but I think it is because of Caché eXTreme, which should replace cachedb.jar. You can read more about Java Caché eXTreme here.

Upvotes: 0

DdP
DdP

Reputation: 438

I think the answer to your first question is in your code. I think you can modify this section:

ListOfObjects obj = new ListOfObjects(dbconnection);
obj.add(diary);
patient.listOfDiariesSetObject(new Oid(obj));

to be as follows:

patient.listOfDiariesSetObject(new Oid(diary));

Your initial code is creating a ListOfObjects instance that you are then inserting into the listOfDiaries property, rather than inserting your Diary instance into the list.

Upvotes: 1

Related Questions