JPA @NamedEntityGraph uses

In order to solve the problem of too many sql queries when using @NamedEntityGraph

@Entity
@Table(name="s_person")
@NamedEntityGraph(name="person.all",attributeNodes={@NamedAttributeNode("address"),@NamedAttributeNode("cards")})
public class Person {
	
	@GeneratedValue(generator = "uuid2")
        @GenericGenerator(name = "uuid2", strategy = "uuid2")
	@Id
	private String id;
	private String username;
	private String age;
	
	@OneToOne
	@JoinColumn(name="address_id",referencedColumnName="id")
	private Address address;
	
	
	
	@OneToMany(fetch=FetchType.EAGER)
	@JoinTable(name="s_person_card",joinColumns={@JoinColumn(name="p_id")}
	,inverseJoinColumns={@JoinColumn(name="c_id")})
	private List<Cards> cards;
	
	public List<Cards> getCards() {
		return cards;
	}
	public void setCards(List<Cards> cards) {
		this.cards = cards;
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}

}

interface

public interface PersonRepository extends JpaRepository<Person, String>{

	@EntityGraph(value = "person.all" , type=EntityGraphType.FETCH)
	Person findById(String id);

}
When querying a person, it will automatically query the contents of Adress and Cards, and only issue one SQL statement, which solves the problem of too many SQL queries caused by the use of associated queries.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325631292&siteId=291194637