2014-12-15 9 views
6

मैं, डाटा-जेपीए के साथ एक EntityGraph लागू करने के लिए कोशिश कर रहा हूँ का कारण बनता है QueryDslPredicateExecutor<T> पद्धति का उपयोग करके उजागर findAll(Predicate, Pageable) एक मैं की जरूरत है, मैं @EntityGraph के साथ उनकी व्याख्या करने के लिए इसे ओवरराइड करने की कोशिश की के बाद से तो मुसीबतों शुरू हुआ फेंक रहा है:अधिभावी वसंत-डाटा-जेपीए EntityGraph साथ डिफ़ॉल्ट विधि टिप्पणी करने QueryException

org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=appointment,role=com.physioclinic.entity.Appointment.createdBy,tableName=user,tableAlias=user5_,origin=appointment appointmen0_,columns={appointmen0_.createdBy_id ,className=com.physioclinic.entity.User}}] [select count(appointment) 
from com.physioclinic.entity.Appointment appointment where lower(concat(concat(appointment.patient.person.name,?1),appointment.patient.person.surname)) like ?2 escape '!']; nested exception is java.lang.IllegalArgumentException: org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=appointment,role=com.physioclinic.entity.Appointment.createdBy,tableName=user,tableAlias=user5_,origin=appointment appointmen0_,columns={appointmen0_.createdBy_id ,className=com.physioclinic.entity.User}}] 

जब मैं डिफ़ॉल्ट विधि का उपयोग जिस तरह से यह है आता है बुरा कुछ भी नहीं है, होता भी एक ही Predicate का उपयोग कर, लेकिन मैं EntityGraph उपयोग नहीं कर सकते हैं, वहाँ मेरी कार्यान्वयन या के साथ कुछ समस्या है भविष्यवाणी के साथ?

परिदृश्य में वस्तुओं रहा:

इकाई

@Table(name = "appointment") 
@Entity 
@Getter 
@Setter 
@NamedEntityGraphs({@NamedEntityGraph(name = "graph.Appointment.default", includeAllAttributes = true, 
    attributeNodes = {@NamedAttributeNode(value = "physiotherapist"), 
     @NamedAttributeNode(value = "patient"), 
     @NamedAttributeNode(value = "care")})}) 
public class Appointment extends PersistableAuditable<User, Long> { 

    private static final long serialVersionUID = -4325126792470516159L; 

    @DateTimeFormat(pattern = "dd/MM/yyyy") 
    @NotNull 
    @Column(name = "date") 
    private LocalDate date; 

    @DateTimeFormat(pattern = "HH:mm") 
    @NotNull 
    @Column(name = "schedule") 
    private LocalTime schedule; 

    @ManyToOne 
    @JoinColumn(name = "physiotherapist", referencedColumnName = "id") 
    private Physiotherapist physiotherapist; 

    @ManyToOne 
    @JoinColumn(name = "service", referencedColumnName = "id") 
    private Service service; 

    @ManyToOne 
    @JoinColumn(name = "patient", referencedColumnName = "id") 
    private Patient patient; 

    @ManyToOne(cascade = CascadeType.REMOVE) 
    @JoinColumn(name = "care", referencedColumnName = "id") 
    private Care care; 

    public Appointment(long id) { 
     setId(id); 
    } 

    public Appointment() { 
    } 

    /** 
    * Helpers 
    */ 

    public boolean isSpecialPrice() { 
     return care.getPrivateCare() && care.getSpecial() && care.getSpecialPrice() != null; 
    } 

    public boolean isPrivatePrice() { 
     return care.getPrivateCare() && care.getHealthCare() == null; 
    } 

    public boolean isHealthCarePrice() { 
     return !care.getPrivateCare() && care.getHealthCare() != null; 
    } 
} 

भंडार

public interface AppointmentRepository extends JpaRepository<Appointment, Long>, 
               QueryDslPredicateExecutor<Appointment> { 

    @EntityGraph(value = "graph.Appointment.default") 
    Page<Appointment> findAll(Predicate predicate, Pageable pageable); 
} 

विधेय

public final class AppointmentPredicate { 

    private AppointmentPredicate() { 
    } 

    public static Predicate bySearch(String search) { 
     QPerson person = QAppointment.appointment.patient.person; 
     return person.name.concat(" ").concat(person.surname).containsIgnoreCase(search); 
    } 

} 
+0

