2012-11-25 64 views
3

我目前正在實施一些試驗樣品學習Neo4j的春季數據感謝像彈簧數據你好,世界的cineast項目和其他樣本...Neo4j的節點持久性問題的情況下

不幸的是我米現在面臨一個問題,我不明白,即使我已經花了一些時間在我的代碼和其他示例代碼的Eclipse調試器試圖compage,因此找到一個解決方案。這個問題是特別爲阻止我,因爲我無法驗證Neo4j的持久性,即使它真的應該是一個平凡的使用情況......

這裏是一流的,我測試:

@NodeEntity 
public class SimplePersonImpl { 
@GraphId 
private Long nodeId; 

@Indexed 
private String id  = null; 

@Indexed(indexType=IndexType.FULLTEXT, indexName = "LastName") 
private String lastName = null; 
@Indexed(indexType=IndexType.FULLTEXT, indexName = "FirstName") 
private String firstName = null; 

public SimplePersonImpl() { 

} 

public SimplePersonImpl(String firstName_, String lastName_) { 
    this.firstName=firstName_; 
    this.lastName=lastName_; 
    this.id=this.firstName+"."+this.lastName; 
} 

public String getID() { 
    return this.id; 
} 

public String getFirstName() { 
    return this.firstName; 
} 

public String getLastName() { 
    return this.lastName; 
} 

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

public void setFirstName(String firstName_) { 
    this.firstName=firstName_; 
} 

public void setLastName(String lastName_) { 
    this.lastName=lastName_; 
} 

@Override 
public boolean equals(Object o) { 
    if (this == o) return true; 
    if (o == null || getClass() != o.getClass()) return false; 

    SimplePersonImpl person = (SimplePersonImpl) o; 
    if (nodeId == null) return super.equals(o); 
    return nodeId.equals(person.nodeId); 

} 

@Override 
public int hashCode() { 
    return nodeId != null ? nodeId.hashCode() : super.hashCode(); 
} 

@Override 
public String toString() { 
    return String.format("Person{first name='%s', last name='%s'}", firstName, lastName); 
} 
} 

我目前正在測試此類感謝以下jUnit測試用例:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration({"/yes-test-context.xml"}) 
@Transactional 
public class SimplePersonImplTest { 
@Autowired Neo4jTemplate template; 

@Rollback(false) 
@BeforeTransaction 
public void cleanUpGraph() { 
    Neo4jHelper.cleanDb(template); 
} 

@Test @Transactional 
public void persistedPersonShouldBeRetrievableFromGraphDB() { 
    SimplePersonImpl qqun   = template.save(new SimplePersonImpl()); 
    SimplePersonImpl retrievedQqun = template.findOne(qqun.getNodeId(), SimplePersonImpl.class); 
    assertEquals("retrieved person matches persisted one", qqun, retrievedQqun); 
} 

@Test @Transactional 
public void persistedPersonWithPropertiesShouldBeRetrievableFromGraphDB() { 
    SimplePersonImpl qqun = template.save(new SimplePersonImpl("testFN","testLN")); 

    SimplePersonImpl retrievedQqun = template.findOne(qqun.getNodeId(), SimplePersonImpl.class); 
    assertEquals("check memory first name matches persisted one", qqun.getFirstName(), retrievedQqun.getFirstName());  
    assertEquals("check memory last name matches persisted one", qqun.getLastName(), retrievedQqun.getLastName()); 
} 
} 

第一個測試運行成功,但第二個測試失敗。當與Eclipse調試器檢查,我可以看到:

  • 保存梅索德後返回SimplePersonImpl qqun與正確firtName,姓氏和id的值。但nodeId爲空,我不明白爲什麼。但是,因爲這種行爲在Spring-data hello-worlds樣本中是一樣的,我猜我的問題不是來自那裏。
  • 即使nodeId在我的qqun對象中爲null qqun.getNodeId()返回1.我不明白從哪裏來的這個值,但讓我們繼續
  • findOne方法返回一個retrieveQqun。所有retrieveQqun屬性都是null,它似乎是我firstName上的第一個assertEquals失敗的原因。

在這裏,我真的不明白我做錯了什麼(我想我做到了),但很明顯彈簧數據neo4j堅持後面有很多東西我不明白。我會很樂意在這些問題上得到一些答案(爲什麼我會在保存調用後看到nodeId = null?爲什麼我檢索一個非null對象,所有屬性都爲null?...)。

最後一點,其中可能會有一些錯誤是我測試的情況下的配置,但我又看不出是哪裏的問題可能是:

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<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.xsd 
    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 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd"> 

<context:annotation-config/> 
<context:spring-configured/> 
<context:component-scan base-package="yes.ds.domain.neo4j"/> 

<neo4j:config graphDatabaseService="graphDatabaseService"/> 
<bean id="graphDatabaseService" class="org.neo4j.test.ImpermanentGraphDatabase" destroy-method="shutdown"/> 
<!-- <neo4j:config storeDirectory="target/neo4j-db"/> --> 
<neo4j:repositories base-package="yes.ds.repository.neo4j"/> 
<tx:annotation-driven mode="proxy"/></beans> 

謝謝

+0

我認爲你的困惑來自aspectj所做的截取,它攔截了你的類中的字段訪問,並且查看了這些值的節點屬性, 該類中的字段未被考慮。 沒有一個獲得者返回一個值嗎? 作爲一個實驗:你可以嘗試直接在構造函數中設置字段,以設置它們在外部還是設置? –

+0

我已在保存調用之前使用setter進行了測試:相同的行爲... – echinopsii

+0

我發現問題出在我的問題上。我不確定它是否與你的問題直接相關,但是如果你創建了一個自定義關係類(對於[at] RelatedToVia關係),並且使用該類創建的關係將自動具有__type__屬性和包和類的名稱的類。如果你有一個創建關係的[at] Query,那麼這個查詢也不會創建__type__屬性。這會在稍後加載關係時導致問題,從而導致缺少__type__屬性問題。希望有所幫助! –

回答

0

我想我遇到過類似的問題在我創建的測試實體中。 在這種特殊情況下,我忘了用@Transactional註釋一個getter-method。

註釋@Transactional在獲得者和設置者NodeEntities上都是必需的,否則它們的行爲就像你描述的那樣。添加註解爲我解決了它。

事務支持必須啓用原因。