Reputation: 11
Fellow Spring Data REST enthusiasts, I am running my Spring Data REST 1.1 application, and I am attempting use curl to add an entity relationship using the "text/uri-list" Content-type, as described in the link: Example-API-usage-with-curl.
curl -v -d "http://localhost:8080/simplemvc/rest/enemies/3" -H "Content-Type: text/uri-list" http://localhost:8080/simplemvc/rest/heroes/1/defeatedEnemies
Unfortunately, although the server returns "201 Created", the body contains an empty JSON object, and the entity relationship does not get created:
{
"links" : [ ],
"content" : [ ]
}
I would expect to see a SQL UPDATE being executed, but analyzing the SQL reveals that only SELECT statements occur:
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=?
Interestingly, if I "manually" add a relationship by executing a SQL statement in my database client:
UPDATE ENEMY SET HERO_ID = 1 WHERE ENEMY_ID = 1;
and then execute the curl statement:
curl -v -H "Accept: application/json" http://localhost:8080/simplemvc/rest/heroes/1/defeatedEnemies
I get a JSON representation that demonstrates -- via hypermedia links -- that Spring Data REST recognizes the one-to-many relationship between the Hero and Enemy entities:
{
"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"
} ]
} ]
}
This is an existing Spring MVC app to which Spring Data REST is being added, using the following article as a guide: Adding-Spring-Data-REST-to-an-existing-Spring-MVC-Application
I have tried using both H2 and MySQL databases with the same result. Below are my JPA entities, Spring Data JPA Repositories, application context, and 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> {
}
root-context.xml includes:
<jpa:repositories base-package="com.simple.simplemvc.repositories" />
<bean id="restConfig" class="org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration"/>
web.xml includes:
<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>
Any ideas? Thanks!
Upvotes: 1
Views: 2211
Reputation: 476
To add relation between existing entities try POST with
Content-Type: application/json
on
http://localhost:8080/simplemvc/rest/enemies/3
with body
{"hero":"http://localhost:8080/simplemvc/rest/heroes/1"}
To add new Enemy entity with relation to hero try POST on
http://localhost:8080/simplemvc/rest/enemies
with body
{"name":"Enemy name", "description":"Enemy description", "hero":"http://localhost:8080/simplemvc/rest/heroes/1"}
Tested on spring-data-rest-webmvc version 2.2.0.RELEASE
Upvotes: 2
Reputation: 10717
I think you are facing the same problem I asked about here:
The answer solves the problem of adding an entity on the Many side of a relationship, though not using the "text/uri-list".
Upvotes: 0