commit
3ca1646e75
@ -0,0 +1,43 @@
|
|||||||
|
package com.baeldung.entitygraph.model;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.FetchType;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.JoinColumn;
|
||||||
|
import javax.persistence.ManyToOne;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Characteristic {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
private Long id;
|
||||||
|
private String type;
|
||||||
|
|
||||||
|
@ManyToOne(fetch = FetchType.LAZY)
|
||||||
|
@JoinColumn
|
||||||
|
private Item item;
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setType(String type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Item getItem() {
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setItem(Item item) {
|
||||||
|
this.item = item;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
package com.baeldung.entitygraph.model;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.NamedAttributeNode;
|
||||||
|
import javax.persistence.NamedEntityGraph;
|
||||||
|
import javax.persistence.OneToMany;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@NamedEntityGraph(name = "Item.characteristics",
|
||||||
|
attributeNodes = @NamedAttributeNode("characteristics")
|
||||||
|
)
|
||||||
|
public class Item {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
private Long id;
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@OneToMany(mappedBy = "item")
|
||||||
|
private List<Characteristic> characteristics = new ArrayList<>();
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Characteristic> getCharacteristics() {
|
||||||
|
return characteristics;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCharacteristics(List<Characteristic> characteristics) {
|
||||||
|
this.characteristics = characteristics;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.baeldung.entitygraph.repository;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.EntityGraph;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
import com.baeldung.entitygraph.model.Characteristic;
|
||||||
|
|
||||||
|
public interface CharacteristicsRepository extends JpaRepository<Characteristic, Long> {
|
||||||
|
|
||||||
|
@EntityGraph(attributePaths = {"item"})
|
||||||
|
Characteristic findByType(String type);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.baeldung.entitygraph.repository;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.EntityGraph;
|
||||||
|
import org.springframework.data.jpa.repository.EntityGraph.EntityGraphType;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
import com.baeldung.entitygraph.model.Item;
|
||||||
|
|
||||||
|
public interface ItemRepository extends JpaRepository<Item, Long> {
|
||||||
|
|
||||||
|
@EntityGraph(value = "Item.characteristics", type = EntityGraphType.FETCH)
|
||||||
|
Item findByName(String name);
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
package com.baeldung.entitygraph;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||||
|
import org.springframework.test.context.jdbc.Sql;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import com.baeldung.entitygraph.model.Characteristic;
|
||||||
|
import com.baeldung.entitygraph.model.Item;
|
||||||
|
import com.baeldung.entitygraph.repository.CharacteristicsRepository;
|
||||||
|
import com.baeldung.entitygraph.repository.ItemRepository;
|
||||||
|
|
||||||
|
@DataJpaTest
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@Sql(scripts = "/entitygraph-data.sql")
|
||||||
|
public class EntityGraphIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ItemRepository itemRepo;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CharacteristicsRepository characteristicsRepo;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenEntityGraph_whenCalled_shouldRetrunDefinedFields() {
|
||||||
|
Item item = itemRepo.findByName("Table");
|
||||||
|
assertThat(item.getId()).isEqualTo(1L);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAdhocEntityGraph_whenCalled_shouldRetrunDefinedFields() {
|
||||||
|
Characteristic characteristic = characteristicsRepo.findByType("Rigid");
|
||||||
|
assertThat(characteristic.getId()).isEqualTo(1L);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
INSERT INTO Item(id,name) VALUES (1,'Table');
|
||||||
|
INSERT INTO Item(id,name) VALUES (2,'Bottle');
|
||||||
|
|
||||||
|
INSERT INTO Characteristic(id,item_id, type) VALUES (1,1,'Rigid');
|
||||||
|
INSERT INTO Characteristic(id,item_id,type) VALUES (2,1,'Big');
|
||||||
|
INSERT INTO Characteristic(id,item_id,type) VALUES (3,2,'Fragile');
|
||||||
|
INSERT INTO Characteristic(id,item_id,type) VALUES (4,2,'Small');
|
Loading…
x
Reference in New Issue
Block a user