जहां आप समाधान ढूंढने में सक्षम हैं? एक ही समस्या है। – bjoernhaeuser

+0

अफसोस की बात है कि मुझे कोई समाधान नहीं मिला, इसलिए मैं इसे डिफ़ॉल्ट रूप से छोड़ देता हूं –

उत्तर

5

मैं बिल्कुल एक ही समस्या का सामना करना पड़ और मिला है बहुत बसंत स्रोत कोड डीबगिंग मज़ा। तो मूल कारण यह है: वसंत गिनती क्वेरी पर संकेत लागू कर रहा है जो उस त्रुटि को जन्म देता है।

QueryDslJpaRepository.java:

@Override 
public Page<T> findAll(Predicate predicate, Pageable pageable) { 

    JPQLQuery countQuery = createQuery(predicate); 
    JPQLQuery query = querydsl.applyPagination(pageable, createQuery(predicate)); 
.... 
} 

protected JPQLQuery createQuery(Predicate... predicate) { 

    JPAQuery query = querydsl.createQuery(path).where(predicate); 
    CrudMethodMetadata metadata = getRepositoryMethodMetadata(); 

    if (metadata == null) { 
     return query; 
    } 

    LockModeType type = metadata.getLockModeType(); 
    query = type == null ? query : query.setLockMode(type); 

    for (Entry<String, Object> hint : getQueryHints().entrySet()) { 
     query.setHint(hint.getKey(), hint.getValue()); 
    } 

    return query; 
} 

और समाधान नहीं है: अपने भंडार इंटरफ़ेस करने के लिए एक समर्थन इंटरफेस बनाने और इसे क्वेरी निर्माण तर्क अधिभावी लागू। यहां मेरा मध्यरात्रि कार्यान्वयन है (यह केवल एक प्रोटोटाइप है जिसे पाठ्यक्रम में सुधार किया जाना चाहिए)

public interface SomeRepository extends JpaRepository<SomeEntity, Long>, QueryDslPredicateExecutor<SomeEntity>, SomeRepositoryCustom { 
} 


public interface SomeRepositoryCustom { 
    Page<SomeEntity> findAll(Predicate predicate, Pageable pageable); 
} 

public class SomeRepositoryImpl extends SimpleJpaRepository<SomeEntity, Long> 
    implements SomeEntityRepositoryCustom 
{ 
    private final EntityManager entityManager; 
    private final EntityPath<SomeEntity> path; 
    private final PathBuilder<SomeEntity> builder; 
    private final Querydsl querydsl; 

    @Autowired 
    public SomeRepositoryImpl(EntityManager entityManager) { 
     super(SomeEntity.class, entityManager); 


     CrudMethodMetadata metadata = getRepositoryMethodMetadata(); 
     this.entityManager = entityManager; 
     this.path = SimpleEntityPathResolver.INSTANCE.createPath(SomeEntity.class); 
     this.builder = new PathBuilder<>(path.getType(), path.getMetadata()); 
     this.querydsl = new Querydsl(entityManager, builder); 
    } 

    @Override 
    public Page<SomeEntity> findAll(Predicate predicate, Pageable pageable) { 
     JPAQuery countQuery = createQuery(predicate); 
     JPAQuery query = (JPAQuery) querydsl.applyPagination(pageable, createQuery(predicate)); 

     query.setHint(EntityGraph.EntityGraphType.LOAD.getKey(), 
      entityManager.getEntityGraph("YOUR GRAPH KEY")); 

     Long total = countQuery.count(); 
     List<SomeEntity> content = total > pageable.getOffset() ? query.list(path) : 
      Collections.<SomeEntity> emptyList(); 

     return new PageImpl<>(content, pageable, total); 
    } 

    private JPAQuery createQuery(Predicate predicate) { 
     return querydsl.createQuery(path).where(predicate); 
    } 

} 

ऐसा लगता है कि यह एक बग है और मैं वसंत जेपीए jira करने के लिए इसे प्रस्तुत करने के लिए जा रहा हूँ।

+0

इसे साझा करने के लिए बहुत बहुत धन्यवाद, मैं अगले परियोजनाओं के लिए एक नज़र डालें और बचाऊंगा! –

संबंधित मुद्दे