简体   繁体   English

对象引用一个未保存的瞬态实例,在刷新之前保存瞬态实例:

[英]Object references an unsaved transient instance save the transient instance before flushing:

表的关系

The diagram shown above shows relation between tables.上图显示了表之间的关系。

The AddressType table contain static values such as mailing , home , work etc. AddressType表包含静态值,例如mailinghomework等。

In AddressTypeRel model class I'm having an AddressType object with annotation many to oneAddressTypeRel模型类中,我有一个带有多对一注释的AddressType对象

AddressTypeRel.java AddressTypeRel.java

public class AddressTypeRel implements java.io.Serializable{
.......

private AddressType addressType=new AddressType();
.......

@ManyToOne()
@LazyCollection(LazyCollectionOption.FALSE)
@JoinColumn(name="typeId", insertable=false, updatable=false, nullable=false)
@NotFound(action = NotFoundAction.IGNORE)
public AddressType getAddressType() {
   return addressType;
}

public void setAddressType(AddressType addressType) {
   this.addressType = addressType;
}
......
}

After saving into Address table I should also save the type ( mailing/billing ) of address and addressId into AddressTypeRel but I'm not able to save.保存到地址表后,我还应该将地址和addressId类型邮寄/账单)保存到AddressTypeRel但我无法保存。 While I try to save当我尝试保存时

AddressTypeRel addressTypeRel=new AddressTypeRel();
addressTypeRel.setAddressId(i) //i=5 for example
addressTypeRel.setTypeId(j)    //j=4 for example
hibernatetemplate.save(addressTypeRel);

The error occuring is :发生的错误是:

object references an unsaved transient instance - save the transient instance before flushing: com.sample.AddressType对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例:com.sample.AddressType

您需要将@ManyToOne()更改为@ManyToOne(cascade = CascadeType.ALL)以便保存将尝试级联到 AddressType 然后忽略它,因为您在 @JoinColumn 上设置了insertable=false, updatable=false @JoinColumn =false 。

The problem问题

Entity AddressTypeRel is mapping one table column to two bean properties:实体AddressTypeRel将一个表列映射到两个 bean 属性:

  1. int typeId (this is not explicitly said in the question but you can infer it from addressTypeRel.setTypeId(j) //j=4 for example ) int typeId (这在问题中没有明确说明,但您可以从addressTypeRel.setTypeId(j) //j=4 for example推断出来)
  2. AddressType addressType (as explicitly described in question) AddressType addressType (如问题中明确描述的)

Hibernate does not allow mapping the same table column twice, unless you take necessary steps as described below. Hibernate 不允许两次映射同一个表列,除非您采取如下所述的必要步骤。

The solution解决方案

This is two step process, and you made only the first one.这是两步过程,你只做了第一步。 The goal is to make AddressType addressType (mapped via its getter getAddressType() ) readonly, because Hibernate allows mapping one column multiple times only if all additional mappings are readonly .目标是使AddressType addressType (通过其 getter getAddressType()映射)只读,因为 Hibernate 仅在所有其他映射都是只读的情况下才允许多次映射一列

  1. Making the relationship between AddressTypeRel and AddressType readonly:使AddressTypeRelAddressType之间的关系只读:

    • as you already did you mapped it as insertable=false, updatable=false , but this apply only on the relationship and not the target entity正如您已经将其映射为insertable=false, updatable=false一样,但这仅适用于关系而不适用于目标实体
  2. Making the target entity readonly, in our case it is the AddressType :使目标实体只读,在我们的例子中是AddressType

    • just mark it @Transient , this annotation will make hibernate to ignore the other entity of the relationship and won't bother you with saving it只需将其标记为@Transient ,此注释将使休眠状态忽略关系的其他实体,并且不会打扰您保存它

Discussion讨论

  • I have used insertable=false, updatable=false before, why am I having this problem now?我之前用过insertable=false, updatable=false ,为什么现在出现这个问题? Because it is far more common to have the read-write mapping on entity field, and have the readonly mapping on some primitive type or non-entity object.因为在实体字段上具有读写映射,而在某些原始类型或非实体对象上具有只读映射更为常见。 In this case you had read-write mapping on int and readonly mapping on entity AddressType .在这种情况下,您对int进行了读写映射,对实体AddressType进行了只读映射。 And you don't have to use @Transient annotation on primitive types or non-entity objects of course.当然,您不必在原始类型或非实体对象上使用@Transient注释。
  • Why isn't insertable=false, updatable=false smarter, and detect those cases?为什么insertable=false, updatable=false不能更智能地检测这些情况? Because Hibernate see relationship between two entities as three part construct: a) this entity b) target entity c) the relationship between them , so making this construct readonly requires b) and c) readonly and for both there are distinct tools to do that (as described in solution).因为 Hibernate 将两个实体之间的关系视为三部分构造: a)这个实体 b)目标实体 c)它们之间的关系,所以使这个构造只读需要b)c)只读,并且对于两者都有不同的工具来做到这一点(如解决方案中所述)。 This is useful because you have finer control, eg you might want to make the target entity @Transient but not the relationship insertable=true, updatable=true .这很有用,因为您有更好的控制,例如,您可能希望使目标实体@Transient但不是关系insertable=true, updatable=true

暂无
暂无

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

相关问题 对象引用未保存的瞬态实例-在刷新之前保存瞬态实例 - object references an unsaved transient instance - 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 对象引用了一个未保存的瞬态实例-在刷新之前保存瞬态实例:Spring Data JPA - object references an unsaved transient instance - save the transient instance before flushing : Spring Data JPA 如何解决对象引用未保存的瞬态实例的错误-在刷新之前保存瞬态实例? - How do I solve this error of object references an unsaved transient instance - save the transient instance before flushing? 对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例:com.entity.Role - object references an unsaved transient instance - save the transient instance before flushing: com.entity.Role TransientObjectException:对象引用了一个未保存的临时实例-在执行合并时在刷新之前保存该临时实例 - TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing when I am doing merge TransientObjectException - 对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例 - TransientObjectException - object references an unsaved transient instance - save the transient instance before flushing 对象引用未保存的瞬态实例-在刷新之前保存瞬态实例(而Fetch Query JPA) - object references an unsaved transient instance - save the transient instance before flushing( while Fetch Query JPA) 对象引用了一个未保存的临时实例-使用休眠空间在刷新之前保存该临时实例 - object references an unsaved transient instance - save the transient instance before flushing using hibernate spatial 域表出现错误“对象引用了未保存的瞬态实例-在刷新之前保存瞬态实例” - Domain table getting error “object references an unsaved transient instance - save the transient instance before flushing”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM