2014-04-04 2 views
0

newEntity1이라는 엔티티가 포함 가능한 클래스 Leaverequest에 있습니다. 엔티티 클래스에서 Collectionofelements을 사용하고 있지만 다음과 같은 예외가 발생합니다. 아무도 그게 무슨 뜻인지 설명해 주시겠습니까?예외 : 키 'PRIMARY'에 대한 '1- x01'중복 항목

예외

Caused by: java.sql.BatchUpdateException: Duplicate entry '1-\x01' for key 'PRIMARY' 
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2028) 
    at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1451) 
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48) 
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246) 
    ... 8 more 
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1-\x01' for key 'PRIMARY' 

법인 :

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package com.treamis.entity; 

import java.io.Serializable; 
import java.util.LinkedHashSet; 
import java.util.Set; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import org.hibernate.annotations.CollectionOfElements; 

/** 
* 
* @author hyva 
*/ 
@Entity 
public class NewEntity1 implements Serializable { 

    private static final long serialVersionUID = 1L; 
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long id; 

    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    @CollectionOfElements 
    private Set<LeaveAppliedDetails> leaveAppliedDetails = new LinkedHashSet<LeaveAppliedDetails>(); 

    @Override 
    public String toString() { 
     return "com.treamis.entity.NewEntity1[ id=" + id + " ]"; 
    } 

    public Set<LeaveAppliedDetails> getLeaveAppliedDetails() { 
     return leaveAppliedDetails; 
    } 

    public void setLeaveAppliedDetails(Set<LeaveAppliedDetails> leaveAppliedDetails) { 
     this.leaveAppliedDetails = leaveAppliedDetails; 
    } 

    public boolean addLeaveAppliedDetails(LeaveAppliedDetails e) { 
     return leaveAppliedDetails.add(e); 
    } 

} 

테스터 등급 :

NewEntity1 newEntity = new NewEntity1(); 

LeaveAppliedDetails lad=new LeaveAppliedDetails(); 
lad.setHalfDay(true); 
newEntity.addLeaveAppliedDetails(lad); 
LeaveAppliedDetails lad1=new LeaveAppliedDetails(); 
lad1.setHalfDay(true); 
newEntity.addLeaveAppliedDetails(lad1); 
Session s=HibernateUtil.getSessionFactory().openSession(); 
s.save(newEntity); 
s.beginTransaction().commit(); 
+0

LeaveAppliedDetails에 대한 코드도 제공해야합니다. 나는 그 엔티티를 저장할 때 문제가 발생했다고 생각한다. – TondaCZE

답변

0

In these docs 다음을 읽을 수 있습니다 :

우리는 당신이 새로운 @ElementCollection 주석에 org.hibernate.annotations.CollectionOfElements @ 에서 마이그레이션하는 것이 좋습니다.

주석 @CollectionOfElements@ElementCollection으로 변경 한 다음 위 섹션을 단계별로 진행하시기 바랍니다.

NewEntity1 클래스에 데이터베이스에 아직 존재하지 않는 (기본 키는 알려져 있지 않음) LeaveAppliedDetails의 새 개체를 추가하려고합니다. 따라서 을 저장하여 NewEntity1에 추가하기 전에 새 LeaveAppliedDetails 개체를 검색하십시오.