2

I've got a pretty general question. For my work I have to implement a demonstrator application using MongoDB, Java and JavaFX (and yWorks). So since I - unfortunately - have to work alone on this and there is not a huge amount of know-how in our company, all I did was studying and learning these technologies for myself.

And since we don't want to have a server application there is only MongoDB as a service and the client working on the data. (This is okay, since its only a demonstrator). But im kind of confused.

Implementing pojo classes to store and load from the database and implementing gui model classes with the exact same properties but using "SimpleStringProperty" by JavaFX led to the - in my opinion - weird fact, that i have two semantically identic model classes that I have to implement some kind of Observer/Observable-Pattern to propagate changes from the ViewModels to the POJOS and vice versa.

There must be a more elegant way to do this, right?

Any ideas on this?

Some visualisation:

UML

1 Answers1

2

Analyse Just the analyse, skip to the proposition if you dont want details

The question to duplicate or not class model definition is an architectural choice, but theses 2 solutions are possible and acceptable:

  1. Use your model bean in controller and Screen
  2. Duplicate all your classes with helpers method from/to for the conversions.

Since the duplication is possible but trivial, I will only describe how to use directly models, there are still another 2 solutions:

1.1 Just manually bind your attributes (the simpler but not the more elegant) Create Observable SimpleStringProperty ... on the fly when binding read-only, or use Helpers to add listener on screen observable to call regular setter when a value is modifiable

1.2. Make your data framework use getters/setters and not fields: in most case you can configure it (that's the case for hibernate) So if mongodb can serialize/deserialize objects using method and not field, It will be possible

I suppose here that you use standard javaFX bean with 3 accessor on each attributes: get,set,property.

Proposition Sorry for this big intro, here is a proposed solution using Jackson. First It is aparently possible to use mongodb with Jackson:Efficient POJO mapping to/from Java Mongo DBObject using Jackson

Then here is an example code of javaFX Bean with jackson:

@JsonAutoDetect(fieldVisibility = Visibility.NONE, getterVisibility = Visibility.PUBLIC_ONLY, setterVisibility = Visibility.PUBLIC_ONLY)
public class MyBean {

    private final StringProperty label = new SimpleStringProperty();

    public final StringProperty labelProperty() {
        return this.label;
    }

    public final String getLabel() {
        return this.labelProperty().get();
    }

    public final void setLabel(final String label) {
        this.labelProperty().set(label);
    }
}

You are using morphia, you have to check if this is possible.

Side note: I'm asking a similar question about non duplication of object on the link below (I am in a 3 tier not 2 tier, but still duplication problem), the usual solution for instance is still the duplication: https://softwareengineering.stackexchange.com/questions/367768/how-to-avoid-dto-on-a-client-server-application?noredirect=1#comment804724_367768

pdem
  • 3,880
  • 1
  • 24
  • 38
  • Well the problem is exactly what you said: MongoDB isnt using anything since, this is low level query programming. I forgot to tell you that I'm using morphia to annotate the classproperties and to read/write to the mongodb. So as you said Morphia sees StringProperties as a complex object and I did not found a way yet to customize this serialization/deserialisation process. I guess I am left with the option to just manually bind POJO properties and javaFX-Properties together via listeners and Observable-Pattern... – Simon Bauer Mar 28 '18 at 08:27
  • Okay, it seem taht mondodb can be using with jackson, which is highly tunable :https://stackoverflow.com/questions/15789471/efficient-pojo-mapping-to-from-java-mongo-dbobject-using-jackson, I will update my answer with this – pdem Mar 28 '18 at 08:29
  • @SimonBauer it seem that morphia use getter/setter, you should use the bean of my proposition, without the jackson annotation, so that I could fix my answer. – pdem Mar 28 '18 at 08:42
  • Thanks for you answer. I did try this afaik and maybe it was just the "SimpleListProperty" that did break my Code. Since the getter and setter for these return ObservableLists and those are not serializable. Correct me if im wrong. (I did spend the last two days on refactoring my code to what you can see above in the picture) – Simon Bauer Mar 28 '18 at 09:32
  • @SimonBauer Yes the getter shouldn't return the properties, if this is possible in your case. happy that it works. – pdem Mar 28 '18 at 09:41
  • @pdem what is your opinion about OGM of Hibernate for NoSQL datastore? – Menai Ala Eddine - Aladdin Mar 28 '18 at 16:45
  • @MenaiAlaEddine I don't have an opinon, The mapping is good and clear just as in jackson or Hibernate ORM, but I have no experience on it. – pdem Mar 29 '18 at 08:04