Hibernate cascade many to many creates duplicates in child reference

ぃ、小莉子 提交于 2019-12-11 09:46:52

问题


Ok so I'm new to hibernate. The question is about cascade many-to-many, avoiding to add duplicate values. So I follow this example. tutorialspoint hibernate many to many mapping

The problem is this, that if I run program twice it adds duplicate values to certificate table.

After I insert values to employee table. It cascades and inserts values to certificate table:

id certificate_name
1   PMP
2   MBA
3   MCA

After I run this example second time it does the same actions.

id certificate_name
1   PMP
2   MBA
3   MCA
4   PMP
5   MBA
6   MCA

But then certificate table has dublicate values. Values 4-6 are the same as 1-3.

I tried to add unique constraint on table certificate, but then I get this error:

ERROR: Duplicate entry 'PMP' for key 'certificate_name_UNIQUE'

How can I insert values not to duplicate them. Can this be avoided, or do I have to use other techniques to do that.

Also add pom.xml if needed to use jars.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>testHibernateCascade2</groupId>
  <artifactId>testHibernateCascade2</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.3.6.Final</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.31</version>
    </dependency>
  </dependencies>  

</project>

回答1:


Ok, I found the solution. First I check if such certificate already exists in DB using HQL and Criteria.

public int getID(String certificateName){

    Criteria cr = session.createCriteria(Certificate.class);
    cr.add(Restrictions.eq("certificate", certificateName));
    List<?> results = cr.list();
    if (results.isEmpty()){ 
        return -1;}
        else{
            Iterator<?> iterator = results.iterator();
            Certificate certificate = (Certificate) iterator.next();
            return certificate.getId();
        }
}

Second if certificate_name is not in certificate table I create new object, else I add existing.

    ...
    String [] userCertificates = {"BKA","RRA","DMA"};

    HashSet<Certificate> certificatesSet = new HashSet<Certificate>();

    for (int i = 0; i<userCertificates.length;i++){
        int id = getID(userCertificates[i]);
        if (id == -1){
            certificatesSet.add(new Certificate(userCertificates[i]));
        }
        else{
            certificatesSet.add((Certificate) cs.getObject(Certificate.class, id));
        }
    }
    ...

And that worked very well.



来源:https://stackoverflow.com/questions/25994043/hibernate-cascade-many-to-many-creates-duplicates-in-child-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!