added default-getId to aspect, added first part of object-primitive handling, extended test for that

This commit is contained in:
Michael Hunger
2010-08-15 18:45:21 +02:00
parent 0e6fd5db03
commit 0af2ead565
5 changed files with 29 additions and 10 deletions

View File

@@ -1,5 +1,6 @@
<project-modules id="moduleCoreId" project-version="2.0">
<?xml version="1.0" encoding="UTF-8"?>
<project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="datastore-graph">
<wb-resource deploy-path="/" source-path="src/main/java"/>
</wb-module>
</project-modules>
</project-modules>

View File

@@ -97,6 +97,10 @@ public aspect Neo4jNodeBacking extends AbstractTypeAnnotatingMixinFields<GraphEn
return underlyingNode;
}
public long NodeBacked.getId() {
return underlyingNode.getId();
}
//-------------------------------------------------------------------------
// Equals and hashCode for Neo4j entities.
@@ -140,7 +144,8 @@ public aspect Neo4jNodeBacking extends AbstractTypeAnnotatingMixinFields<GraphEn
FieldSignature fieldSignature=(FieldSignature) thisJoinPoint.getSignature();
Field f = fieldSignature.getField();
// TODO fix arrays
if (f.getType().isPrimitive() || f.getType().equals(String.class)) {
Class fieldType=f.getType();
if (isPropertyType(fieldType)) {
String propName = getNeo4jPropertyName(f);
entity.getUnderlyingNode().setProperty(propName, newVal);
log.info("SET " + f + " -> Neo4J simple node property [" + propName + "] with value=[" + newVal + "]");
@@ -159,6 +164,11 @@ public aspect Neo4jNodeBacking extends AbstractTypeAnnotatingMixinFields<GraphEn
throw new InvalidDataAccessResourceUsageException("Not in a Neo4j transaction.", e);
}
}
private boolean isPropertyType(Class fieldType) {
return fieldType.isPrimitive() || fieldType.equals(String.class) || (fieldType.getName().startsWith("java.lang") && Number.class.isAssignableFrom(fieldType));
// TODO boolean, arrays, character
}
private boolean isSingleRelationshipField(Field f) {
return f.getType().isAnnotationPresent(GraphEntity.class);
@@ -199,7 +209,7 @@ public aspect Neo4jNodeBacking extends AbstractTypeAnnotatingMixinFields<GraphEn
return new OneToNRelationshipInfo(DynamicRelationshipType.withName(relAnnotation.type()),
relAnnotation.direction().toNeo4jDir(), relAnnotation.elementClass(), graphEntityInstantiator);
}
throw new IllegalArgumentException("Not a Neo4j relationship field.");
throw new IllegalArgumentException("Not a Neo4j relationship field: "+field);
}
private static boolean isSingleRelationshipField(Field f) {

View File

@@ -16,6 +16,8 @@ public class Person {
private int age;
private Short height;
Person spouse;
@Relationship(type="mother", direction=Direction.OUTGOING)
@@ -62,6 +64,15 @@ public class Person {
public void setAge(int age) {
this.age = age;
}
public Short getHeight() {
return height;
}
public void setHeight(Short height) {
this.height = height;
}
public Person getSpouse() {
return spouse;

View File

@@ -39,11 +39,6 @@ privileged aspect Person_Graph_Entity {
public Person.new(Node node) {
setUnderlyingNode(node);
}
public Long Person.getId() {
return getUnderlyingNode().getId();
}
public static long Person.countPeople() {
return Neo4jHelper.count(Person.class, Person_Graph_Entity.graphDatabaseService());

View File

@@ -66,8 +66,10 @@ public class Neo4jGraphPersistenceTest {
Person p = new Person("Foo", 2);
p.setName("Michael");
p.setAge(35);
p.setHeight((short)182);
Assert.assertEquals("Michael", p.getUnderlyingNode().getProperty("Person.name"));
Assert.assertEquals(35, p.getUnderlyingNode().getProperty("Person.age"));
Assert.assertEquals((short)182, p.getUnderlyingNode().getProperty("Person.height"));
}
@Test
@@ -168,7 +170,7 @@ public class Neo4jGraphPersistenceTest {
} finally {
tx.finish();
}
Assert.assertEquals("Wrong age.", 35, p.getAge());
Assert.assertEquals("Wrong age.", (int)35, p.getAge());
}
@Test(expected = InvalidDataAccessApiUsageException.class)