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.
Related
I've been dealing with DAOClasses and I've come accross a serious problem which forces me to reorganize my entire project. However I have no clue on how to solve this problem.
Suppose I have 3 Entities:
public class User {
private int id;
private int cfu;
private String name;
private String surname;
}
public class Project {
private int ID;
private String info;
private Doc doc;
}
public class Doc {
private int id;
private String name;
private String surname;
private ArrayList<Project> projects;
}
Each of this classes has its own DAO and its own table (regarding Doc the table does not keep reference of Projects IDs);
Among their various methods, DAOClasses uses these 2 methods:
read allows to create a element of type T given the ID
readAll allows to recover all elements of type T given a filter
When I call readmethod for DocDAO it is necessary to verify whether Project table contains Doc elements, if so create those elements thanks to ProjectDAO readAll and add them to its ArrayList. But also ProjectDAO need to read a Doc element, which again needs ProjectDAO, causing a circular dependency (therefore a StackOverflow):
DocDAO.read(int id) --calls--> ProjectDAO.readAll(Doc d) --calls-> DocDAO.read(int id) --calls--> ProjectDAO.readAll(Doc d)...
readAll allows to recover all elements of type T given a filter
create allows to create an element of type T in database
how can I decouple these functionality without changing base classes (but just DAO)?
I would skip giving more code since it is very long.
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
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.
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.
I have the following BO which is already there in the system
public class userBO
{
private String userId;
private String password;
private String firstName;
private String midName;
private String lastName;
private String userType;
private String userDepartment;
private String userAuthority;
//There are some more fields
//getter and setter
}
Now I want to built a dropdown in which I will display Name (firstName + lastName) and will use userId as value. So for that I will make a list of object.
So my question is should I use the existing userBO class or should I create new class something like below
public class userDropDwonBO
{
private String userId;
private String firstName;
private String lastName;
//getter and setter
}
I want to know the answer from Good Architect point of view and also performance point of view, Will there be any better performance if I user new userDropDownBO
userDropDownBO object will definitely use less memory than the above class.
It is because all your members are private intance variable, everytime a constructor is invoked, a set of all private variables will be created on stack and will be initialized to their default values so will consume more memory and initialization time.
But it solely depend on your requirement:
If other fields are required other than these three fields go for the userBO class.
If other fields are unnecessary but no of objects to be created are small in number, go for userBO.
If other fields are unnecessary but no of objects to be created are very large in number, go for userDropDownBO.
Its a personal opinion and rest is your choice.
If you are going to create a new class beside the existing one named UserBO just for the sake of binding it to the JComboBox, that will definitely be a waste of memory and waste of time as well and also you will need to provide an additional logic to map your original object of type UserBO to the the object of type UserDropDownBO.
I would say that your approach maybe applicable in case the BO itself is so complex in dealing with, so that you need to create a separate model to be used in the drop down box.