2021年5月5日星期三

In @OneToMany, can 'Many' have a unique key of 'One'?

I want to add Username to the ProductInfo table.

-- spring boot JPA (productInfo table)

@ManyToOne  @JoinColumn(name = "user_id")  private User user;  

-- spring boot JPA (User table)

@OneToMany(cascade = CascadeType.ALL)  private Set<ProductInfo> productInfos = new HashSet<>();  

-- spring boot controller

@PutMapping(value = {"/products/{id}", "/productSetting/{id}", "/productList/{id}"})  public ResponseEntity<ProductInfo> updateUser(@PathVariable("id") long id, @RequestBody ProductInfo productInfo) {    Optional<ProductInfo> productData = productInfoRepository.findById(id);      if (productData.isPresent()) {      ProductInfo _ProductInfo = productData.get();      _ProductInfo.setUser(productInfo.getUser());      _ProductInfo.setProductType(productInfo.getProductType());      _ProductInfo.setProductSize(productInfo.getProductSize());      _ProductInfo.setProductBios(productInfo.getProductBios());      _ProductInfo.setProductCpu(productInfo.getProductCpu());      _ProductInfo.setProductDate(productInfo.getProductDate());      _ProductInfo.setProductFac(productInfo.getProductFac());      _ProductInfo.setProductHdd(productInfo.getProductHdd());      _ProductInfo.setProductNum(productInfo.getProductNum());      _ProductInfo.setProductOs(productInfo.getProductOs());      _ProductInfo.setProductRam(productInfo.getProductRam());      _ProductInfo.setProductResolution(productInfo.getProductResolution());      _ProductInfo.setProductSecurity(productInfo.getProductSecurity());      _ProductInfo.setProductSn(productInfo.getProductSn());      _ProductInfo.setProductSsd(productInfo.getProductSsd());      _ProductInfo.setProductSsdType(productInfo.getProductSsdType());      _ProductInfo.setProductUseYn(productInfo.isProductUseYn());      return new ResponseEntity<>(productInfoRepository.save(_ProductInfo), HttpStatus.OK);    } else {      return new ResponseEntity<>(HttpStatus.NOT_FOUND);    }  }  

-- vue.js (script)

updateProductUserAdd(status) {    var data = {      id:this.currentProductInfo.id,      productType: this.currentProductInfo.productType,      productBios: this.currentProductInfo.productBios,      productCpu: this.currentProductInfo.productCpu,      productDate: this.currentProductInfo.productDate,      productFac: this.currentProductInfo.productFac,      productHdd: this.currentProductInfo.productHdd,      productNum: this.currentProductInfo.productNum,      productOs: this.currentProductInfo.productOs,      productRam: this.currentProductInfo.productRam,      productResolution: this.currentProductInfo.productResolution,      productSn: this.currentProductInfo.productSn,      productSsd: this.currentProductInfo.productSsd,      productSsdType: this.currentProductInfo.productSsdType,      productSecurity: this.currentProductInfo.productSecurity,      productUseYn: status,      user: {        username: this.currentUser.username      }    };    ProductInfoDataService.update(this.currentProductInfo.id, data)      .then(response => {        this.currentProductInfo = response.data;        this.currentProductInfo = status;        console.log(this.currentProductInfo);        this.message = 'success';      })      .catch(e => {        console.log(e);      });  },  

user Table

productInfo Table

Relationship table between User and ProductInfo

This will give you an error.

>> error Console

>> Network window

When I look at the network window, it becomes what I want. However, an error occurs.

Is there a way to put only the username column in the productInfo table?

++) terminal error

2021-05-06 10:56:21.052 ERROR 20212 --- [nio-8050-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.bezkoder.springjwt.models.ProductInfo.user -> com.bezkoder.springjwt.models.User; nested exception is java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.bezkoder.springjwt.models.ProductInfo.user -> com.bezkoder.springjwt.models.User] with root cause

org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.bezkoder.springjwt.models.ProductInfo.user -> com.bezkoder.springjwt.models.User

https://stackoverflow.com/questions/67410689/in-onetomany-can-many-have-a-unique-key-of-one May 06, 2021 at 09:19AM

没有评论:

发表评论