There are two implementation methods for multi-table query in spring data jpa. The first is to use hibernate cascade query to implement it, and the second is to create a result set interface to receive the results after the connected table query. Here is a second method.
1. One-to-one mapping
Entity UserInfo: User.
Entity Address: Home address.
Here, one-to-one association is achieved through the foreign key method (one entity is associated with the primary key of another entity through a foreign key).
Entity Class
1. Entity class UserInfo.java
package com.johnfnash.learn.domain;import java.io.Serializable;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;@Entity@Table(name="tb_user")public class UserInfo implements Serializable { private static final long serialVersionUID = 8283950216116626180L; @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long userId; private String name; private int age; private String sex; private String email; // Association with Address private Long addressId; public UserInfo() { super(); } public UserInfo(String name, int age, String sex, String email, Long addressId) { super(); this.name = name; this.age = age; this.sex = sex; this.email = email; this.addressId = addressId; } // getter, setter @Override public String toString() { return String.format("UserInfo [userId=%d, name=%s, age=%s, sex=%s, email=%s]", userId, name, age, sex, email); }} 2. Entity class Address.java
package com.johnfnash.learn.domain;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;@Entity@Table(name = "tb_address")public class Address { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long addressId; private String areaCode; private String country; private String province; private String city; private String area; private String detailAddress; public Address() { super(); } public Address(String areaCode, String country, String province, String city, String area, String detailAddress) { super(); this.areaCode = areaCode; this.country = country; this.province = province; this.city = city; this.area = area; this.detailAddress = detailAddress; } // getter, setter @Override public String toString() { return "Address [addressId=" + addressId + ", areaCode=" + areaCode + ", country=" + country + ", province=" + province + ", city=" + city + ", area=" + area + ", detailAddress=" + detailAddress + "]"; }} Dao Layer
1. UserInfoRepository.java
package com.johnfnash.learn.repository;import java.util.List;import org.springframework.data.jpa.repository.JpaRepository;import org.springframework.data.jpa.repository.Query;import com.johnfnash.learn.domain.UserInfo;import com.johnfnash.learn.domain.ViewInfo;public interface UserInfoRepository extends JpaRepository<UserInfo, Long> { @Query(value = "SELECT new com.johnfnash.learn.domain.ViewInfo(u, a) FROM UserInfo u, Address a WHERE u.addressId = a.addressId") public List<ViewInfo> findViewInfo();} Note: The ViewInfo class here is used to receive multi-table query result sets (using new + full class name constructor)
The code is as follows:
package com.johnfnash.learn.domain;import java.io.Serializable;public class ViewInfo implements Serializable { private static final long serialVersionUID = -6347911007178390219L; private UserInfo userInfo; private Address address; public ViewInfo() { } public ViewInfo(UserInfo userInfo) { Address address = new Address(); this.userInfo = userInfo; this.address = address; } public ViewInfo(Address address) { UserInfo userInfo = new UserInfo(); this.userInfo = userInfo; this.address = address; } public ViewInfo(UserInfo userInfo, Address address) { this.userInfo = userInfo; this.address = address; } // getter, setter} 2. AddressRepository.java
package com.johnfnash.learn.repository;import org.springframework.data.jpa.repository.JpaRepository;import com.johnfnash.learn.domain.Address;public interface AddressRepository extends JpaRepository<Address, Long> {} Test code
package com.johnfnash.learn;import java.util.List;import org.junit.After;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import com.johnfnash.learn.domain.Address;import com.johnfnash.learn.domain.UserInfo;import com.johnfnash.learn.domain.ViewInfo;import com.johnfnash.learn.repository.AddressRepository;import com.johnfnash.learn.repository.UserInfoRepository;@RunWith(SpringRunner.class)@SpringBootTestpublic class UserInfoRepositoryTests { @Autowired private UserInfoRepository userInfoRepository; @Autowired private AddressRepository addressRepository; @Before public void init() { Address addr1 = new Address("027","CN","HuBei", "WuHan","WuChang", "123 street"); Address addr2 = new Address("023","CN","ChongQing", "ChongQing", "YuBei", "123 road"); addressRepository.save(addr1); addressRepository.save(addr2); UserInfo user1 = new UserInfo("ZS", 21,"Male","[email protected]", addr1.getAddressId()); UserInfo user2 = new UserInfo("Ww", 25,"Male","[email protected]", addr2.getAddressId()); userInfoRepository.save(user1); userInfoRepository.save(user2); } @After public void deleteAll() { userInfoRepository.deleteAll(); addressRepository.deleteAll(); } @Test public void testQuery() { List<ViewInfo> viewInfos = userInfoRepository.findViewInfo(); for (ViewInfo viewInfo : viewInfos) { System.out.println(viewInfo.getUserInfo()); System.out.println(viewInfo.getAddress()); } }}The query related SQL is as follows:
Hibernate: select userinfo0_.user_id as col_0_0_, address1_.address_id as col_1_0_ from tb_user userinfo0_ cross join tb_address address1_ where userinfo0_.address_id=address1_.address_idHibernate: select userinfo0_.user_id as user_id1_4_0_, userinfo0_.address_id as address_2_4_0_, userinfo0_.age as age3_4_0_, userinfo0_.email as email4_4_0_, userinfo0_.name as name5_4_0_, userinfo0_.sex as sex6_4_0_ from tb_user userinfo0_ where userinfo0_.user_id=?Hibernate: select address0_.address_id as address_1_3_0_, address0_.area as area2_3_0_, address0_.area_code as area_cod3_3_0_, address0_.city as city4_3_0_, address0_.country as country5_3_0_, address0_.detail_address as detail_a6_3_0_, address0_.province as province7_3_0_ from tb_address address0_ where address0_.address_id=?Hibernate: select userinfo0_.user_id as user_id1_4_0_, userinfo0_.address_id as address_2_4_0_, userinfo0_.age as age3_4_0_, userinfo0_.email as email4_4_0_, userinfo0_.name as name5_4_0_, userinfo0_.sex as sex6_4_0_ from tb_user userinfo0_ where userinfo0_.user_id=?Hibernate: select address0_.address_id as address_1_3_0_, address0_.area as area2_3_0_, address0_.area_code as area_cod3_3_0_, address0_.city as city4_3_0_, address0_.country as country5_3_0_, address0_.detail_address as detailed_a6_3_0_, address0_.province as province7_3_0_ from tb_address address0_ where address0_.address_id=?Hibernate: select userinfo0_.user_id as user_id1_4_, userinfo0_.address_id as address_2_4_, userinfo0_.age as age3_4_, userinfo0_.email as email4_4_, userinfo0_.name as name5_4_, userinfo0_.sex as sex6_4_ from tb_user userinfo0_Hibernate: select address0_.address_id as address_1_3_, address0_.area as area2_3_, address0_.area_code as area_cod3_3_, address0_.city as city4_3_, address0_.country as country5_3_, address0_.detail_address as detail_a6_3_, address0_.province as province7_3_ from tb_address address0_
The query results are as follows:
UserInfo [userId=1, name=ZS, age=21, sex=Male, [email protected]]
Address [addressId=1, areaCode=027, country=CN, province=HuBei, city=WuHan, area=WuChang, detailAddress=123 street]
UserInfo [userId=2, name=Ww, age=25, sex=Male, [email protected]]
Address [addressId=2, areaCode=023, country=CN, province=ChongQing, city=ChongQing, area=YuBei, detailAddress=123 road]
2. Many-to-many mapping
Entity Author: Author.
Entity Book: Books
Here, many-to-many association is achieved through the association table.
Entity Class
Entity class: Author.java
package com.johnfnash.learn.domain;import java.io.Serializable;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;@Entitypublic class Author implements Serializable { private static final long serialVersionUID = 1227555837798655046L; @Id @GeneratedValue private Integer id; private String name; public Author() { super(); } public Author(String name) { super(); this.name = name; } // getter, setter @Override public String toString() { return String.format("Author [id=%s, name=%s]", id, name); }} Book.java entity class
package com.johnfnash.learn.domain;import java.io.Serializable;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;@Entitypublic class Book implements Serializable { private static final long serialVersionUID = -2470510857424220408L; @Id @GeneratedValue private Integer id; private String name; public Book() { super(); } public Book(String name) { super(); this.name = name; } //getter, setter @Override public String toString() { return String.format("Book [id=%s, name=%s]", id, name); }} Entity class BookAuthor.java
package com.johnfnash.learn.domain;import javax.persistence.Entity;import javax.persistence.Id;import javax.persistence.IdClass;import javax.persistence.Table;@Entity@IdClass(BookAuthorPK.class)@Table(name = "book_author")public class BookAuthor { @Id private Integer bookId; @Id private Integer authorId; public BookAuthor() { super(); } public BookAuthor(Integer bookId, Integer authorId) { super(); this.bookId = bookId; this.authorId = authorId; } // getter, setter}Note: Here, use the @IdClass annotation to specify a union primary key class to map multiple properties of an entity class. The code for this joint primary key class is as follows:
package com.johnfnash.learn.domain;import java.io.Serializable;public class BookAuthorPK implements Serializable { private static final long serialVersionUID = -1158141803682305656L; private Integer bookId; private Integer authorId; public Integer getBookId() { return bookId; } public void setBookId(Integer bookId) { this.bookId = bookId; } public Integer getAuthorId() { return authorId; } public void setAuthorId(Integer authorId) { this.authorId = authorId; }} Dao Layer
BookRepository.java
package com.johnfnash.learn.repository;import java.util.List;import org.springframework.data.jpa.repository.JpaRepository;import org.springframework.data.jpa.repository.Query;import com.johnfnash.learn.domain.Book;public interface BookRepository extends JpaRepository<Book, Integer> { @Query(nativeQuery = true, value = "SELECT b.id, b.name, GROUP_CONCAT(a.name) as authorName from book b, author a, book_author ba" + " where b.id = ba.book_id and a.id = ba.author_id and b.name like ?1 group by b.id, b.name") List<Object[]> findByNameContaining(String name);} Note:
1) Here, nativeQuery = true specifies that native SQL is used for querying (I personally think it is better to use native SQL for complex queries.
2) Here, mysql's built-in function GROUP_CONCAT is used for row-to-column conversion, and HQL cannot be directly recognized. Caused by: org.hibernate.QueryException: No data type for node: org.hibernate.hql.internal.ast.tree.MethodNode error may occur
JpaRepository.java
package com.johnfnash.learn.repository;import org.springframework.data.jpa.repository.JpaRepository;import com.johnfnash.learn.domain.Author;public interface AuthorRepository extends JpaRepository<Author, Integer> {} BookAuthorRepository.java
package com.johnfnash.learn.repository;import org.springframework.data.jpa.repository.JpaRepository;import com.johnfnash.learn.domain.BookAuthor;public interface BookAuthorRepository extends JpaRepository<BookAuthor, Integer> {} Test code
package com.johnfnash.learn;import static org.junit.Assert.assertEquals;import java.util.List;import org.junit.After;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import com.johnfnash.learn.domain.Author;import com.johnfnash.learn.domain.Book;import com.johnfnash.learn.domain.BookAuthor;import com.johnfnash.learn.repository.AuthorRepository;import com.johnfnash.learn.repository.BookAuthorRepository;import com.johnfnash.learn.repository.BookRepository;@RunWith(SpringRunner.class)@SpringBootTestpublic class BookRepositoryTests { @Autowired private BookRepository bookRepository; @Autowired private AuthorRepository authorRepository; @Autowired private BookAuthorRepository bookAuthorRepository; @Before public void init() { Author lewis = new Author("Lewis"); Author mark = new Author("Mark"); Author peter = new Author("Peter"); authorRepository.save(lewis); authorRepository.save(mark); authorRepository.save(peter); Book spring = new Book("Spring in Action"); Book springboot = new Book("Spring Boot in Action"); bookRepository.save(spring); bookRepository.save(springboot); bookAuthorRepository.save(new BookAuthor(spring.getId(), lewis.getId())); bookAuthorRepository.save(new BookAuthor(spring.getId(), mark.getId())); bookAuthorRepository.save(new BookAuthor(springboot.getId(), mark.getId())); bookAuthorRepository.save(new BookAuthor(springboot.getId(), peter.getId())); } @After public void deleteAll() { bookAuthorRepository.deleteAll(); bookRepository.deleteAll(); authorRepository.deleteAll(); } @Test public void findAll() { assertEquals(bookRepository.findAll().size(), 2); assertEquals(authorRepository.findAll().size(), 3); List<Object[]> books = bookRepository.findByNameContaining("Spring%"); for (Object[] book : books) { for (Object object : book) { System.out.print(object + ", "); } System.out.println(); } }}After executing the findAll method, the relevant SQL for query is as follows:
Hibernate: SELECT b.id, b.name, GROUP_CONCAT(a.name) as authorName from book b, author a, book_author ba where b.id = ba.book_id and a.id = ba.author_id and b.name like ? group by b.id, b.name
The output results are as follows:
3652, Spring in Action, Lewis,Mark,
3653, Spring Boot in Action, Mark, Peter,
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.