0

I have two tables: STUDY and Diagnosis, each study has a unique diagnosis, and the diagnosis' name is unique, my code is:

@Data
@Entity
@Table(name = "diagnosis")
public class Diagnosis {
    @Id
    @GeneratedValue(strategy =GenerationType.AUTO)
    private Integer id;

    @Column(unique = true)
    private String name;

    @Column
    private String stage;

    @Column
    private String description;

}

the STUDY is :

@Data
@Entity
@Table(name = "study")
public class Study {

    @Id
    @Column
    private Double uid;

    @Column
    private String manufacture;

    @Column
    private Date study_datatime;

    @ManyToOne(cascade = {CascadeType.PERSIST})
    @JoinColumn(name = "diagnosis_id")
    private Diagnosis diagnosis;

}

but I got the exception:org.h2.jdbc.JdbcSQLException: Unique index or primary key violation: "CONSTRAINT_INDEX_E ON PUBLIC.DIAGNOSIS(NAME) VALUES ('Normal', 1)" I'm using H2 database 1.4.190, I don't know the reason.

Example test:

    public static void main(String[] args) throws Exception{
        Diagnosis diagnosis=new Diagnosis();
        diagnosis.setName("normal");

        Study study=new Study();
        Study study1=new Study();
        study.setDiagnosis(diagnosis);
        study.setUid(1.0);

        study1.setDiagnosis(diagnosis);
        study1.setUid(2.0);

        SessionFactory sessionFactory=new     Configuration().configure().buildSessionFactory();
        Session session=sessionFactory.openSession();
        Transaction transaction=session.beginTransaction();
        session.persist(study);
        session.persist(study1);
        transaction.commit();
        session.clear();
        session.close();
}

actual code:

    ....
    diagnosisName = row.getCell(6).getStringCellValue();
    if (diagnosisNameMap.keySet().contains(diagnosisName) == false)
      {
       diagnosis.setName(diagnosisName);
       diagnosisNameMap.put(diagnosisName,diagnosis);
       study.setDiagnosis(diagnosis);
       session.persist(diagnosis);
      }else{
       diagnosis=diagnosisNameMap.get(diagnosisName);
       study.setUid(Double.parseDouble(row.getCell(0).toString()));
       study.setDiagnosis(diagnosis);
     }

     session.persist(study);
nmdxpc
  • 1
  • 4
  • please post your database table screenshots as well. – jarvo69 Aug 22 '16 at 04:03
  • Well, that just means you're trying to insert a diagnosis with the same name as another one, although they should be unique. Thankfully, the database constraint forbids you to do that, since you shouldn't. – JB Nizet Aug 22 '16 at 05:55
  • @JB Nizet, yes, that's the reason, now I use this: @SQLInsert(sql="Insert into diagnosis (id,description,name,stage) values(?,?,?,?) on duplicate key UPDATE stage=null", a new exception was thrown: Referential integrity constraint violation: "FK68B0DC974FAFF73: PUBLIC.STUDYFOREIGN KEY(DIAGNOSIS_ID) REFERENCES PUBLIC.DIAGNOSIS(ID) (1)"; SQL statement: insert into study (diagnosis_id, manufacture, study_datatime, uid) values (?, ?, ?, ?), How Can I persist a unique name into diagnosis when the study references its id? – nmdxpc Aug 22 '16 at 08:56
  • I'm confused. The original exception you got, as I said, is perfectly normal. Why are you surprised you got it? What are you trying to achieve? What's your code? – JB Nizet Aug 22 '16 at 09:28
  • @JBNizet, I need to persist a lot data to table STUDY and DIAGNOSIS, each study has a diagnosis result, like this: study1-----cancer, study2--normal, study3---cancer. so there are duplicate data for the DIAGNOSIS, but I want to persist one, that is many study reference to one diagnosis result. I a new for hibernate, so I'm confused. I paste a example in the question,, but I have so many data, I try to use a map in the actual code, but I got a exception:org.hibernate.PersistentObjectException: detached entity passed to persist: com.toshiba.POJO.Diagnosis – nmdxpc Aug 23 '16 at 01:10
  • @JBNizet, oh, I find the solution[link](http://stackoverflow.com/a/29235227/6132267), but anyway, thank you so much!! If you have suggestion about my code for me, pls tell me, I'm new so I will appreciate for any suggestion! thank you – nmdxpc Aug 23 '16 at 01:51

0 Answers0