2013-05-23 3 views
1

복합 기본 키 (indId, studentId)가있는 엔티티 학생이 있습니다. 이 테이블의 일부 행을 업데이트하려고합니다. 내 코드 :이 코드를 실행하면복합 기본 키를 사용하여 JPA에서 쿼리를 업데이트하십시오.

private EntityManager em = null; 

@PersistenceContext 
public void setEntityMgr(EntityManager em) { 
    this.em = em; 
} 

public void updateStudent(Student student){ 
    em.merge(student); 
} 

, 삽입 쿼리가 실행된다 (그리고 그에 따라 삽입하기 때문에 예외가 기본 키가 이미 존재하는 경우 발생합니다). 합성 기본 키를 사용할 때 JPA를 사용하여 업데이트 쿼리를 작성하는 방법을 모르겠습니다. 제안을 찾고 있습니다. 감사.

편집

@Entity 
@Table(name="student") 
public class Student { 
    private Integer studentId; 
    private Long indId; 
    private Integer field1; 
    private Integer field2; 

@Id 
@Column(name="student_id") 
public Integer getStudentId() { 
    return student; 
} 

@Id 
@Column(name="ind_id") 
public Long getIndId() { 
    return indId; 
} 

//rest of the getters and setters 
} 


public class StudentBean{ 
// properties 

    public void update(){ 
    Student student = new Student(); 
    student.setIndId(this.getIndId()); 
    student.setStudentId(this.getStudentId()); 
    . 
    . 

    // have a service class which I did not include 
    studentDao.updateStudent(student) 
    } 

    // getters and setters 

} 

@Repository("StudentDao") 
public class StudentDao{ 
    private EntityManager em = null; 

    @PersistenceContext 
    public void setEntityMgr(EntityManager em) { 
     this.em = em; 
    } 

    public void updateStudent(Student student){ 
      em.merge(student); 
    } 
} 


Stacktrace (where indId = 117 and studentId = 1) 

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '117-1' for key 'PRIMARY' 
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:513) 
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) 
at com.mysql.jdbc.Util.getInstance(Util.java:386) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1041) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052) 
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503) 
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664) 
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2815) 
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155) 
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458) 
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2006) 
... 98 more 
+1

엔티티의 코드와 매핑을 보여주세요. 무엇이 포함되어 있는지 알려주십시오. 실행중인 예외 및 SQL 쿼리의 전체 스택 추적을 표시하십시오. –

+0

수정 된 부분을보고 더 자세한 정보가 필요하면 알려주십시오. – mona

답변

1

당신의 Student 엔티티 정말 2 coulmns, 당신은 @EmbeddedId 주석을 사용하고있는이 개 필드를 유지하는 @Embeddable 클래스를 사용해야로 구성된 기본 키가없는 경우 기본 키.

관련 문제