2013-10-02 126 views
1

Fellow Spring Data REST愛好者,我正在運行我的Spring Data REST 1.1應用程序,並試圖使用curl使用「text/uri-list」內容添加實體關係類型,如鏈接中所述:Example-API-usage-with-curlSpring Data REST:添加實體關係時出現靜默失敗

curl -v -d "http://localhost:8080/simplemvc/rest/enemies/3" -H "Content-Type: text/uri-list" http://localhost:8080/simplemvc/rest/heroes/1/defeatedEnemies 

不幸的是,儘管服務器返回「201創建」,體內包含一個空的JSON對象,與實體的關係還沒有生成:

{ 
    "links" : [ ], 
    "content" : [ ] 
} 

我希望看到一個SQL UPDATE正在執行,但分析SQL顯示,只有SELECT語句出現:

Hibernate: select hero0_.HERO_ID as HERO1_1_0_, hero0_.name as name2_1_0_ from HERO hero0_ where hero0_.HERO_ID=? 
Hibernate: select defeateden0_.HERO_ID as HERO4_1_1_, defeateden0_.ENEMY_ID as ENEMY1_0_1_, defeateden0_.ENEMY_ID as ENEMY1_0_0_, defeateden0_.description as descript2_0_0_, defeateden0_.HERO_ID as HERO4_0_0_, defeateden0_.name as name3_0_0_ from ENEMY defeateden0_ where defeateden0_.HERO_ID=? 
Hibernate: select enemy0_.ENEMY_ID as ENEMY1_0_1_, enemy0_.description as descript2_0_1_, enemy0_.HERO_ID as HERO4_0_1_, enemy0_.name as name3_0_1_, hero1_.HERO_ID as HERO1_1_0_, hero1_.name as name2_1_0_ from ENEMY enemy0_ left outer join HERO hero1_ on enemy0_.HERO_ID=hero1_.HERO_ID where enemy0_.ENEMY_ID=? 

有趣的是,如果我「手動」在我的數據執行SQL語句添加的關係基礎客戶端:

UPDATE ENEMY SET HERO_ID = 1 WHERE ENEMY_ID = 1; 

,然後執行卷曲聲明:

curl -v -H "Accept: application/json" http://localhost:8080/simplemvc/rest/heroes/1/defeatedEnemies 

我得到的演示JSON表示 - 通過超媒體鏈接 - 這春數據REST承認一個一對多的關係英雄和敵方實體之間:

{ 
    "links" : [ ], 
    "content" : [ { 
    "name" : "Red Ghost", 
    "description" : "Likes to chase", 
    "links" : [ { 
     "rel" : "self", 
     "href" : "http://localhost:8080/simplemvc/rest/enemies/1" 
    }, { 
     "rel" : "enemy.enemy.hero", 
     "href" : "http://localhost:8080/simplemvc/rest/enemies/1/hero" 
    } ] 
    } ] 
} 

這是被添加了現有Spring MVC的應用,其彈簧數據REST,使用下面的文章作爲指導:Adding-Spring-Data-REST-to-an-existing-Spring-MVC-Application

我已經嘗試使用H2和MySQL數據庫都有相同的結果。下面是我的JPA實體,春天JPA的數據存儲庫,應用程序上下文,和web.xml:

Hero.java

@Entity 
@Table(name = "HERO") 
public class Hero { 

    @TableGenerator(
      name="heroGen", 
      table="ID_GEN", 
      pkColumnName="GEN_KEY", 
      valueColumnName="GEN_VALUE", 
      pkColumnValue="HERO_ID", 
      allocationSize=1) 
    @Id 
    @GeneratedValue(strategy=TABLE, generator="heroGen") 
    @Column(name = "HERO_ID") 
    private Integer id; 

    @Column 
    private String name; 

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "hero") 
    private Set<Enemy> defeatedEnemies; 

    ... 
} 

Enemy.java

@Entity 
@Table(name = "ENEMY") 
public class Enemy { 

    @TableGenerator(name="enemyGen", 
      table="ID_GEN", 
      pkColumnName="GEN_KEY", 
      valueColumnName="GEN_VALUE", 
      pkColumnValue="ENEMY_ID", 
      allocationSize=1) 
    @Id 
    @GeneratedValue(strategy=TABLE, generator="enemyGen") 
    @Column(name="ENEMY_ID") 
    private Integer id; 

    @Column 
    private String name; 

    @Column 
    private String description; 

    @ManyToOne 
    @JoinColumn(name = "HERO_ID") 
    private Hero hero; 

    ... 
} 

HeroRepository。 java

@RestResource(path = "heroes") 
public interface HeroRepository extends CrudRepository<Hero, Integer> { 

} 

EnemyRepository.java

@RestResource(path = "enemies") 
public interface EnemyRepository extends CrudRepository<Enemy, Integer> { 

} 

根的context.xml包括:

<jpa:repositories base-package="com.simple.simplemvc.repositories" /> 

<bean id="restConfig" class="org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration"/> 

的web.xml包括:

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/spring/root-context.xml</param-value> 
</context-param> 

<servlet> 
    <servlet-name>rest-dispatcher</servlet-name> 
    <servlet-class>org.springframework.data.rest.webmvc.RepositoryRestDispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>rest-dispatcher</servlet-name> 
    <url-pattern>/rest/*</url-pattern> 
</servlet-mapping> 

任何想法?謝謝!

回答

0

我覺得你面對我問了一下這裏同樣的問題:

Spring Data Rest, Many to one

答案解決了上關係的許多方面,增加一個實體,雖然沒有使用「文本/的問題URI列表」。

2

要添加關係之間存在的實體試圖POST與

Content-Type: application/json 

http://localhost:8080/simplemvc/rest/enemies/3 

與身體

{"hero":"http://localhost:8080/simplemvc/rest/heroes/1"} 

要使用關係增添新的敵人實體英雄嘗試

POST
http://localhost:8080/simplemvc/rest/enemies 

與身體

{"name":"Enemy name", "description":"Enemy description", "hero":"http://localhost:8080/simplemvc/rest/heroes/1"} 

測試在彈簧 - 數據 - 其餘-webmvc版本2.2.0.RELEASE

相關問題