2016-10-25 5 views
0

그래서 JPARepository를 사용하여 Postgres DB에 객체를 커밋하는 스프링 부트 애플리케이션이 있습니다. 내 저장소에 대한 코드는 다음과 같습니다 : 스프링 부트 JPARepository - postgres 테이블을 업데이트하는 방법

public interface TestObjectDao extends JPARepository<TestObject, Long> { 

List<TestObject> findByTestRefIdAndTestType(String testRefId, 
     Short testType); 

TestObject save(TestObject testObject); 
} 

내가 만들

, 내 서비스 구현에서 나는 방법을 "저장"을 사용 (인터페이스 위에 구현). 그러나 업데이트하려고하면 항목을 만들거나 업데이트하지 않습니다.

누구나 prostgres db에서 레코드를 업데이트하는 방법을 알려줄 수 있습니까?

@Component 
public class TestObjectServiceImpl implements TestObjectService { 

    @Inject 
    private TestObjectDao TestObjectDao; 

    @Override 
    public TestObjectResponse createTestObject(String testRefId, String testType, TestObject testObject) throws Exception{ 

    -- 
    -- 
    -- 

    try { 
      //update object 
      testObject = TestObjectDao.save(testObject); 
     } 
    } 
    catch (Exception ex) { 
     throw new Exception("Object could not be modified"); 
    } 

    TestObjectResponse testObjectResponse = new TestObjectResponse(); 
    testObjectResponse.setVal(testObject); 

    return testObjectResponse; 
    } 

} 

나는 모든 관련 게시물이 겪었지만 만족스러운 해결책을 얻을 didnt는 : 다음 업데이트를 사용하여 내 코드입니다.

빠른 답변을 보내 주시면 더 자세한 정보가 필요하시면 알려 주시기 바랍니다. 이 저장하거나 ID를 확인하여 엔티티를 생성 할 필요가 어떠했는지

감사

답변

0

봄 감지합니다.

try { 
     testObject = TestObjectDao.findOne(testRefId); 
     //update object 
     testObject = TestObjectDao.save(testObject); 
} 

섹션 2.2.1 참조하십시오 : 봄 제대로 개체를 채우는 것입니다 귀하의 경우

, 당신은 먼저 선택해야
http://docs.spring.io/spring-data/jpa/docs/1.4.1.RELEASE/reference/html/jpa.repositories.html

참고 만 일부 열 경우 업데이트해야하는 경우 훨씬 더 효율적으로 사용할 수 있습니다.

@Modifying 
@Query("update User u set u.firstname = ?1 where u.lastname = ?2") 
int setFixedFirstnameFor(String firstname, String lastname); 
+0

감사합니다. Alexey !!!! 너는 내 하루를 보냈다. 레코드를 업데이트 중입니다. – user5423592

+0

듣기가 좋습니다. 그런 다음 정답으로 표시하십시오. –

관련 문제