Android RealmList<Integer> and RealmList<String> - java

I'm working with Realm to create my android app's ORM with Realm. The problem is that when I try to create an object like this:
public class Airport extends RealmObject {
private int Id;
private String Name;
private String Code;
private RealmList<Integer> destinations;
}
androidStudio tells me that I can't have the RealmList with type Integer; and for String type either.
I've been looking a few similar questions, but the best approach is to declare an object like:
public class MyRealmInteger extends RealmObject {
private int destination;
}
so this way I can rewrite my class as follows:
public class Airport extends RealmObject {
private int Id;
private String Name;
private String Code;
private RealmList<MyRealmInteger> destinations;
}
but I think it's a very complicated. There isn't any other easier solution?

but I think it's a very complicated. There isn't any other easier solution?
No there is not. Not yet at least. They're "working on it":
This feature is among a handfull of top features that we hope to take on next. We will however give the current 1.0 a bit of peace to ensure stability before we push a lot of new features.
You can check this issue for updates on it https://github.com/realm/realm-java/issues/575

Please take a look at this answer https://stackoverflow.com/a/46576569/3479489 realm will add support for primitives in the version 4.0.0

Related

Does Intellij IDEA have methods (getters, setters or builder) prediction option?

Intellij IDEA is really great but I was really frustrated by the way IDEA predicts what method to call next when building some object with setters or builder.
For example we have simple object(powered by lombok):
#Value
#Builder
public class Benefit {
private final long sourceUserId;
private final BigDecimal baseValue;
private final String multiplier;
private final int bonusId;
private final long bonusTtl;
private final String benefitId;
}
But IDEA doesn't really care that I've already called it.
It would be great to see this method on the bottom of prediction list.
Is it obvious only for me that next one in the prediction should be baseValue?
I didn't find anything about it in google and here so maybe I've just missed some basic stuff?
Is there some way to change this prediction strategy?

Realm java advanced query instead of for-each for search inside list ids

i have a model like this
public class WorkDB extends RealmObject {
private String address;
private RealmList<ReportDB> reports;
getters setters
}
public class ReportDB extends RealmObject implements Serializable{
private int idReport;
private String nameReport;
private RealmList<ReplieGroupDB> repliesGroup;
private RealmList<QuestionGroupDB> questionGroups;
getters setters
}
public class QuestionGroupDB extends RealmObject {
private int idQuestionGroupInReplie;
private String nameQuestionGroupInReplie;
private RealmList<QuestionDB> questions;
private int times;
}
How can i do a query like
realm.search(WorkDb.class).where(id,20).and(ReportDb.class).where(idReport,1).and(QuestionGroupDB.class).where(idQuestionGroupInReplie,2);
To avoid do a query to get WorkDB and do a for each in reports to find by id, and then, other do a other for each in questionGroupsDB to find any by id..
If you want to retrieve a list QuestionDB objects there is unfortunately no other way around it than building a manual query like the one you describe. We have a concept called BackLinks on our TODO (https://github.com/realm/realm-java/issues/607) which would make it possible to do the sort of query you are looking for, but currently you will have to do it manually. Sorry.

Serialization problems: objects that depend on each other and ArrayLists

I have to serialize a project and it's the first time I use serialization. After getting informed about it, I thought of two possible problems: my classes have atributes which type is another different class that has atributes which type is the first class (explained poorly, but can see in the code) and the fact that I use ArrayLists (which I've read can't be serialized). So I decided to try with a very simplified version of the project:
A group, this containts an ArrayList of Person:
public class Group implements Serializable {
private static final long serialVersionUID = 1L;
private Person leader;
private List<Person> members;
private int number;
public Group(Person leader, int number) {
this.leader = leader;
this.number = number;
this.members = new ArrayList<Person>();
this.members.add(leader);
}
public void addMember(Person p) {
this.members.add(p);
}
public int getNumber() {
return number;
}
}
A person, this contains an ArrayList of Groups:
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
private List<Group> groups;
private String name;
public Person(String name) {
this.name = name;
this.groups = new ArrayList<Group>();
}
public Group createGroup(int number) {
Group g = new Group(this, number);
this.groups.add(g);
return g;
}
public void joinGroup(Group g) {
this.groups.add(g);
g.addMember(this);
}
}
And a main method which creates a few groups and people and uses writeObject() to write them into a file, plus another main that uses readObject() to get the objects back (it only uses readObject() and prints them).
I didn't expect this to work for the reasons mentioned above, but it worked perfectly, so I tried to serialize my main project (way more complex) but it didn't work (huge stack trace, simply saying "User", which is the equivalent to person, is not serializable).
Is there any reason for this or any major flaw that I should take into account?
I apologize for not including the two main methods I use, as well as none of the stacktrace or the main project, but I didn't want to make this question extremely long.
my classes have atributes which type is another different class that has atributes which type is the first class (explained poorly, but can see in the code)
Incomprehensible. Both Java and Serialization handle circular dependencies, if that's what you're talking about.
and the fact that I use ArrayLists (which I've read can't be serialized)
Wrong.
simply saying "User", which is the equivalent to person, is not serializable
So User doesn't implement Serializable.
Is there any reason for this or any major flaw that I should take into account?
Make User implement Serializable.Same for any other class that gives you the same message.
You need to read the Object Serialization Specification and the relevant Javadoc, and stop relying on arbitrary Internet rubbish.

