Once you have created a core project as shown in last blog. Now in this tutorial we will check various methods for various SELECT queries. So we will check IN, GREATER THAN, LESS THAN etc in this blog. Following is the JUnit Test class
Following is the repository
Service interface
Service implementation
Following log is for reference.
2015/08/15T23:38:49,203 DEBUG [com.techcielo.sphr.core.test.CountryServiceTest] [testFindByColumn] - Finding Records for GNP > 50000
Hibernate: select countrybea0_.code as code1_0_, countrybea0_.gnp as gnp2_0_, countrybea0_.name as name3_0_ from Country countrybea0_ where countrybea0_.gnp>?
2015/08/15T23:38:49,382 DEBUG [com.techcielo.sphr.core.test.CountryServiceTest] [testFindByPartialCode] - Finding Records for Code LIKE AR)
Hibernate: select countrybea0_.code as code1_0_, countrybea0_.gnp as gnp2_0_, countrybea0_.name as name3_0_ from Country countrybea0_ where countrybea0_.code like ?
2015/08/15T23:38:49,393 DEBUG [com.techcielo.sphr.core.test.CountryServiceTest] [testFindSingle] - Finding Single Record by Code = IND
Hibernate: select countrybea0_.code as code1_0_, countrybea0_.gnp as gnp2_0_, countrybea0_.name as name3_0_ from Country countrybea0_ where countrybea0_.code=?
2015/08/15T23:38:49,402 DEBUG [com.techcielo.sphr.core.test.CountryServiceTest] [testFindByCodeList] - Finding Records for Code in (IND,USA)
Hibernate: select countrybea0_.code as code1_0_, countrybea0_.gnp as gnp2_0_, countrybea0_.name as name3_0_ from Country countrybea0_ where countrybea0_.code in (? , ?)
2015/08/15T23:38:49,415 DEBUG [com.techcielo.sphr.core.test.CountryServiceTest] [testFindByCodeAndName] - Finding Records for Code=IND and CountryName=India)
Hibernate: select countrybea0_.code as code1_0_, countrybea0_.gnp as gnp2_0_, countrybea0_.name as name3_0_ from Country countrybea0_ where countrybea0_.code=? and countrybea0_.name=?
For exhaustive list please refer spring JPA reference document
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.techcielo.sphr.core.test; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.context.support.ClassPathXmlApplicationContext; | |
import com.techcielo.sphr.core.bean.CountryBean; | |
import com.techcielo.sphr.core.service.CountryService; | |
import junit.framework.TestCase; | |
public class CountryServiceTest extends TestCase{ | |
private Logger logger = LoggerFactory.getLogger(CountryServiceTest.class); | |
ApplicationContext ctx = new ClassPathXmlApplicationContext("countries-app-context.xml"); | |
CountryService svc = ctx.getBean(CountryService.class); | |
public void testFindSingle(){ | |
logger.debug("Finding Single Record by Code = IND"); | |
CountryBean country = svc.getCountryByCode("IND"); | |
assertNotNull(country); | |
assertEquals("India", country.getName()); | |
} | |
public void testFindByColumn(){ | |
logger.debug("Finding Records for GNP > 50000"); | |
List<CountryBean> countryList = svc.findCountryByGnp(50000.0); | |
assertNotNull(countryList); | |
assertEquals(51,countryList.size()); | |
} | |
public void testFindByCodeList(){ | |
logger.debug("Finding Records for Code in (IND,USA)"); | |
List<String> codeList = new ArrayList<String>(); | |
codeList.add("IND"); | |
codeList.add("USA"); | |
List<CountryBean> countryList = svc.findByCountryCode(codeList); | |
assertNotNull(countryList); | |
assertEquals(2,countryList.size()); | |
} | |
public void testFindByCodeAndName(){ | |
logger.debug("Finding Records for Code=IND and CountryName=India)"); | |
List<CountryBean> countryList = svc.findCountryByCodeAndName("IND", "India"); | |
assertNotNull(countryList); | |
assertEquals(1, countryList.size()); | |
} | |
public void testFindByPartialCode(){ | |
logger.debug("Finding Records for Code LIKE AR)"); | |
List<CountryBean> countryList = svc.findCountryLike("AR%"); | |
assertNotNull(countryList); | |
assertEquals(3, countryList.size()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.techcielo.sphr.core.repo; | |
import java.util.List; | |
import org.springframework.data.jpa.repository.JpaRepository; | |
import com.techcielo.sphr.core.bean.CountryBean; | |
public interface CountryRepo extends JpaRepository<CountryBean, String>{ | |
public CountryBean findByCode(String code); | |
public List<CountryBean> findByGnpGreaterThan(Double gnp); | |
public List<CountryBean> findByCodeIn(List<String> codeList); | |
public List<CountryBean> findByCodeAndName(String code,String name); | |
public List<CountryBean> findByCodeLike(String code); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.techcielo.sphr.core.service; | |
import java.util.List; | |
import com.techcielo.sphr.core.bean.CountryBean; | |
public interface CountryService { | |
public CountryBean getCountryByCode(String code); | |
public List<CountryBean> findCountryByGnp(Double gnp); | |
public List<CountryBean> findByCountryCode(List<String> codeList); | |
public List<CountryBean> findCountryByCodeAndName(String code,String name); | |
public List<CountryBean> findCountryLike(String code); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.techcielo.sphr.core.service.impl; | |
import java.util.List; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Service; | |
import com.techcielo.sphr.core.bean.CountryBean; | |
import com.techcielo.sphr.core.repo.CountryRepo; | |
import com.techcielo.sphr.core.service.CountryService; | |
@Service | |
public class CountryServiceImpl implements CountryService{ | |
@Autowired | |
CountryRepo repo; | |
public CountryBean getCountryByCode(String code) { | |
return repo.findByCode(code); | |
} | |
public List<CountryBean> findCountryByGnp(Double gnp) { | |
return repo.findByGnpGreaterThan(gnp); | |
} | |
public List<CountryBean> findByCountryCode(List<String> codeList){ | |
return repo.findByCodeIn(codeList); | |
} | |
public List<CountryBean> findCountryByCodeAndName(String code, String name) { | |
return repo.findByCodeAndName(code, name); | |
} | |
public List<CountryBean> findCountryLike(String code){ | |
return repo.findByCodeLike(code); | |
} | |
} |
Following log is for reference.
2015/08/15T23:38:49,203 DEBUG [com.techcielo.sphr.core.test.CountryServiceTest] [testFindByColumn] - Finding Records for GNP > 50000
Hibernate: select countrybea0_.code as code1_0_, countrybea0_.gnp as gnp2_0_, countrybea0_.name as name3_0_ from Country countrybea0_ where countrybea0_.gnp>?
2015/08/15T23:38:49,382 DEBUG [com.techcielo.sphr.core.test.CountryServiceTest] [testFindByPartialCode] - Finding Records for Code LIKE AR)
Hibernate: select countrybea0_.code as code1_0_, countrybea0_.gnp as gnp2_0_, countrybea0_.name as name3_0_ from Country countrybea0_ where countrybea0_.code like ?
2015/08/15T23:38:49,393 DEBUG [com.techcielo.sphr.core.test.CountryServiceTest] [testFindSingle] - Finding Single Record by Code = IND
Hibernate: select countrybea0_.code as code1_0_, countrybea0_.gnp as gnp2_0_, countrybea0_.name as name3_0_ from Country countrybea0_ where countrybea0_.code=?
2015/08/15T23:38:49,402 DEBUG [com.techcielo.sphr.core.test.CountryServiceTest] [testFindByCodeList] - Finding Records for Code in (IND,USA)
Hibernate: select countrybea0_.code as code1_0_, countrybea0_.gnp as gnp2_0_, countrybea0_.name as name3_0_ from Country countrybea0_ where countrybea0_.code in (? , ?)
2015/08/15T23:38:49,415 DEBUG [com.techcielo.sphr.core.test.CountryServiceTest] [testFindByCodeAndName] - Finding Records for Code=IND and CountryName=India)
Hibernate: select countrybea0_.code as code1_0_, countrybea0_.gnp as gnp2_0_, countrybea0_.name as name3_0_ from Country countrybea0_ where countrybea0_.code=? and countrybea0_.name=?