2016-10-12 2 views
1

Ich habe People Einheit zu definieren, die name und parent hat, wo parent ein People ist.Frühling Stiefel/Hibernate: Wie eine Eins-zu-Eins-Beziehung unidirektionale

@Entity 
public class People { 

    @Id 
    private Long id; 

    @Column 
    @NotNull(message = "error.name.type.null") 
    private String name; 

    @OneToOne(cascade=CascadeType.ALL,fetch = FetchType.EAGER) 
    private People parent; 

    // getters and setters 
} 

Wenn ich Daten speichern, mit PUT, gebe ich die id 1 als Pfadvariablen und die name als Dateneingabe zur Verfügung stelle und nicht bestehen parent wie:

curl -i -X PUT -H "Content-Type:application/json" -d '{"name":"John"}'

Antwort

http://localhost:8080/people/1 
HTTP/1.1 200 
Content-Type: application/json;charset=UTF-8 
Transfer-Encoding: chunked 
Date: Wed, 12 Oct 2016 13:51:52 GMT 

Wenn ich abrufen, was gespeichert ist, parent habe bereits einen Link zu dem Objekt selbst, obwohl ich null bei der curl Anfrage übergeben habe.

Was mache ich falsch mit der Hibernate One-to-One Beziehung?

curl -i -X GET http://localhost:8080/people 
HTTP/1.1 200 
Content-Type: application/hal+json;charset=UTF-8 
Transfer-Encoding: chunked 
Date: Wed, 12 Oct 2016 13:53:10 GMT 

{ 
    "_embedded" : { 
    "people" : [ { 
     "name" : "John", 
     "_links" : { 
     "self" : { 
      "href" : "http://localhost:8080/people/1" 
     }, 
     "people" : { 
      "href" : "http://localhost:8080/people/1" 
     }, 
     "parent" : { 
      "href" : "http://localhost:8080/people/1/parent" 
     } 
     } 
    } ] 
    }, 
    "_links" : { 
    "self" : { 
     "href" : "http://localhost:8080/people" 
    }, 
    "profile" : { 
     "href" : "http://localhost:8080/profile/people" 
    }, 
    "search" : { 
     "href" : "http://localhost:8080/people/search" 
    } 
    } 
} 
+0

Warum tun, was Sie es falsch ist? Wenn Sie sich mit dem Link _http: // localhost: 8080/people/1/parent_ verbinden, was bekommen Sie? –

+0

@ Cèsar Es leitet mich zurück zum Link. Da ich keinen Wert an parent übergeben habe, sollte es null sein. –

Antwort

Verwandte Themen