which java collection object is best when persisting entities using jpa

I am writing an application that parses n number of records each with m number of fields. It is similar to parsing header information out of ip packets. One difference is that the records I'm working with have arrays of fields for optional user comments. My question is, what is the best java.util.collection object for persisting a collection of fields? A set is probably best for what I'm trying to do, because of multiple 'comment' fields that must be unique, but are there any performance or serialization concerns when choosing one collection type over another? Below is psedo-code for what I'm trying to do:
#Entity
public class MyRecord implements Serializable{
#Id
private int id;
private String filename;
#OneToMany(targetEntity=MyField.class, mappedBy="filename")
private Set<MyField> field;
}
public class MyField implements Serializable{
private int id;
private String filename;
private String filePath;
private String fieldName;
private String fieldType;
private String fieldValue;
}
You can take control by yourself for ultimate serialization performance, have a look at the Externalizable interface ( http://docs.oracle.com/javase/7/docs/api/java/io/Externalizable.html )
You can use Collection to be prepared for future changes. Maybe someday MyField is allowed to be not unique.

GWT Editor Framework, how to implement an effective reuse of the editor

In many cases I have the same panel which edits a set of properties that are common to different DTOs.
So I want to have this panels defined only once and reuse so I came up with the following implementation for one of them:
public class IdentificationPanel<M> extends Panel implements Editor<M> {
BusinessUnitField businessUnit;
OperationCodeField operationCode;
OperationNumber operationNumber;
...........
}
So I will use the IdentificationPanel with different DTOs depending on the Models I will need to edit.
For example I have:
public class ExampleTrans01 extends ModelDTO {
private ExampleTrans01Header header;
.......
}
public class ExampleTrans02 extends ModelDTO {
private ExampleTrans02Header header;
.....
}
public class ExampleTrans01Header extends ModelDTO {
private Integer businessUnit;
private String operationCode;
private Long operationNumber;
.......
// Setters & Getters
}
public class ExampleTrans02Header extends ModelDTO {
private Integer businessUnit;
private String operationCode;
private Long operationNumber;
.......
// Setters & Getters
}
So in the implementation of the editors for the 2 classes I need to edit I will have:
public class ExampleTrans01Editor extends Panel implements Editor<ExampleTrans01> {
#Path("header")
IdentificationPanel<ExampleTrans01Header> identification;
.......
}
public class ExampleTrans02Editor extends Panel implements Editor<ExampleTrans02> {
#Path("header")
IdentificationPanel<ExampleTrans02Header> identification;
........
}
When I try to compile this, GWT complains because it said that there is no constructor for the IdentificationPanel_businessUnit_Context class with the class ExampleTrans02Header as parent when it was generating the Delegate.
I know I maybe get rid of the problem by extending IdentificationPanel, like:
public class ExampleTrans01Identification extends IdentificationPanel<ExampleTrans01Header> {
// Nothing interesting to do here
}
public class ExampleTrans02Identification extends IdentificationPanel<ExampleTrans02Header> {
// Nothing interesting to do here
}
And then use this classes instead the parameterized, but that solution seems to be a little nasty because those classes will not have any other use.
So the question is, are there any other way of implement this case? I was wondering that this should be a very common use case, but I couldn't found much info about it.
On a side note I may said that I new to Editor Framework so maybe I was interpreting something wrong, I will appreciated if you could put me in the right direction.
Regards,
Daniel
This is a known issue and there's no other known workaround.
http://code.google.com/p/google-web-toolkit/issues/detail?id=6016

Categories

Resources