[英]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表包含静态值,例如mailing 、 home 、 work等。
In AddressTypeRel model class I'm having an AddressType object with annotation many to one在AddressTypeRel模型类中,我有一个带有多对一注释的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 。
Entity AddressTypeRel
is mapping one table column to two bean properties:实体
AddressTypeRel
将一个表列映射到两个 bean 属性:
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
推断出来)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 不允许两次映射同一个表列,除非您采取如下所述的必要步骤。
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 仅在所有其他映射都是只读的情况下才允许多次映射一列。
Making the relationship between AddressTypeRel
and AddressType
readonly:使
AddressTypeRel
和AddressType
之间的关系只读:
insertable=false, updatable=false
, but this apply only on the relationship and not the target entityinsertable=false, updatable=false
一样,但这仅适用于关系而不适用于目标实体Making the target entity readonly, in our case it is the AddressType
:使目标实体只读,在我们的例子中是
AddressType
:
@Transient
, this annotation will make hibernate to ignore the other entity of the relationship and won't bother you with saving it@Transient
,此注释将使休眠状态忽略关系的其他实体,并且不会打扰您保存它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.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
注释。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).@Transient
but not the relationship insertable=true, updatable=true
.@Transient
但不是关系insertable=true, updatable=true
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.