본문으로 바로가기

JPA NATIVE QUERY 사용법

category JPA 2023. 7. 3. 21:01
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence/orm" version="2.2">
    
    <named-native-query name="UserInfo.findBypwd" result-set-mapping="findUserInfoResult">
        <query>
		 <![CDATA[
            SELECT u.user_id as userId,
            u.password as password
            FROM USER_INFO u
            where 1=1 and
            u.password = :password
          ]]>
        </query>
        <!--<parameter name="password"/>-->
    </named-native-query>
    <sql-result-set-mapping name="findUserInfoResult">
      <constructor-result target-class="com.hwc.isl.loader.dbif.entity.UserInfo">
        <column name="userId" class="java.lang.String"/>
        <column name="password" class="java.lang.String"/>
      </constructor-result>
    </sql-result-set-mapping>
    
    
    
</entity-mappings>​
		// (내부 DB) 모든 SERVER,AGENT 정보 조회
		final List<Object[]> agentsWithServers = this.tbAgentInfoService.getAgentWithServer();
		System.out.println("agentsWithServers.size() : " + agentsWithServers.size());

		// (ORACLE) AGENT 정보 LOAD
		List<UserInfo> userInfoList = userInfoRepository.findBypwd(null);
		
		for(UserInfo userInfo : userInfoList)
		{
			System.out.println("agentsWithServers.size() : " + userInfo.toString());	
		}
package com.hwc.isl.loader.dbif.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.hwc.isl.loader.dbif.entity.UserInfo;

@Repository
public interface UserRepository extends JpaRepository<UserInfo, Long>{
	
	@Query(nativeQuery = true)
	public List<UserInfo> findBypwd(@Param("password") String passowrd);
}