简体   繁体   English

Spring JPA ManyToOne - 在刷新之前保存瞬态实例

[英]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. personcompany都有Address ,因此personcompany都参考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.

相关问题 JPA在刷新之前保存临时实例 - JPA save the transient instance before flushing 对象引用了一个未保存的瞬态实例-在刷新之前保存瞬态实例:Spring Data JPA - object references an unsaved transient instance - save the transient instance before flushing : Spring Data JPA Spring Data/JPA 和 errorType :对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例 - Spring Data/JPA and errorType : object references an unsaved transient instance - save the transient instance before flushing JPA对象引用了未保存的瞬态实例-在刷新之前保存瞬态实例 - JPA object references an unsaved transient instance - save the transient instance before flushing 对象引用了未保存的瞬态实例-在刷新休眠JPA之前保存瞬态实例 - Object references an unsaved transient instance - save the transient instance before flushing hibernate JPA 保存实体时JPA错误,对象引用了未保存的瞬态实例-在刷新之前保存瞬态实例 - JPA error while saving entity, object references an unsaved transient instance - save the transient instance before flushing ManyToMany关系:刷新之前保存临时实例 - ManyToMany relation: save the transient instance before flushing Hibernate 错误消息:在刷新之前保存瞬态实例 - Hibernate Error Message: Save the transient instance before flushing 使用 Hibernate 保存 object object 引用未保存的瞬态实例 在刷新之前保存瞬态实例 - save the object using Hibernate object references an unsaved transient instance save the transient instance before flushing 如何解决对象引用未保存的瞬态实例的错误-在刷新之前保存瞬态实例? - How do I solve this error of object references an unsaved transient instance - save the transient instance before flushing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM