[英]Spring JPA ManyToOne - Save the transient instance before flushing
I created a Spring Web Application
with database access.我创建了一个具有数据库访问权限的
Spring Web Application
。 I have a class Person
- which has references to a class Address
.我有一个类
Person
- 它引用了一个类Address
。 A person has an address as well as the company, so person
and company
both refer to the Address
table. person
和company
都有Address
,因此person
和company
都参考Address
表。 I did this by defining a @ManyToOne
relation from person to address as well as the company.我通过定义从个人到地址以及公司的
@ManyToOne
关系来做到这一点。
@ManyToOne
@JoinColumn (name="COMPANY_ID")
private Adress companyId;
@ManyToOne
@JoinColumn (name="SUBSIDIARY_ID")
private Adress subsidiaryId;
In the person repository I have a save method like this:在个人存储库中,我有一个像这样的保存方法:
public Person save(Person entity) {
SaveAccess<Person> ao = createSaveAccess();
ao.setEntity(entity);
ao.execute();
return ao.getResult();
}
The thing is, if I try to add a new person I get a Hibernate-TransientPropertyValueException
which says Object references an unsaved transient instance - save the transient instance before flushing.
问题是,如果我尝试添加一个新人,我会得到一个
Hibernate-TransientPropertyValueException
,它表示Object references an unsaved transient instance - save the transient instance before flushing.
Can anyone help me here?有人能帮我一下吗?
Can you try this你能试试这个吗
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn (name="COMPANY_ID")
private Adress companyId;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn (name="SUBSIDIARY_ID")
private Adress subsidiaryId;
reference: Hibernate TransientPropertyValueException When saving data参考: Hibernate TransientPropertyValueException 保存数据时
https://dzone.com/tutorials/java/hibernate/hibernate-example/hibernate-mapping-many-to-one-using-annotations-1.html https://dzone.com/tutorials/java/hibernate/hibernate-example/hibernate-mapping-many-to-one-using-annotations-1.html
I had a similar issue with a @OneToOne
annotation of a table called Product
to another one called Category
.我有一个类似的问题,一个名为
Product
的表的@OneToOne
注释到另一个名为Category
。 I wanted to specify that every Product
has to have a single Category
.我想指定每个
Product
必须有一个Category
。
Thus, I had to make sure to set the cascade type to be CascadeType.ALL
like so:因此,我必须确保将级联类型设置为
CascadeType.ALL
如下所示:
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "CATEGORY_NAME")
private Category category;
I don't think so the address contains so much data it justifiable to store as an independent entity.我不认为地址包含如此多的数据,因此可以将其作为独立实体进行存储。 For performance considerations I recommend you to use embeddable + embedded classes for this purpose:
出于性能考虑,我建议您为此使用可嵌入 + 嵌入的类:
@Embeddable
public class Address
{
String number;
String street;
String city;
String country;
}
@Entity
public class Person
{
@Id
protected long id
...
@Embedded
Address address;
@Embedded
Address companyAddress;
...
}
If you have a legacy system and you have to store the addresses in a separate table, there is the @SecondaryTable solution:如果您有旧系统并且必须将地址存储在单独的表中,则可以使用 @SecondaryTable 解决方案:
@Entity
@Table(name = "EMPLOYEE")
@SecondaryTable(name = "ADDRESS", pkJoinColumns = @PrimaryKeyJoinColumn(name = "EMPLOYEE_ID"))
public class Employee
{
//...
@Column(table = "ADDRESS")
private Address address;
@Column(table = "ADDRESS")
private Address companyAddress;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.