2017-02-26 5 views
0

querydsl 및 spring-data를 사용하여 mongodb 데이터베이스에서 결과를 찾으려고 할 때 MyDocument 클래스에서 MyInterfaceImpl 유형을 사용하면 몇 가지 결과가 발생합니다. MyInterface를 사용하면 결과가 없습니다. MongoRepository를 사용하여 MyInterface로 결과를 얻습니다. QueryDSL과 Java의 특정 인터페이스가 있습니까? 내가 놓친 것이 있습니까? Querydsl 및 Java에서 Mongodb와 인터페이스

나는 다음과 같은 게시물을 찾았지만 여전히하거나하는 MyInterface에 @QuerySupertype없이 같은 결과를 얻을 :

How queryDSL works with interface?

package com.myapp.bean.subfolder; 

... 

@Document 
public class MyDocument implements Comparable<MyDocument>, IMyDocument{ 

    private MyInterface myInterface; 

    @Id 
    private String id; 

    @CreatedDate 
    private Date createdDate; 

    public void setCreatedDate(Date createdDate) { 
    this.createdDate = createdDate; 
    } 

    public Date getCreatedDate() { 
     return createdDate; 
    } 

    public void setMyInterface(MyInterface myInterface) { 
     this.myInterface = myInterface; 
    } 

    public MyInterface getMyInterface() { 
     return myInterface; 
    } 

    ... 
} 

하는 MyInterface :

package com.myapp.bean; 

@QuerySupertype 
public interface MyInterface { 

    public MyInterfaceImplBase getMyInterfaceImplBase(); 
    public void setMyInterfaceImplBase(MyInterfaceImplBase myInterfaceImplBase); 
    public String getIdExterne(); 
    public void setIdExterne(String idExterne); 
} 

MyInterfaceImpl :

package com.myapp.bean.subfolder; 

... 

@Document 
public class MyInterfaceImpl implements MyInterface{ 
    private MyInterfaceImplBase myInterfaceImplBase; 
    @Id 
    private String idExterne; 

    public MyInterfaceImplBase getMyInterfaceImplBase() { 
     return myInterfaceImplBase; 
    } 

    public void setMyInterfaceImplBase(MyInterfaceImplBase myInterfaceImplBase) { 
     this.myInterfaceImplBase = myInterfaceImplBase; 
    } 

    public String getIdExterne() { 
     return idExterne; 
    } 

    public void setIdExterne(String idExterne) { 
     this.idExterne = idExterne; 
    } 
} 
01 23,516,

MyInterfaceImplBase.java :

package com.myapp.bean; 

... 

@Document 
public class MyInterfaceImplBase { 
    @Id 
    private String internalId; 

    public String getInternalId() { 
     return internalId; 
    } 

    public void setInternalId(String internalId) { 
     this.internalId = internalId; 
    } 
} 

MyDocumentRepository.java :

package com.myapp.repository; 

... 

public interface MyDocumentRepository extends MongoRepository<MyDocument, String>, QueryDslPredicateExecutor<MyDocument> { 
    Collection<MyDocument> findByMyInterface(MyInterface myInterface); 
} 

MyService.java :

package com.myapp.service; 

--- 

@Service 
public class MyService { 
    @Autowired 
    MyDocumentRepository myDocumentRepository; 

    public Iterable<MyDocument> findById(String id){ 
     BooleanExpression booleanExpression = QMyDocument.myDocument.myInterface.externalId.eq("1"); 
     return myDocumentRepository.findAll(booleanExpression); 
    } 

}

MyServiceTest.java :

,745 MongoDB의에서
package com.myapp.service; 

... 

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes={MongoConfig.class, ServiceConfig.class, BeanConfig.class, FactoryConfig.class}) 
public class MyServiceTest { 
    @Autowired 
    MyService myService; 

    @Before 
    public void setup() { 

    } 

    @Test 
    public void findByObjectIdInDB(){ 
     String id = 1; 
     Iterable<MyDocument> iterableResult = myService.findById(id); 
     assertEquals(1,Lists.newArrayList(iterableResult).size()); 
    } 
} 

데이터 :

{ "_id" : ObjectId("58b09c55a7986c0ce0407a76"), "_class" : "com.myapp.bean.subfolder.MyDocument", "myInterface" : { "_class" : "com.myapp.bean.subfolder.MyInterfaceImpl", "_id" : "1", "myInterfaceImplBase" : { "_id" : "2" }}, "createdDate" : ISODate("2017-02-24T20:49:25.891Z") } 

내가 MyServiceTest.java 실행 : 여기

내 pom.xml 파일에서 관련 정보의 booleanExpression = "myDocument.myInterface.idExterne = 1":

<dependency> 
    <groupId>org.mongodb</groupId> 
    <artifactId>mongo-java-driver</artifactId> 
    <version>3.3.0</version> 
</dependency> 

<dependency> 
    <groupId>org.springframework.data</groupId> 
    <artifactId>spring-data-mongodb</artifactId> 
    <version>1.9.4.RELEASE</version> 
</dependency> 

<dependency> 
    <groupId>com.querydsl</groupId> 
    <artifactId>querydsl-jpa</artifactId> 
    <version>4.1.4</version> 
</dependency> 

<dependency> 
    <groupId>com.querydsl</groupId> 
    <artifactId>querydsl-core</artifactId> 
    <version>4.1.4</version> 
</dependency> 

<dependency> 
    <groupId>com.querydsl</groupId> 
    <artifactId>querydsl-mongodb</artifactId> 
    <version>4.1.4</version> 
</dependency> 

<plugin> 
    <groupId>com.mysema.maven</groupId> 
    <artifactId>apt-maven-plugin</artifactId> 
    <version>1.1.3</version> 
    <executions> 
     <execution> 
      <goals> 
      <goal>process</goal> 
      </goals> 
      <configuration> 
      <outputDirectory>target/generated-sources/apt</outputDirectory> 

      <processor>org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor</processor> 
      </configuration> 
     </execution> 
    </executions> 

    <dependencies> 
     <dependency> 
      <groupId>com.querydsl</groupId> 
      <artifactId>querydsl-apt</artifactId> 
      <version>4.1.4</version> 
     </dependency> 
    </dependencies> 
</plugin> 

답변

0

답변을 찾았습니다.

QMyDocument.myDocument.myInterface.as(MyInterfaceImpl.class).externalId.eq("1"); 
: 문제는 인터페이스가 EntityPathBase 방법 등으로 주조 할 수 있고, I는

QMyDocument.myDocument.myInterface.externalId.eq("1"); 

을 실행할 수 있었다이면 MyService