简体   繁体   English

使用 JpaRepository 同时保存一个组合对象

[英]Save at the same time an composed object using JpaRepository

Can I save an object that contains another object directly in the database?我可以在数据库中直接保存包含另一个对象的对象吗?

My back-end structure is like this:我的后端结构是这样的:

  1. Rest services休息服务
  2. Services服务
  3. Repository (extends JpaRepository )存储库(扩展JpaRepository
  4. Model模型

Suppose that I have two entity in my model: Company and Address .假设我的模型中有两个实体: CompanyAddress Both generated by the JPA tool provided by IntelliJ.两者均由 IntelliJ 提供的 JPA 工具生成。

Company class model公司类模型

@Entity
public class Company {
    private int idcompany;
    private String name;
    private Address address;

    @Id
    @Column(name = "idcompany")
    public int getIdcompany() {
        return idcompany;
    }

    public void setIdcompany(int idcompany) {
        this.idcompany = idcompany;
    }

    @Basic
    @Column(name = "name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @ManyToOne
    @JoinColumn(name = "idaddress", referencedColumnName = "idaddress", nullable = false)
    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}

Address class model地址类模型

@Entity
public class Address {
    private long idaddress;
    private String zip;

    @Id
    @Column(name = "idaddress")
    public long getIdaddress() {
        return idaddress;
    }

    public void setIdaddress(long idaddress) {
        this.idaddress = idaddress;
    }

    @Basic
    @Column(name = "zip")
    public String getZip() {
        return zip;
    }

    public void setZip(String zip) {
        this.zip = zip;
    }
}

Moreover, both entities have an interface that extends the interface JpaRepository<T,ID> .此外,两个实体都有一个扩展接口JpaRepository<T,ID>的接口。 So, I have CompanyRepository and AddressRepository .所以,我有CompanyRepositoryAddressRepository I use these two interfaces in they respective Service classes: CompanyService and AddressService .我在它们各自的服务类中使用这两个接口: CompanyServiceAddressService This is where I put the business logic.这是我放置业务逻辑的地方。 All ok !一切都好

Now, I recive using a REST service, through POST an object Company that contains the object Address .现在,我通过POST使用 REST 服务接收一个包含对象Address的对象Company I need to save them into the database (MySql).我需要将它们保存到数据库(MySql)中。

In the json file there are Company that contains Address !在json文件中有包含Address Company

Until now, I've always done these steps:到现在为止,我一直在做这些步骤:

  1. Save Address ;保存Address
  2. Retrieve the Address just saved (i need the idaddress );检索刚刚保存的Address (我需要idaddress );
  3. I associate the Address to the company using setAddress ;我使用setAddressAddress关联到公司;
  4. Save Company拯救Company

I tried to save the object Company received via REST calling the method save from CompanyService (using CompanyRepository ) but I got the error:我试图保存通过 REST 接收的对象Company调用从CompanyService save的方法(使用CompanyRepository ),但出现错误:

Column 'idaddress' cannot be null

I ask you.我问你。 Is there an easier way to save Company and Address at the same time using JpaRepository ???有没有更简单的方法可以使用JpaRepository同时保存CompanyAddress ???

You don't have defined any cascading.您没有定义任何级联。

You could define PERSIST to let the Address persist when Company is persisted:你可以定义 PERSIST 来让地址在 Company 被持久化时持久化:

@ManyToOne
@JoinColumn(name = "idaddress", referencedColumnName = "idaddress", nullable = false,
            cascade = CascadeType.PERSIST)
public Address getAddress() {
    return address;
}

For every method on the EntityManager there is a CascadeType defined.对于 EntityManager 上的每个方法,都定义了一个 CascadeType。

Read more about the cascade types here: https://vladmihalcea.com/a-beginners-guide-to-jpa-and-hibernate-cascade-types/在此处阅读有关级联类型的更多信息: https : //vladmihalcea.com/a-beginners-guide-to-jpa-and-hibernate-cascade-types/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 JPARepository 保存实体 - Save Entity Using JPARepository Spring JpaRepository:delete(),在同一事务中使用后续的save() - Spring JpaRepository: delete() with subsequent save() in the same transaction Spring JpaRepository save()不使用Mockito进行模拟 - Spring JpaRepository save() does not mock using Mockito 如何使用Spring Boot JpaRepository及其MySQL脚本保存多个表 - How to save multiple tables using Spring Boot JpaRepository and their MySQL script 在休眠状态下同时保存父对象和子对象列表 - Save Parent Object and list of child object Same Time in hibernate 使用复杂的sqlquery时如何使用Hibernate创建组合对象的映射? - How to create mapping of composed object with Hibernate when using a complex sqlquery? save()上的Spring Boot JPARepository性能 - Spring Boot JPARepository performance on save() JPARepository 保存方法返回 null? - JPARepository save methods returns null? Java Hibernate save an object and update a different object using the same session in the same transaction only update and not save - Java Hibernate save an object and update a different object using the same session in the same transaction only update and not save 在GAE的JPA中以相同的方法调用多个JpaRepository时,ID为“”的对象由另一个对象管理器管理 - Object with id “” is managed by a different Object Manager when calling several JpaRepository in the same methods in GAE's JPA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM