【问题标题】:Hibernate ManyToOne syntax休眠多对一语法
【发布时间】:2021-02-11 10:52:58
【问题描述】:

我正在尝试编写使用 MSSQL + Hibernate/JPA 的代码,我可以在其中输入一些产品并在其上创建 FK,指代一家公司。

我的关系是这样的

    @OneToMany(mappedBy = "companyId")
    private Set<Product> product;
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "companyId")
    private Company companyId;

但不是采用我在 JSON 中发送的 FK,它会创建一家新公司

这是 JSON:

{
    "name":"Test",
    "price":"35.63",
    "productCompanyId":"10293", (this is the ID on company receipt, to make )
    "companyId":"2"
}

这里是我的实体

@Data
@Entity
@Table
@NoArgsConstructor
@AllArgsConstructor
public class Company {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    int id;

    @NotNull
    String name;

    @OneToMany(mappedBy = "companyId")
    private Set<Product> product;

    public Company(String name){
        this.name = name;
    }
}
@Data
@Entity
@Table
@NoArgsConstructor
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @NotNull
    int productCompanyId; //product code in company receipt, making easier later updates

    @NotNull
    String name;

    @NotNull
    java.math.BigDecimal price;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "companyId")
    private Company companyId;// identification of where it was purchased

    public Product(String name, java.math.BigDecimal price, int productCompanyId, Company companyId) {

        this.price = price;
        this.name = name;
        this.productCompanyId = productCompanyId;
        this.companyId = companyId;
    }

}

数据库: [1]:https://i.stack.imgur.com/WtRWR.jpg

id  name
1   TestCompany1
2   TestCompany2
3   2
id  name    price   product_company_id  company_id
1   Test    35.63   10293   3

我认为问题出在这里:

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    ProductRepository productRepository;

    @PostMapping
    public ResponseEntity<Product> insertProduct (@RequestBody ProductForm form){
        Product product = form.creationConverter();
        productRepository.save(product);
        return ResponseEntity.ok().build();
    }
}

@Data
public class ProductForm {

    @NotNull
    String name;

    @NotNull
    java.math.BigDecimal price;

    @NotNull
    int productCompanyId;

    @NotNull
    Company companyId;

    public Product creationConverter() {
        return new Product(name, price, productCompanyId, companyId);
    }

    public Product updateConverter(Integer id, ProductRepository productRepository) {
        Product product = productRepository.getOne(id);
        product.setName(this.name);
        product.setPrice(this.price);
        product.setProductCompanyId(this.productCompanyId);
        product.setCompanyId(companyId);
        return product;
    }
}

但我找不到将这个 companyId 设置为 int 的方法(我正在向控制器发送一个新的 objetc,预计会创建新公司,而不是仅仅链接它)

【问题讨论】:

  • 如何持久化 Product 实体,看看是否可以在产品保存期间设置公司。
  • 抱歉,没有完全理解您的问题。我正在使用 lombok 注释 (@Data) 和 JPA。我有另一个带有构造函数的类“表单”,可以使用 productRepository 保存。
  • 实体(产品)在提交以保存或保留之前如何创建。您应该在 .save() 或 .persist() 方法中传递实体。
  • @PrashantKatara 我刚刚用我昨天学到的东西更新了代码。不久前我开始编码,我并不熟悉用于引用类等的所有术语......
  • 好的没问题,参考一些YouTube视频清楚,最终当实体处于瞬态时,您需要将公司ID与产品数据一起设置为产品,然后执行persit( ) 或 save() 操作以进入持久状态,然后您可以将其提交并刷新到数据库。

标签: hibernate spring-data-jpa hibernate-mapping


【解决方案1】:

经过一些测试并阅读了很多东西后,我发现问题不是我的关系。但是我的 ProductForm.java

事实上,我修复它的方法非常简单。 首先我删除我的@ManyToOne 关系,因为它迫使我在我的产品中创建一个公司变量 只留下@OneToMany 在公司。 这样我就可以在我的产品中使用一个 int 变量“companyId”,并且数据库中的记录停止重复 =)

这是如何工作的:

产品:

@Data
@Entity
@Table
@NoArgsConstructor
public class Product {


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @NotNull
    int productCompanyId; //product code in company receipt, making easier later updates

    @NotNull
    String name;

    @NotNull
    java.math.BigDecimal price;

    @NotNull
    int companyId;// identification of where it was purchased

    public Product(String name, java.math.BigDecimal price, int productCompanyId, int companyId) {

        this.price = price;
        this.name = name;
        this.productCompanyId = productCompanyId;
        this.companyId = companyId;
    }

}

产品形式:

@Data
public class ProductForm {

    @NotNull
    String name;

    @NotNull
    java.math.BigDecimal price;

    @NotNull
    int productCompanyId;

    @NotNull
    int companyId;

    public Product creationConverter() {
        return new Product(name, price, productCompanyId, companyId);
    }

    public Product updateConverter(Integer id, ProductRepository productRepository) {
        Product product = productRepository.getOne(id);
        product.setName(this.name);
        product.setPrice(this.price);
        product.setProductCompanyId(this.productCompanyId);
        product.setCompanyId(companyId);
        return product;
    }
}

然后是有关系的公司:

@Data
@Entity
@Table
@NoArgsConstructor
@AllArgsConstructor
public class Company {


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    int id;

    @NotNull
    String name;

    @OneToMany(mappedBy = "companyId")
    private Set<Product> product;

    public Company(String name){
        this.name = name;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-29
    • 2014-05-05
    • 2011-04-07
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多