[英]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:我的后端结构是这样的:
JpaRepository
)JpaRepository
)Suppose that I have two entity in my model: Company
and Address
.假设我的模型中有两个实体:
Company
和Address
。 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
.所以,我有
CompanyRepository
和AddressRepository
。 I use these two interfaces in they respective Service classes: CompanyService
and AddressService
.我在它们各自的服务类中使用这两个接口:
CompanyService
和AddressService
。 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:到现在为止,我一直在做这些步骤:
Address
;Address
;Address
just saved (i need the idaddress
);Address
(我需要idaddress
);Address
to the company using setAddress
;setAddress
将Address
关联到公司;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
同时保存Company
和Address
???
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.