本文介绍了Hibernate级联多对多在子引用中创建副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我对hibernate很陌生。
问题是关于级联多对多,避免添加重复值。
所以我遵循这个例子。



问题是,如果我运行程序两次,它会将重复值添加到证书表。



值给员工表。它级联并向证书表插入值:

  id证书名称
1 PMP
2 MBA
3 MCA

在我运行这个例子后,行动。

  id证书名称
1 PMP
2 MBA
3 MCA
4 PMP
5 MBA
6 MCA

但是证书表具有共享值。值为4-6和1-3一样。



我试图在表格证书上添加唯一约束,但是我得到这个错误:

我如何插入值不复制它们。
这可以避免,还是我必须使用其他技术来做到这一点。



如果需要使用jar,请添加pom.xml。

  < project xmlns =http://maven.apache.org/POM/4.0.0xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi: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>
<配置>
< source> 1.6< / source>
< target> 1.6< / target>
< / configuration>
< / plugin>
< / plugins>
< / build>

<依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-core< / artifactId>
< version> 4.3.6.Final< / version>
< /依赖关系>

< dependency>
< groupId> mysql< / groupId>
< artifactId> mysql-connector-java< / artifactId>
< version> 5.1.31< / version>
< /依赖关系>
< /依赖关系>

< / project>


解决方案

好的,我找到了解决方案。
首先,我使用HQL和Criteria检查数据库中是否存在这样的证书。
$ b

  public int getID(String certificateName) {

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






其次如果certificate_name不在 certificate 表我创建新对象,否则我添加现有的。

  ... 
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));
}
}
...

好。


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:

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>
解决方案

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.

这篇关于Hibernate级联多对多在子引用中创建副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 14:16