-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Neo4j POC goes wrong
PostPosted: Thu Mar 13, 2014 11:06 am 
Newbie

Joined: Mon Jan 02, 2006 11:11 am
Posts: 2
Recently I’ve discovered Hibernate OGM Neo4j. I do realize that it’s still in a beta phase, but I can’t get things to properly work. Please check out http://github.com/TimmyStorms/hibernate-ogm-neo4j-example. It contains classes OgmTest and OgmWithJTATest, both giving different yet wrong results. If you try it out, you’ll have to start from an empty database for both classes. Also make sure that the persistence.xml file is correct (uncomment the second part for OgmWithJTATest).

This example persists a Person node and two related Item nodes. Retrieving them does not work as expected. Also the database contents differ largely in both examples.

_________________
http://javaprogress.wordpress.com


Top
 Profile  
 
 Post subject: Re: Neo4j POC goes wrong
PostPosted: Fri Mar 14, 2014 8:11 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
Hi timmy,

the current version of hibernate-ogm-neo4j has a bug and it won't create the expected model when saving data on the db.
I've fixed this problem in the pull request that will upgrade the supported Neo4j version to 2.0.1: https://github.com/hibernate/hibernate-ogm/pull/305
I've tested the example with both implementations though

That said, I had to change some things in your test case to make it work properly:
1) Remove from the persistence.xml the declaration of the JtaPlatform:
Code:
<property name="hibernate.transaction.jta.platform" value="org.hibernate.engine.transaction.jta.platform.internal.JBossStandAloneJtaPlatform" />

Neo4j uses its own JtaPlatform (added when you select neo4 has provider) and it won't work if you replace it: org.hibernate.ogm.datastore.neo4j.transaction.impl.Neo4jJtaPlatform
We are working to find a better solution.

2) Retrieving the transaction manager using com.arjuna.ats.jta.TransactionManager.transactionManager() does not seem to work as expected with neo4j, I'm investigating about this but, as a workaround, you can use this method:
Code:
public static TransactionManager extractTransactionManager(EntityManagerFactory factory) {
        SessionFactoryImplementor sessionFactory = factory.unwrap( SessionFactoryImplementor.class );
        return sessionFactory.getServiceRegistry().getService( JtaPlatform.class ).retrieveTransactionManager();
}


3) In the example a bidirectional relationship is created doing:
Code:
Person person = new Person("John", "Doe");
em.persist(person);
final Item iphone = new Item("iPhone");
em.persist(iphone);


but because this is a bidirectional association, the good practice (as far as I know) is to set the value in both direction and declare the owner of the relationship, so you should also set
Code:
public static void main(final String[] args) throws Exception {
...
Set<Person> persons = new HashSet()<>;
persons.add( person );
iphone.setPersons( persons );
...
}


public class Item {
    ...
    @ManyToMany(mappedBy = "items")
    private Set<Person> persons;
    ...
}


Applying these changes and using the updated hibernate-ogm-neo4j should give the expected model.

In conclusion:
As you can see the integration still requires some work and refinement,
thank you very much for your feedback it's been much appreciated.

Don't hesitate to ask questions if you have some more doubts or issues.

Cheers,
Davide


Top
 Profile  
 
 Post subject: Re: Neo4j POC goes wrong
PostPosted: Fri Mar 14, 2014 8:55 am 
Newbie

Joined: Mon Jan 02, 2006 11:11 am
Posts: 2
Thanks a lot for the clarifications Davide! I've ran both tests again though, and although both tests are consistent now, it doesn't seem to be entirely correct. On a first run, the tests return the following:

Code:
Persons:1
Items:2
0
1 value_sequence_ogm - 3, id_sequence_ogm - hibernate_sequencessequence_namePerson,
2 value_sequence_ogm - 5, id_sequence_ogm - hibernate_sequencessequence_nameItem,
3 id - 1, lastName - Doe, _table - Person, firstName - John,
4 id - 1, name - iPhone, _table - Item,
5 id - 2, name - iPod, _table - Item,
6 persons_id - 1, items_id - 1,
7 persons_id - 1, items_id - 2,
8 persons_id - 1, items_id - 2,
9 persons_id - 1, items_id - 1,


This means there is one person and two items found. This is correct, but I'm wondering what the difference between node 6 and 9, and node 7 and 8 is? They seem to be identical, so is this a bug? Maybe you can explain a bit what node sturcture is used by OGM?

Anyway, on a second run, this is the output:

Code:
Persons:1
Items:2
0
1 value_sequence_ogm - 3, id_sequence_ogm - hibernate_sequencessequence_namePerson,
2 value_sequence_ogm - 5, id_sequence_ogm - hibernate_sequencessequence_nameItem,
3 id - 1, lastName - Doe, _table - Person, firstName - John,
4 id - 1, name - iPhone, _table - Item,
5 id - 2, name - iPod, _table - Item,
6 persons_id - 1, items_id - 1,
7 persons_id - 1, items_id - 2,
8 persons_id - 1, items_id - 2,
9 persons_id - 1, items_id - 1,
10 id - 2, lastName - Doe, _table - Person, firstName - John,
11 id - 3, name - iPhone, _table - Item,
12 id - 4, name - iPod, _table - Item,
13 persons_id - 2, items_id - 4,
14 persons_id - 2, items_id - 3,
15 persons_id - 2, items_id - 3,
16 persons_id - 2, items_id - 4,


If you look at the Neo4j contents, it appears that there are now 2 persons and 4 items stored. This is correct. But the queries still return Persons:1 and Items:2. Again, is this a bug or am I simply missing something?

_________________
http://javaprogress.wordpress.com


Top
 Profile  
 
 Post subject: Re: Neo4j POC goes wrong
PostPosted: Fri Mar 14, 2014 10:05 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
The additional nodes (6, 7, 8, 9) are the related to the bugs I've mentioned before in the previous reply.
If we take the unidirectional relationship between John and the iPhone we would expect the following (using Cypher query lnguage):
Code:
(n:{id:1, _table: "Persons", firstName: "John", lastName: "Doe"}) - [r:items, ...] -> (i: {id: 1, _table: "Item", name: "iPhone"})


instead, OGM is creating the following:
Code:
(n:{id: 1, _table: "Person", firstName: "John", lastName: "Doe"}) - [r:items,...] -> ({persons_id: 1, items_id: 1})
(i: {id: 1, _table: "Item", name: "iPhone"})


As you can see there is an additional node and the relationship is not connected to the expected node but only to an intermediate anonymous node.
In OGM a bidirectional relationship is mapped with two unidirectional relationships and therefore is creating two additional nodes.
In your example there are 2 bidirectional relationship, therefore 4 additional nodes.
The dialect will work anyway but this is a bug and it should be fixed as soon as the pull request I've sent is merged in master.

As for the second question, at the moment JPQL/HQL queries are not executed on Neo4j directly, they are actually converted in Lucene queries and executed on the indexes. I believe what is happening is that you have deleted the indexes (or they are in memory) and not the db.

The convertion of HQL queries into Cypher queries is one of the things I'm working at the moment.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.