У меня есть 2 таблицы, которые связываются в соединении «Один ко многим», когда я устанавливаю отношение в EAGER, затем записи выбираются из таблицы A, а затем для каждой записи данные выбираются из таблицы B, если, скажем, Iиметь 100 записей в таблице B, затем выполняется 100 запросов выбора, что очень плохо сказывается на производительности.Что мне нужно сделать, чтобы установить его правильно, чтобы все данные были выбраны в 1 запросе?
вот весь код: для таблицы A (опрос)
package hibernateDataFiles;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
/**
* Surveys generated by hbm2java
*/
@Entity
@Table(name = "surveys", schema = "edi_ms")
public class Survey implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private long surveyId;
private String umn;
private String firstName;
private String middleName;
private String lastName;
private String phoneNumber;
private Date creationDate;
private Long shortFeedbackGrade;
private String shortFeedbackComment;
private List<MemberAnswer> memberAnswers = new ArrayList<MemberAnswer>(0);
private List<CategoryAnswer> categoriesAnswers = new ArrayList<CategoryAnswer>(0);
public Survey() {
}
public Survey(long surveyId, String firstName, String lastName, String phoneNumber, Date creationDate) {
this.surveyId = surveyId;
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
this.creationDate = creationDate;
}
public Survey(long surveyId, String umn, String firstName, String middleName, String lastName, String phoneNumber,
Date creationDate, Long shortFeedbackGrade, String shortFeedbackComment,
List<MemberAnswer> memberAnswers, List<CategoryAnswer> categoriesAnswer) {
this.surveyId = surveyId;
this.umn = umn;
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
this.creationDate = creationDate;
this.shortFeedbackGrade = shortFeedbackGrade;
this.shortFeedbackComment = shortFeedbackComment;
this.memberAnswers = memberAnswers;
this.categoriesAnswers = categoriesAnswer;
}
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "survey_id_seq")
@SequenceGenerator(allocationSize = 1, name = "survey_id_seq", sequenceName = "EDI_MS.survey_id_seq")
@Id
@Column(name = "survey_id", unique = true, nullable = false)
public long getSurveyId() {
return this.surveyId;
}
public void setSurveyId(long surveyId) {
this.surveyId = surveyId;
}
@Column(name = "umn", length = 15)
public String getUmn() {
return this.umn;
}
public void setUmn(String umn) {
this.umn = umn;
}
@Column(name = "first_name", nullable = false, length = 20)
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Column(name = "middle_name", length = 20)
public String getMiddleName() {
return this.middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
@Column(name = "last_name", nullable = false, length = 20)
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Column(name = "phone_number", nullable = false, length = 15)
public String getPhoneNumber() {
return this.phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "creation_date", nullable = false, length = 29)
public Date getCreationDate() {
return this.creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
@Column(name = "short_feedback_grade")
public Long getShortFeedbackGrade() {
return this.shortFeedbackGrade;
}
public void setShortFeedbackGrade(Long shortFeedbackGrade) {
this.shortFeedbackGrade = shortFeedbackGrade;
}
@Column(name = "short_feedback_comment", length = 500)
public String getShortFeedbackComment() {
return this.shortFeedbackComment;
}
public void setShortFeedbackComment(String shortFeedbackComment) {
this.shortFeedbackComment = shortFeedbackComment;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "survey")
@NotFound(action = NotFoundAction.IGNORE)
public List<MemberAnswer> getMemberAnswers() {
return this.memberAnswers;
}
public void setMemberAnswers(List<MemberAnswer> membersAnswers) {
this.memberAnswers = membersAnswers;
}
@OneToMany(fetch = FetchType.EAGER, mappedBy = "survey")
@NotFound(action = NotFoundAction.IGNORE)
public List<CategoryAnswer> getCategoriesAnswers() {
return this.categoriesAnswers;
}
public void setCategoriesAnswers(List<CategoryAnswer> categoriesAnswers) {
this.categoriesAnswers = categoriesAnswers;
}
}
здесьэто JPA
import java.util.Date;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import hibernateDataFiles.Survey;
public interface SurveyRepository extends JpaRepository<Survey, Long> {
@Query("from Survey where ?1 <= creation_date and creation_date < ?2 ")
List<Survey> getSurveysByDates(Date fromDate , Date toDate );
}
вот таблица B (category_answers)
package hibernateDataFiles;
// Generated Jul 5, 2018 8:37:29 AM by Hibernate Tools 5.2.10.Final
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIgnore;
/**
* CategoriesAnswers generated by hbm2java
*/
@Entity
@Table(name = "categories_answers", schema = "edi_ms")
public class CategoryAnswer implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private CategoryAnswerId id;
private Category category;
private Survey survey;
private long grade;
private String comment;
public CategoryAnswer() {
}
public CategoryAnswer(CategoryAnswerId id, Category category, Survey survey, long grade) {
this.id = id;
this.category = category;
this.survey = survey;
this.grade = grade;
}
public CategoryAnswer(CategoryAnswerId id, Category category, Survey survey, long grade, String comment) {
this.id = id;
this.category = category;
this.survey = survey;
this.grade = grade;
this.comment = comment;
}
@EmbeddedId
@AttributeOverrides({ @AttributeOverride(name = "surveyId", column = @Column(name = "survey_id", nullable = false)),
@AttributeOverride(name = "categoryId", column = @Column(name = "category_id", nullable = false)) })
public CategoryAnswerId getId() {
return this.id;
}
public void setId(CategoryAnswerId id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id", nullable = false, insertable = false, updatable = false)
public Category getCategory() {
return this.category;
}
public void setCategory(Category category) {
this.category = category;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "survey_id", nullable = false, insertable = false, updatable = false)
@JsonIgnore
public Survey getSurvey() {
return this.survey;
}
public void setSurvey(Survey survey) {
this.survey = survey;
}
@Column(name = "grade", nullable = false)
public long getGrade() {
return this.grade;
}
public void setGrade(long grade) {
this.grade = grade;
}
@Column(name = "comment", length = 500)
public String getComment() {
return this.comment;
}
public void setComment(String comment) {
this.comment = comment;
}
}
and categoryAnswerId (pk of the table )
package hibernateDataFiles;
// Generated Jul 5, 2018 8:37:29 AM by Hibernate Tools 5.2.10.Final
import javax.persistence.Column;
import javax.persistence.Embeddable;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
/**
* CategoriesAnswersId generated by hbm2java
*/
@Embeddable
public class CategoryAnswerId implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private long surveyId;
private long categoryId;
public CategoryAnswerId() {
}
public CategoryAnswerId(long surveyId, long categoryId) {
this.surveyId = surveyId;
this.categoryId = categoryId;
}
@Column(name = "survey_id", nullable = false)
public long getSurveyId() {
return this.surveyId;
}
public void setSurveyId(long surveyId) {
this.surveyId = surveyId;
}
@Column(name = "category_id", nullable = false)
public long getCategoryId() {
return this.categoryId;
}
public void setCategoryId(long categoryId) {
this.categoryId = categoryId;
}
public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof CategoryAnswerId))
return false;
CategoryAnswerId castOther = (CategoryAnswerId) other;
return (this.getSurveyId() == castOther.getSurveyId()) && (this.getCategoryId() == castOther.getCategoryId());
}
public int hashCode() {
int result = 17;
result = 37 * result + (int) this.getSurveyId();
result = 37 * result + (int) this.getCategoryId();
return result;
}
}
и JPA:
package repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;
import hibernateDataFiles.Category;
public interface CategoryRepository extends JpaRepository<Category, Long>{
@Transactional
@Modifying
@Query("update Category set expiration_date = current_date() where category_id = ?1 ")
void expireCategory(Long id );
@Query("from Category where function ('coalesce' ,effectiveDate ,current_date() ) <= current_date() "
+ "and function('coalesce' ,expirationDate , to_date('50001231','yyyymmdd')) > current_date() ")
List<Category> getEffective( );
}