Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 级联合并不能正常工作_Java_Spring_Hibernate_Jpa - Fatal编程技术网

Java 级联合并不能正常工作

Java 级联合并不能正常工作,java,spring,hibernate,jpa,Java,Spring,Hibernate,Jpa,我创建了以下代码: Sale sale = new Sale(); sale = saleService.create(sale); Company company = new Company("companyName"); Vendor vendor = new Vendor("name", company); sale.setVendor(vendor); try { sale = saleService.update(sale); } catch (Enti

我创建了以下代码:

Sale sale = new Sale();
sale = saleService.create(sale);

Company company = new Company("companyName");

Vendor vendor = new Vendor("name", company);

sale.setVendor(vendor);

try {
        sale = saleService.update(sale);
    } catch (EntityNotFoundException ex) {
        System.err.println("");
    }

Product product = new Product("name", company);
sale.setProduct(product);

try {
        sale = saleService.update(sale);
    } catch (EntityNotFoundException ex) {
        System.err.println("");
    }
@Transactional
public Sale create(Sale entity) {
    Sale created = entity;
    saleRepository.saveAndFlush(created);
    return created;
}

@Transactional(rollbackFor = EntityNotFoundException.class)
public Calibration update(Calibration entity) throws EntityNotFoundException {

   if (!calibrationRepository.exists(entity.getId())) {
        throw new EntityNotFoundException();
    }


    return calibrationRepository.saveAndFlush(entity);
}
此外,销售与供应商密切相关:

@ManyToOne(cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, 
 targetEntity = Vendor.class)
private Vendor vendor;
与公司合作的供应商和产品:

@ManyToOne(cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, 
 targetEntity = Company.class)
private Company company;
SalesService具有以下代码:

Sale sale = new Sale();
sale = saleService.create(sale);

Company company = new Company("companyName");

Vendor vendor = new Vendor("name", company);

sale.setVendor(vendor);

try {
        sale = saleService.update(sale);
    } catch (EntityNotFoundException ex) {
        System.err.println("");
    }

Product product = new Product("name", company);
sale.setProduct(product);

try {
        sale = saleService.update(sale);
    } catch (EntityNotFoundException ex) {
        System.err.println("");
    }
@Transactional
public Sale create(Sale entity) {
    Sale created = entity;
    saleRepository.saveAndFlush(created);
    return created;
}

@Transactional(rollbackFor = EntityNotFoundException.class)
public Calibration update(Calibration entity) throws EntityNotFoundException {

   if (!calibrationRepository.exists(entity.getId())) {
        throw new EntityNotFoundException();
    }


    return calibrationRepository.saveAndFlush(entity);
}
它还被标注为
@Service
。存储库是一个实现
JpaRepository
的接口

我得到一个错误,说公司的
名称
属性是重复的,不能重复。我很清楚,代码试图在数据库中创建另一家公司。如何合并或使用已创建的文件


谢谢

您不应该创建新实例,而是从上下文中读取要更新的实例,并使用该实例进行更改。什么的新实例?好的,我知道了…我应该从数据库中再次获取该公司。把它作为答案,我会把它设置为正确的。你不应该创建一个新实例,而应该从上下文中读取你要更新的实例,并使用该实例进行更改。什么的新实例?好的,我得到了它…我应该从数据库中再次获得该公司。把它作为答案,我会把它设置为正确的。你不应该创建一个新实例,而应该从上下文中读取你要更新的实例,并使用该实例进行更改。什么的新实例?好的,我得到了它…我应该从数据库中再次获得该公司。把它作为答案,我会把它设置为正确答案。