Giriş
Şu satırı dahil ederiz
Şu satırı dahil ederiz
import org.springframework.data.rest.core.annotation.RepositoryRestResource;Maven
Maven'da şöyle yaparız.
Eğer bu repository JSON formatına çevrilirse kullanılacak nesne ismini belirtir.
exported Alanı
false yaparsak SpringData Rest bu repository sınıfı görmezden gelir.
path Alanı
Rest Endpoint için kullanılacak ismi belirtir.
Örnek
Elimizde şöyle bir kod olsun.
SpringData Rest Bulk Insert işlemini desteklemiyor.
Bidiretional OneToMany İlişki Yaratmak
SpringData Rest bidirectional ilişkileri yönetmiyor. Elimizdeki parent nesne şöyle olsun
Örnek
Elimizde şöyle bir kod olsun. Post nesnesi ilişkinin "One" tarafını temsil eder.
Elimizde şöyle bir kod olsun. Container nesnesi ilişkinin "One" tarafını temsil eder.
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>Kullanım
Şöyle yaparız
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
@RepositoryRestResource
public interface ProductionOrders extends CrudRepository<ProductionOrder, Long> {
}collectionResourceRel  Alanı
exported Alanı
false yaparsak SpringData Rest bu repository sınıfı görmezden gelir.
path Alanı
Rest Endpoint için kullanılacak ismi belirtir.
Örnek
Elimizde şöyle bir kod olsun.
@RepositoryRestResource(path = "tuple", collectionResourceRel = "tuple")
public interface TupleRepository extends JpaRepository<Tuple, Integer> {
  @RestResource(path = "findAllByAnr", rel = "findAllByAnr")
  List<Tuple> findAllByAnr(@Param("anr") String anr);
}http://localhost:8081/tuple/search/findAllByAnr?anr=somevalueSpringData Rest Bulk Insert işlemini desteklemiyor.
Bidiretional OneToMany İlişki Yaratmak
SpringData Rest bidirectional ilişkileri yönetmiyor. Elimizdeki parent nesne şöyle olsun
@Data
@Entity
@Table(name = "POTATO")
public class PotatoEntity {
    @Id
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private BigInteger id;
    @Column(name = "FIRSTNAME")
    private String firstname;
    @Column(name = "LASTNAME")
    private String lastname;
    @OneToMany(mappedBy = "potato", cascade = CascadeType.PERSIST)
    private List<DetailPotatoEntity> detailPotatoList;
}@Data
@Entity
@Table(name = "DETAIL_POTATO")
public class DetailPotatoEntity {
    @Id
    @Column(name = "ID")
    private BigInteger id;
    @Column(name = "WEIGHT")
    private BigDecimal weight;
    @Column(name = "HEIGHT")
    private BigDecimal height;
    @JoinColumn(name = "POTATO_ID", nullable = false)
    @ManyToOne
    @JsonBackReference(value = "potato-detailPotato")
    private PotatoEntity potato;
}{
  "firstname":"patate",
  "lastname":"potato",
  "detailPotatoList": [
    {
      "weight":12,
      "height":13
    }
  ]
}@PrePersist
public void prePersist() {
    detailPotatoList.forEach(detailPotato-> detailPotato.setPotato(this));
}Örnek
Elimizde şöyle bir kod olsun. Post nesnesi ilişkinin "One" tarafını temsil eder.
@Entity
public class Post {
    @Id
    @GeneratedValue
    private long id;
    private String author;
    private String content;
    private String title;
    @OneToMany
    private List<Comment> comments;
    // Standard getters and setters...
}@Entity
public class Comment {
    @Id
    @GeneratedValue
    private long id;
    private String author;
    private String content;
    @ManyToOne
    private Post post;
    // Standard getters and setters...
}http://{server:port}/comment METHOD:POST
{"author":"abc","content":"PQROHSFHFSHOFSHOSF", "post":"http://{server:port}/post/1"}curl -X POST -H "Content-Type: application/json" $url 
{ … // your payload // … }
201 Created
Location: $comment_urlcurl -X PUT -H "Content-Type: text/uri-list" $association_url
$comment_url
204 No ContentElimizde şöyle bir kod olsun. Container nesnesi ilişkinin "One" tarafını temsil eder.
@javax.persistence.Entity
@Table(name = "CONTAINER")
public class Container implements Serializable {
  ...
  @OneToMany(fetch = FetchType.LAZY, mappedBy = "container", cascade = CascadeType.ALL)
  private List<Item> items;
}@javax.persistence.Entity
@Table(name = "ITEM")
public class Record implements Serializable {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "ID")
  private Long id;
  @Basic
  private Long value;
  @Basic
  private Date date;
  @ManyToOne(fetch = FetchType.EAGER)
  @JoinColumn(name = "CONTAINER_ID")
  private Container container;
}POST { "value" : 666, "date" : "2016-01-31T23:00:00.000+0000",
  "container":"http://localhost:8080/container/1"} 
Hiç yorum yok:
Yorum Gönder