Skip to content

Commit

Permalink
DATAGRAPH-388 Support for Labels
Browse files Browse the repository at this point in the history
* short-class name for default type alias
* updated repository queries to new cypher syntax
* fix tests
* docs
  • Loading branch information
jexp committed Feb 24, 2014
1 parent f4994af commit 556a14b
Show file tree
Hide file tree
Showing 53 changed files with 289 additions and 153 deletions.
Expand Up @@ -19,6 +19,7 @@

import org.springframework.data.neo4j.annotation.*;
import org.springframework.data.neo4j.fieldaccess.DynamicProperties;
import org.springframework.data.neo4j.support.index.IndexType;

import java.util.Date;

Expand All @@ -45,7 +46,7 @@ public Friendship(Person p1, Person p2, int years) {
@EndNode
private Person p2;

@Indexed
@Indexed(numeric = true,indexType = IndexType.SIMPLE)
private int years;

@RelationshipType
Expand Down
Expand Up @@ -54,11 +54,11 @@ public class Group {
private Iterable<Relationship> peopleRelationships;

@GraphProperty
@Indexed
@Indexed(indexType = IndexType.SIMPLE)
private String name;

@GraphProperty
@Indexed
@Indexed(indexType = IndexType.SIMPLE)
private Boolean admin;

@Query("start n=node({self}) match n-[:persons]->() return count(*)")
Expand All @@ -76,20 +76,20 @@ public class Group {
@Indexed(indexName = SEARCH_GROUPS_INDEX, indexType = IndexType.FULLTEXT)
private String fullTextName;

@Indexed(fieldName = OTHER_NAME_INDEX)
@Indexed(fieldName = OTHER_NAME_INDEX,indexType = IndexType.SIMPLE)
private String otherName;

@Indexed(level=Indexed.Level.GLOBAL)
@Indexed(level=Indexed.Level.GLOBAL,indexType = IndexType.SIMPLE)
private String globalName;

@Indexed(level=Indexed.Level.CLASS)
@Indexed(level=Indexed.Level.CLASS,indexType = IndexType.SIMPLE)
private String classLevelName;

@Indexed(level=Indexed.Level.INSTANCE)
@Indexed(level=Indexed.Level.INSTANCE,indexType = IndexType.SIMPLE)
private String indexLevelName;
private String[] roleNames;

@Indexed(numeric = false)
@Indexed(indexType = IndexType.SIMPLE, numeric = false)
private Byte secret;

public Date getCreationDate() {
Expand Down
Expand Up @@ -20,6 +20,7 @@
import org.neo4j.graphdb.Node;
import org.springframework.data.neo4j.annotation.*;
import org.springframework.data.neo4j.fieldaccess.DynamicProperties;
import org.springframework.data.neo4j.support.index.IndexType;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
Expand All @@ -36,16 +37,16 @@ public class Person {
@GraphId
private Long graphId;

@Indexed(indexName = NAME_INDEX)
@Indexed(indexName = NAME_INDEX,indexType = IndexType.SIMPLE)
@Size(min = 3, max = 20)
private String name;

@Indexed
@Indexed(indexType = IndexType.SIMPLE)
private String nickname;

@Max(100)
@Min(0)
@Indexed
@Indexed(indexType = IndexType.SIMPLE,numeric = true)
private int age;

private Short height;
Expand Down
Expand Up @@ -33,19 +33,19 @@
*/
public interface PersonRepository extends GraphRepository<Person>, NamedIndexRepository<Person> {

@Query("start team=node({p_team}) match (team)-[:persons]->(member) return member")
@Query("MATCH (team:Group)-[:persons]->(member) WHERE id(team) = {p_team} RETURN member")
Iterable<Person> findAllTeamMembers(@Param("p_team") Group team);

@Query("start team=node({p_team}) match (team)-[:persons]->(member) return member.name,member.age")
@Query("MATCH (team:Group)-[:persons]->(member) WHERE id(team) = {p_team} RETURN member.name,member.age")
Iterable<Map<String,Object>> findAllTeamMemberData(@Param("p_team") Group team);

@Query("start person=node({p_person}) match (person)<-[:boss]-(boss) return boss")
@Query("MATCH (person:Person)<-[:boss]-(boss) where id(person) = {p_person} return boss")
Person findBoss(@Param("p_person") Person person);

Group findTeam(@Param("p_person") Person person);

@Query("start team=node({p_team}) match (team)-[:persons]->(member) return member")
@Query("MATCH (team:Group)-[:persons]->(member) WHERE id(team) = {p_team} RETURN member")
Page<Person> findAllTeamMembersPaged(@Param("p_team") Group team, Pageable page);
@Query("start team=node({p_team}) match (team)-[:persons]->(member) return member")
@Query("MATCH (team:Group)-[:persons]->(member) WHERE id(team) = {p_team} RETURN member")
Iterable<Person> findAllTeamMembersSorted(@Param("p_team") Group team, Sort sort);
}
Expand Up @@ -90,7 +90,7 @@ public void testTraverseFieldFromGroupToPeople() {
assertEquals(Collections.singletonList(p),IteratorUtil.asCollection(group.getPeople()));
}

@Ignore("TODO - add back when strict setting working properly again in AbstractMappingContext.getPersistentEntity")
// @Ignore("TODO - add back when strict setting working properly again in AbstractMappingContext.getPersistentEntity")
@Test
@Transactional
public void testTraverseFieldFromGroupToPeopleNodes() {
Expand All @@ -100,7 +100,7 @@ public void testTraverseFieldFromGroupToPeopleNodes() {
assertEquals(Collections.singletonList(getNodeState(p)), IteratorUtil.asCollection(group.getPeopleNodes()));
}

@Ignore("TODO - add back when strict setting working properly again in AbstractMappingContext.getPersistentEntity")
// @Ignore("TODO - add back when strict setting working properly again in AbstractMappingContext.getPersistentEntity")
@Test
@Transactional
public void testTraverseFieldFromGroupToPeopleRelationships() {
Expand Down
Expand Up @@ -10,7 +10,7 @@ public class SubSubThing extends SubThing {
@Indexed(indexType = IndexType.SIMPLE)
String legacyIndexedSubSubThingName;

@Indexed(indexType = IndexType.LABEL, numeric = false)
@Indexed(indexType = IndexType.LABEL)
String schemaIndexedSubSubThingName;

public String getLegacyIndexedSubSubThingName() {
Expand Down
Expand Up @@ -10,7 +10,7 @@ public class SubThing extends Thing {
@Indexed(indexType = IndexType.SIMPLE)
String legacyIndexedSubThingName;

@Indexed(indexType = IndexType.LABEL, numeric = false)
@Indexed(indexType = IndexType.LABEL)
String schemaIndexedSubThingName;

public String getLegacyIndexedSubThingName() {
Expand Down
Expand Up @@ -12,10 +12,10 @@ public class Thing {
@Indexed(indexType = IndexType.SIMPLE)
String legacyIndexedThingName;

@Indexed(indexType = IndexType.LABEL, numeric = false)
@Indexed(indexType = IndexType.LABEL)
String schemaIndexedCommonName;

@Indexed(indexType = IndexType.LABEL, numeric = false)
@Indexed(indexType = IndexType.LABEL)
String schemaIndexedThingName;

public void setName(String name) {
Expand Down
Expand Up @@ -12,7 +12,7 @@ public class TypeAliasedSubSubThing extends TypeAliasedSubThing {
@Indexed(indexType = IndexType.SIMPLE)
String legacyIndexedSubSubThingName;

@Indexed(indexType = IndexType.LABEL, numeric = false)
@Indexed(indexType = IndexType.LABEL)
String schemaIndexedSubSubThingName;

public String getLegacyIndexedSubSubThingName() {
Expand Down
Expand Up @@ -12,7 +12,7 @@ public class TypeAliasedSubThing extends TypeAliasedThing {
@Indexed(indexType = IndexType.SIMPLE)
String legacyIndexedSubThingName;

@Indexed(indexType = IndexType.LABEL, numeric = false)
@Indexed(indexType = IndexType.LABEL)
String schemaIndexedSubThingName;

public String getLegacyIndexedSubThingName() {
Expand Down
Expand Up @@ -15,10 +15,10 @@ public class TypeAliasedThing {
@Indexed(indexType = IndexType.SIMPLE)
String legacyIndexedThingName;

@Indexed(indexType = IndexType.LABEL, numeric = false)
@Indexed(indexType = IndexType.LABEL)
String schemaIndexedCommonName;

@Indexed(indexType = IndexType.LABEL, numeric = false)
@Indexed(indexType = IndexType.LABEL)
String schemaIndexedThingName;

public void setName(String name) {
Expand Down
Expand Up @@ -25,6 +25,7 @@
import org.springframework.data.neo4j.mapping.Neo4jPersistentEntity;
import org.springframework.data.neo4j.support.Neo4jTemplate;
import org.springframework.data.neo4j.support.mapping.Neo4jMappingContext;
import org.springframework.data.neo4j.support.mapping.Neo4jPersistentEntityImpl;
import org.springframework.data.neo4j.support.node.NodeEntityStateFactory;

import javax.persistence.EntityManagerFactory;
Expand All @@ -47,7 +48,8 @@ public CrossStoreNodeEntityStateFactory(Neo4jMappingContext neo4jMappingContext,
public EntityState<Node> getEntityState(final Object entity, boolean detachable, Neo4jTemplate template) {
final Class<?> entityType = entity.getClass();
if (isPartial(entityType)) {
final Neo4jPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(entityType);
Neo4jPersistentEntity<Object> persistentEntity = getPersistentEntity(entityType);
if (persistentEntity==null) return null;
final DelegatingFieldAccessorFactory fieldAccessorFactory = crossStoreFactory.provideFactoryFor(template);
@SuppressWarnings("unchecked") final CrossStoreNodeEntityState<NodeBacked> partialNodeEntityState =
new CrossStoreNodeEntityState<NodeBacked>(null, (NodeBacked)entity, (Class<? extends NodeBacked>) entityType,
Expand Down
Expand Up @@ -21,6 +21,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.neo4j.partial.model.User;
import org.springframework.data.neo4j.support.Neo4jTemplate;
import org.springframework.data.neo4j.support.node.Neo4jHelper;
import org.springframework.test.context.CleanContextCacheTestExecutionListener;
Expand Down
Expand Up @@ -22,10 +22,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.neo4j.partial.model.User;
import org.springframework.data.neo4j.support.Neo4jTemplate;
import org.springframework.data.neo4j.support.node.Neo4jHelper;

import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.CleanContextCacheTestExecutionListener;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
Expand Down
Expand Up @@ -5,8 +5,8 @@

<persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>org.springframework.data.neo4j.partial.User</class>
<class>org.springframework.data.neo4j.partial.Restaurant</class>
<class>org.springframework.data.neo4j.partial.model.User</class>
<class>org.springframework.data.neo4j.partial.model.Restaurant</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
Expand Down
Expand Up @@ -10,7 +10,7 @@
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/neo4j http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd">


<neo4j:config graphDatabaseService="graphDatabaseService" entityManagerFactory="entityManagerFactory"/>
<neo4j:config graphDatabaseService="graphDatabaseService" entityManagerFactory="entityManagerFactory" base-package="org.springframework.data.neo4j.partial.model"/>
<bean id="graphDatabaseService" class="org.neo4j.test.ImpermanentGraphDatabase"
destroy-method="shutdown" scope="singleton">
</bean>
Expand Down
Expand Up @@ -81,7 +81,15 @@
<constructor-arg name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<bean id="mappingContext" class="org.springframework.data.neo4j.support.mapping.Neo4jMappingContext"/>
<bean id="mappingContext" class="org.springframework.data.neo4j.support.mapping.Neo4jMappingContext">
<property name="initialEntitySet">
<set>
<value>org.springframework.data.neo4j.partial.model.Restaurant</value>
<value>org.springframework.data.neo4j.partial.model.Recommendation</value>
<value>org.springframework.data.neo4j.partial.model.User</value>
</set>
</property>
</bean>


<bean id="relationshipEntityStateFactory" class="org.springframework.data.neo4j.support.relationship.RelationshipEntityStateFactory">
Expand Down
17 changes: 16 additions & 1 deletion spring-data-neo4j-rest/pom.xml
Expand Up @@ -67,7 +67,22 @@
<version>${neo4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>

<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-lucene3</artifactId>
<version>${querydsl}</version>
<optional>true</optional>
<exclusions>
<exclusion>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-kernel</artifactId>
<version>${neo4j.version}</version>
Expand Down
Expand Up @@ -8,7 +8,7 @@
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<context:annotation-config/>
<neo4j:config storeDirectory="target/test-db"/>
<neo4j:config storeDirectory="target/test-db" base-package="org.springframework.data.neo4j.aspects"/>

<neo4j:repositories base-package="org.springframework.data.neo4j.aspects"/>

Expand Down
Expand Up @@ -35,13 +35,13 @@
*/
String indexName() default "";

org.springframework.data.neo4j.support.index.IndexType indexType() default org.springframework.data.neo4j.support.index.IndexType.SIMPLE;
org.springframework.data.neo4j.support.index.IndexType indexType() default org.springframework.data.neo4j.support.index.IndexType.LABEL;

String fieldName() default "";

boolean unique() default false;

boolean numeric() default true;
boolean numeric() default false;

// FQN is a fix for javac compiler bug http://bugs.sun.com/view_bug.do?bug_id=6512707
org.springframework.data.neo4j.annotation.Indexed.Level level() default org.springframework.data.neo4j.annotation.Indexed.Level.CLASS;
Expand Down
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.data.neo4j.config;

import org.springframework.aop.target.LazyInitTargetSource;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
Expand All @@ -25,6 +26,7 @@
import org.springframework.data.config.IsNewAwareAuditingHandlerBeanDefinitionParser;
import org.springframework.data.mapping.context.MappingContextIsNewStrategyFactory;
import org.springframework.data.neo4j.lifecycle.AuditingEventListener;
import org.springframework.data.support.IsNewStrategyFactory;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

Expand All @@ -51,7 +53,7 @@ protected boolean shouldGenerateId() {
* @see org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser#doParse(org.w3c.dom.Element, org.springframework.beans.factory.xml.ParserContext, org.springframework.beans.factory.support.BeanDefinitionBuilder)
*/
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder auditingListenerBuilder) {

BeanDefinitionRegistry registry = parserContext.getRegistry();

Expand All @@ -66,10 +68,16 @@ protected void doParse(Element element, ParserContext parserContext, BeanDefinit
createIsNewStrategyFactoryBeanDefinition(templateName, parserContext, element);
}

BeanDefinitionParser parser = new IsNewAwareAuditingHandlerBeanDefinitionParser(IS_NEW_STRATEGY_FACTORY);
BeanDefinition handlerBeanDefinition = parser.parse(element, parserContext);
BeanDefinitionParser isNewStrategyParser = new IsNewAwareAuditingHandlerBeanDefinitionParser(IS_NEW_STRATEGY_FACTORY);
BeanDefinition isNewStrategyBeanDefinition = isNewStrategyParser.parse(element, parserContext);

builder.addConstructorArgValue(handlerBeanDefinition);
// TODO
// BeanDefinitionBuilder lazyInitTS = BeanDefinitionBuilder.genericBeanDefinition(LazyInitTargetSource.class);
// lazyInitTS.addPropertyValue("targetBeanName", isNewStrategyBeanDefinition);
// lazyInitTS.addPropertyValue("targetClass", IsNewStrategyFactory.class.getName());
//
// auditingListenerBuilder.addConstructorArgValue(lazyInitTS);
auditingListenerBuilder.addConstructorArgValue(isNewStrategyBeanDefinition);
}

static String resolveMappingContextRef(Element element) {
Expand Down
Expand Up @@ -209,7 +209,7 @@ public Neo4jMappingContext neo4jMappingContext() throws Exception {

@Bean
protected EntityAlias entityAlias() {
return new ClassNameAlias();
return new EntityAlias();
}

@Bean
Expand Down Expand Up @@ -254,10 +254,10 @@ public GraphDatabase graphDatabase() {
return new DelegatingGraphDatabase(graphDatabaseService);
}

@Bean
public ConfigurationCheck configurationCheck() throws Exception {
return new ConfigurationCheck(neo4jTemplate(),neo4jTransactionManager());
}
// @Bean
// public ConfigurationCheck configurationCheck() throws Exception {
// return new ConfigurationCheck(neo4jTemplate(),neo4jTransactionManager());
// }

@Bean
public PersistenceExceptionTranslator persistenceExceptionTranslator() {
Expand Down

0 comments on commit 556a14b

Please sign in to comment.