DATAGRAPH-213 inheritance with relationship-entities

This commit is contained in:
Michael Hunger
2012-03-23 03:12:32 +01:00
parent d5f50958f3
commit b6cd024b19
10 changed files with 334 additions and 0 deletions

View File

@@ -353,4 +353,12 @@ class Neo4jPersistentPropertyImpl extends AbstractPersistentProperty<Neo4jPersis
if (providedMappingPolicy != null) return providedMappingPolicy;
return getMappingPolicy();
}
public boolean equals(Object other) {
if (other==this) return true;
if (!AbstractPersistentProperty.class.isInstance(other)) return false;
return getField().equals(((AbstractPersistentProperty)other).getField());
}
public int hashCode() {
return getField().hashCode();
}
}

View File

@@ -0,0 +1,72 @@
/**
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.neo4j.inheritance;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.neo4j.inheritance.model.Project;
import org.springframework.data.neo4j.inheritance.model.ProjectDetail;
import org.springframework.data.neo4j.inheritance.model.ProjectDetailRelationship;
import org.springframework.data.neo4j.inheritance.repository.ProjectDetailRepository;
import org.springframework.data.neo4j.inheritance.repository.ProjectRepository;
import org.springframework.data.neo4j.support.Neo4jTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import static org.junit.Assert.assertEquals;
import static org.neo4j.helpers.collection.IteratorUtil.count;
/**
* @author mh
* @since 23.03.12
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class InheritanceTest {
private String validName="projectDetail";
@Autowired ProjectRepository projectRepository;
@Autowired ProjectDetailRepository projectDetailRepository;
@Autowired Neo4jTemplate template;
@Test
@Transactional
public void testCreatedProjectWithInheritance() throws Exception {
Project project = new Project();
project = projectRepository.save(project);
ProjectDetail projectDetail = new ProjectDetail(validName);
projectDetailRepository.save(projectDetail);
ProjectDetailRelationship projectDetailRelationship = new ProjectDetailRelationship(null, project, projectDetail);
template.save(projectDetailRelationship);
project.getProjectDetailRelationships().add(projectDetailRelationship);
project = projectRepository.save(project);
assertEquals(1, count(projectRepository.findAll()));
assertEquals(1, count(projectDetailRepository.findAll()));
ProjectDetail actualProjectDetail = projectDetailRepository.findAll().iterator().next();
assertEquals(validName, actualProjectDetail.getName());
assertEquals(project.getEntityId(), actualProjectDetail.getParentProject().getEntityId());
//all tests above pass without issue -- the test below fails: expected:<1> but was:<0>
Project actualProject = projectRepository.findOne(project.getEntityId());
assertEquals(1, actualProject.getProjectDetailRelationships().size());
}
}

View File

@@ -0,0 +1,34 @@
/**
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.neo4j.inheritance.model;
import org.springframework.data.neo4j.annotation.GraphId;
import org.springframework.data.neo4j.annotation.NodeEntity;
@NodeEntity
public class BasicEntity {
@GraphId
private Long entityId;
/* lots of other common properties here in our real domain */
public BasicEntity() {
}
public Long getEntityId() {
return entityId;
}
}

View File

@@ -0,0 +1,47 @@
/**
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.neo4j.inheritance.model;
import org.springframework.data.neo4j.annotation.EndNode;
import org.springframework.data.neo4j.annotation.Fetch;
import org.springframework.data.neo4j.annotation.GraphId;
import org.springframework.data.neo4j.annotation.RelationshipEntity;
import org.springframework.data.neo4j.annotation.StartNode;
@RelationshipEntity
public class BasicRelationship {
@GraphId
private Long entityId;
@Fetch
@StartNode
private BasicEntity startNode;
@Fetch
@EndNode
private BasicEntity endNode;
/* lots of other common properties here in our real domain */
public BasicRelationship() {
}
public BasicRelationship(Long entityId, BasicEntity startNode, BasicEntity endNode) {
this.entityId = entityId;
this.startNode = startNode;
this.endNode = endNode;
}
}

View File

@@ -0,0 +1,32 @@
/**
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.neo4j.inheritance.model;
import org.springframework.data.neo4j.annotation.Fetch;
import org.springframework.data.neo4j.annotation.RelatedToVia;
import java.util.Set;
public class Project extends BasicEntity {
@Fetch
@RelatedToVia// (type = ProjectDetailRelationship.TYPE)
private Set<ProjectDetailRelationship> projectDetailRelationships;
public Set<ProjectDetailRelationship> getProjectDetailRelationships() {
return projectDetailRelationships;
}
}

View File

@@ -0,0 +1,43 @@
/**
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.neo4j.inheritance.model;
import org.neo4j.graphdb.Direction;
import org.springframework.data.neo4j.annotation.Fetch;
import org.springframework.data.neo4j.annotation.RelatedTo;
public class ProjectDetail extends BasicEntity {
private String name;
@Fetch
@RelatedTo(type = ProjectDetailRelationship.TYPE, direction = Direction.INCOMING)
private Project parentProject;
public ProjectDetail() {
}
public ProjectDetail(String name) {
this.name = name;
}
public String getName() {
return name;
}
public Project getParentProject() {
return parentProject;
}
}

View File

@@ -0,0 +1,34 @@
/**
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.neo4j.inheritance.model;
import org.springframework.data.neo4j.annotation.RelationshipEntity;
import org.springframework.data.neo4j.annotation.RelationshipType;
@RelationshipEntity(type = ProjectDetailRelationship.TYPE)
public class ProjectDetailRelationship extends BasicRelationship {
public static final String TYPE = "PROJECT_HAS_PROJECT_DETAIL";
//@RelationshipType
//private String type = TYPE;
public ProjectDetailRelationship() {
}
public ProjectDetailRelationship(Long entityId, Project project, ProjectDetail projectDetail) {
super(entityId, project, projectDetail);
}
}

View File

@@ -0,0 +1,26 @@
/**
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.neo4j.inheritance.repository;
import org.springframework.data.neo4j.inheritance.model.ProjectDetail;
import org.springframework.data.neo4j.repository.GraphRepository;
/**
* @author mh
* @since 23.03.12
*/
public interface ProjectDetailRepository extends GraphRepository<ProjectDetail> {
}

View File

@@ -0,0 +1,22 @@
/**
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.neo4j.inheritance.repository;
import org.springframework.data.neo4j.inheritance.model.Project;
import org.springframework.data.neo4j.repository.GraphRepository;
public interface ProjectRepository extends GraphRepository<Project> {
}

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/neo4j http://www.springframework.org/schema/data/neo4j/spring-neo4j-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config/>
<neo4j:config graphDatabaseService="graphDatabaseService"/>
<neo4j:repositories base-package="org.springframework.data.neo4j.inheritance.repository"/>
<bean id="graphDatabaseService" class="org.neo4j.test.ImpermanentGraphDatabase"/>
<tx:annotation-driven/>
</beans>