2014-07-14 2 views
1
내가 Hibernate4을 사용하고

, 그리고 Hibernate는 항상 삽입하려고하는 대신에 업데이트되지 3.최대 절전 모드 4 : 될 saveOrUpdate 예외

봄.

내 엔티티 클래스 :

@Entity 
@Table(name="APP_SERVER") 
public class ServerEntity { 

    @Id 
    @GeneratedValue(generator="seq_item", strategy = GenerationType.SEQUENCE) 
    @SequenceGenerator(name="seq_item",sequenceName="SEQ_APP_SERVER") 
    @Column(name="APP_SERVER_KEY") 
    private int app_server_key; 

    @Column(name="SYS_ID" , unique=true) 
    private String sys_id; 
    @Column(name="SERVER_NAME") 
    private String server_name; 
} 

내 서비스 클래스 :

@Service("ServerService") 
public class ServerServiceImpl implements ServerService{ 

// other code 
    @Override 
    @Transactional 
    public void addServer(ServerEntity server) { 
    serverDao.addServer(server); 

    } 
} 

다오 등급 :

@Repository("ServerDao") 
public class ServerDaoImpl implements ServerDao { 
    @Override 
    public void addServer(ServerEntity server) { 
    try{ 
     this.sessionFactory.getCurrentSession().saveOrUpdate(server); 
     }catch (Exception e) { 
     e.printStackTrace(); 
     } 
    } 
} 

다른 클래스 :

ServerEntity serverEntity = new ServerEntity(); 
serverEntity.setSys_id((result.getSysId())); 
serverEntity.setServer_name(result.getName()); 

ServerService serverService = (ServerService)   
applicationContext.getBean("ServerService"); 
serverService.addServer(serverEntity); 

테이블 :

CREATE TABLE "APP_SERVER" 
    (   "APP_SERVER_KEY" NUMBER NOT NULL ENABLE, 
       "CMDB_SYS_ID" VARCHAR2(64) NOT NULL UNIQUE ENABLE , 
       "SERVER_NAME" VARCHAR2(255) NOT NULL ENABLE, 
    PRIMARY KEY ("APP_SERVER_KEY") ENABLE 
    ) ; 

예외가 오는 : 최대 절전 모드 대신 업데이트의 삽입 왜 여기

05:36:40,292 INFO [STDOUT] Hibernate: insert into USER.APP_SERVER (SYS_ID,SERVER_NAME) values (?, ?) 
05:36:40,558 INFO [STDOUT] WARN : org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 1, SQLState: 23000 
05:36:40,558 INFO [STDOUT] ERROR: org.hibernate.engine.jdbc.spi.SqlExceptionHelper - ORA-00001: unique constraint (USER.APP_SERVER_UK1) violated 
05:36:40,684 INFO [STDOUT] Exception occured... org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [OIMUSER.SPP_SERVER_UK1]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement 

. sys_id은 이미 데이터베이스에 존재하며 sys-id가 이미 존재하기 때문에 최대 절전 모드로 업데이트하려고합니다.

코드를 변경해야합니다. 제발 제안 해주세요. 당신의 객체가이를 조작 개체의 프록시 &를로드 영속 상태에있는 경우

+0

질문에 혼란이 있다면 pls 알려주세요 – VJS

+0

'OIMUSER.SPP_SERVER_UK1' 제약 조건은 무엇입니까? 또한 ID 열의 문자열 유형이 왜입니까? 엔티티에는 3 개의 필드가 있는데, 매핑을 수행하는 "다른 클래스"는 그 경우 기본 키인 실제 'int app_server_key;'ID와 관련이 없습니다. 그것이 새로운 엔티티이고 삽입 될 이유입니다. 또한 다음과 같은 것이 필요합니다 :'serverEntity.setApp_server_key ((result.getApp_server_key()));'. 말할 것도없이 코드 스타일이 좋지 않으므로 코드 스타일을 더 명확하고 쉽게 읽을 수 있어야합니다. – Vaelyr

+0

@ Vaelyr : 1) app_server_key는 @SequenceGenerator에 의해 생성되는 기본 키입니다. 2) sys_id는 영숫자를 포함하고 있기 때문에 varchar2입니다. 3) SPP_SERVER_UK1 : DB 측에서는 고유 한 제약 조건으로 sys_id를 적용했습니다 ... 이해해 주시기 바랍니다. – VJS

답변

0

는이

Student ss = new Student(); 
Student get = (Student) s.load(Student.class, new Long(id)); 
get.setDegree(newDegree); 
tx.commit(); 

뭔가를 시도 할 수 있습니다. 일단 조작이 완료되면 트랜잭션을 커밋하십시오. 개체는 최대 절전 모드로 자동 업데이트됩니다.

이것은 아마도 올바른의 미스에 뒤 Hibernate Load() Example

0

에서보세요 기능 느릅 나무가 최대 절전 모드가는 것입니다 알 수없는 즉, 당신의 entites의 ID를 기반으로해야 동일 같은 기록

관련 문제