Syncleus / Ferma

An ORM / OGM for the TinkerPop graph stack.
http://syncleus.com/Ferma
Apache License 2.0
136 stars 26 forks source link

Not persisting to JanusGraph #54

Open oodles-arvind-das opened 5 years ago

oodles-arvind-das commented 5 years ago

I am trying to use ferma ORM for my project . Current problem is, I am not able to persist my entities to JanusGraph DB. Here is my code Required : Entity Class


public abstract class Person extends AbstractVertexFrame {
    private String id;
    private String name;
    private int age;
    private String mobile;

    @Property("age")
    public abstract int getAge();

    @Property("age")
    public abstract void setAge(int age) ;

    @Property("mobile")
    public abstract String getMobile();

    @Property("mobile")
    public abstract void setMobile(String mobile) ;

    public String getId() {
        return id;
    }

    @Property("name")
    public abstract String getName() ;

    @Property("name")
    public abstract  void setName(String name);

    public String toString(){
        return this.name+" has Id"+this.getId();
    }
}

This is how I create/initialize my DelegatedFramedGraph bean:

@Bean
    FramedGraph framedGraph(JanusGraph graph,GremlinBeanPostProcessor gremlinBeanPostProcessor){
        return new DelegatingFramedGraph(graph,true,gremlinBeanPostProcessor.getEntityClasses());
    }

JanusGraph is initialized :

@Bean
    JanusGraph graph(JanusGremlinGraphFactory janusGremlinGraphFactory){
        JanusGraph janusGraph = null;
        janusGraph = janusGremlinGraphFactory.openGraph();
        return janusGraph;
    }

    @Bean
    JanusGremlinGraphFactory janusGremlinGraphFactory(){
        JanusGremlinGraphFactory jggf;
        jggf = new JanusGremlinGraphFactory();
        jggf.setConfiguration(getConfiguration());
        return jggf;
    }

When I try to persist it:

 @Autowired
    private FramedGraph framedGraph;

public String savePerson(String name,int age,String mobile){
        Person person =  framedGraph.addFramedVertex(Person.class);
        person.setName(name);
        person.setAge(age);
        person.setMobile(mobile);
        System.out.println(person.getName()+"and person id is"+person.getId());
        return person.toString();
    }

Nothing is stored to JanusGraph, not sure where is the problem.

Can somebody help me ?

oodles-arvind-das commented 5 years ago

My bad, I made a basic mistake , I had to perform: framedGraph.tx().commit();

This should be the part of FramedGraph on every add and other operations. The only thing left is , I did not get the Id property back.

syncleus-bot commented 5 years ago

Glad you fixed this

On Fri, Oct 19, 2018 at 5:50 PM oodles-arvind-das notifications@github.com wrote:

My bad, I made a basic mistake , I had to perform: framedGraph.tx().commit();

This should be the part of FramedGraph on every add and other operations. The only thing left is , I did not get the Id property back.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/Syncleus/Ferma/issues/54#issuecomment-431409547, or mute the thread https://github.com/notifications/unsubscribe-auth/AWNYb3whPTM7aoKtDUwTJTSNaPJV6Kkdks5umfTHgaJpZM4Xwchq .

oodles-arvind-das commented 5 years ago

Thanks , can you guide me how can I get the currently saved object's Id back ? Commit is a void operation and it does not return anything and also transaction is closed. Getting id property which is assigned by the graph is highly needed.

freemo commented 5 years ago

Sure, but that will need to wait till im back home behind my desktop.

On Fri, Oct 19, 2018 at 6:33 PM oodles-arvind-das notifications@github.com wrote:

Thanks , can you guide me how can I get the currently saved object's Id back ? Commit is a void operation and it does not return anything and also transaction is closed. Getting id property which is assigned by the graph is highly needed.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/Syncleus/Ferma/issues/54#issuecomment-431422547, or mute the thread https://github.com/notifications/unsubscribe-auth/AC5JAh3pEdGykRhjsT2B5dLzZ4T0nFKsks5umf7jgaJpZM4Xwchq .

oodles-arvind-das commented 5 years ago

Got it, had to tinker a little, I should not be running too fast in asking questions without having tried out everything, but got it fixed.

person.getElement().id()

Since usage of Spring Framework wraps you in chains and you always expect that id returned from DB is automatically mapped to the id property of the entity, that took some time for me to come out of that world.

oodles-arvind-das commented 5 years ago

Though this thread is closed now, I saw a behavior here, If I have my non abstract class and then ofcourse non abstract properties, ferma persists the node but not the properties. Is that the expected behavior ?

Following class is persisted without properties


public class Post extends AbstractVertexFrame {

    private String id;
    private String title;
    private String description;
    private LocalDate dateCreated;

    public String getTitle(){
        return this.title;
    }

    public void setTitle(String title){
        this.title = title;
    }

    public String getId(){
        return id;
    }

    public void setId(String id){
        this.id = id;
    }

    public  String getDescription() {
        return this.description;
    }

    public  void setDescription(String description){
        this.description = description;
    }

    public  LocalDate getDateCreated() {
        return this.dateCreated;
    }

    public  void setDateCreated(LocalDate localDate){
        this.dateCreated = localDate;
    }

    public String toString(){
        return this.getTitle()+" has Id"+this.getId();
    }
}```
oodles-arvind-das commented 5 years ago

Though this thread is closed now, I saw a behavior here, If I have my non abstract class and then ofcourse non abstract properties, ferma persists the node but not the properties. Is that the expected behavior ?

Following class is persisted without properties

public class Post extends AbstractVertexFrame {

    private String id;
    private String title;
    private String description;
    private LocalDate dateCreated;

    public String getTitle(){
        return this.title;
    }

    public void setTitle(String title){
        this.title = title;
    }

    public String getId(){
        return id;
    }

    public void setId(String id){
        this.id = id;
    }

    public  String getDescription() {
        return this.description;
    }

    public  void setDescription(String description){
        this.description = description;
    }

    public  LocalDate getDateCreated() {
        return this.dateCreated;
    }

    public  void setDateCreated(LocalDate localDate){
        this.dateCreated = localDate;
    }

    public String toString(){
        return this.getTitle()+" has Id"+this.getId();
    }
}```

From the documentation this works


public class Post extends AbstractVertexFrame {

    private String id;
    private String title;
    private String description;
    private LocalDate dateCreated;

    public String getTitle(){
       return getProperty("title");
    }

    public void setTitle(String title){
        setProperty("title",title);
    }

    public String getId(){
        return id;
    }

    public void setId(String id){
        this.id = id;
    }

    public  String getDescription() {
        return getProperty("description");
    }

    public  void setDescription(String description){
       setProperty("description",description);
    }

    public  LocalDate getDateCreated() {
        return getProperty("dateCreated");
    }

    public  void setDateCreated(LocalDate localDate){
        setProperty("dateCreated",localDate);
    }

    public String toString(){
        return this.getTitle()+" has Id"+this.getId();
    }
}

But why there is so much boilerplate code for getProperty and setProperty, and Post class instance is not able to have its own states, everything is driven by behavior (methods) and states are attained by the @Property annotation .

Please help me understanding that .

stozk commented 5 years ago

I just encountered the same problem, the @property annotation didn't work, using setters/getters with setProperty works.

Edit: It seems that the @Property annotions (or annotations in general?) only work when the graphelement class is defined as an abstract class.