@ManyToAny annotation transient instance error for Cascade.All
Description
I have below domain model. I added specialization entities both Product and ProductDetail entities with @ManyToAny annotation. Also, Product entity has many ProductDetail entities.
I want to save a new Product entity with one of its specializations and productDetails(with one of its specializations,too).
I use merge() method for new Product saving process, but it produces an error:
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.test.model.FlyingProductDetailSpecialization.
I searched for this issue, but I cannot find anything that is different from CascadeType.ALL usage answers. I used this cascade type.
If I use persist() method for new Product saving process, it results successful. But, if I want to update this Product entity (with merge() method) with adding a productDetail with its specializations, the same TransientObjectException also occurs.
Do you have any idea about this problem? Thanks.
(BaseObject class has createDate, updateDate, id columns. I don't add this class for shortening the question.)
@Table(name="flying_product_detail_specialization") @Entity public class FlyingProductDetailSpecialization extends BaseObject implements ISpecialization<ProductDetail>{
// some string and numerical fields }
@Table(name="walking_product_detail_specialization") @Entity public class WalkingProductDetailSpecialization extends BaseObject implements ISpecialization<ProductDetail>{
// some string and numerical fields }
// package-info definition for ManyToAny relationship @AnyMetaDef(name="ProductDetailSpecMetaDef", metaType="string", idType="string", metaValues= {@MetaValue(value = "Flying", targetEntity=FlyingProductDetailSpecialization.class), @MetaValue(value = "Walking", targetEntity=WalkingProductDetailSpecialization.class) }) // End of Product Detail Model
Activity
Show:
Gavin King November 29, 2024 at 9:13 AM
No runnable reproducer here, the link to the stack overflow question goes nowhere, and this is very old with no activity. Closing.
I have below domain model. I added specialization entities both Product and ProductDetail entities with @ManyToAny annotation. Also, Product entity has many ProductDetail entities.
I want to save a new Product entity with one of its specializations and productDetails(with one of its specializations,too).
I use merge() method for new Product saving process, but it produces an error:
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.test.model.FlyingProductDetailSpecialization.
I searched for this issue, but I cannot find anything that is different from CascadeType.ALL usage answers. I used this cascade type.
If I use persist() method for new Product saving process, it results successful. But, if I want to update this Product entity (with merge() method) with adding a productDetail with its specializations, the same TransientObjectException also occurs.
Do you have any idea about this problem? Thanks.
(BaseObject class has createDate, updateDate, id columns. I don't add this class for shortening the question.)
You can find this question at link:
https://stackoverflow.com/questions/62062582/manytoany-annotation-transient-instance-error-for-cascade-all
// Product Model
@Table(name="product")
@Entity
public class Product extends BaseObject{
@OneToMany(mappedBy="product", cascade=CascadeType.ALL, orphanRemoval=true)
@LazyCollection(LazyCollectionOption.FALSE)
@JsonIgnoreProperties(value="product", allowSetters=true)
private List<ProductDetail> productDetails = new ArrayList<>();
@ManyToAny(metaDef="ProductSpecMetaDef", metaColumn=@Column(name="product_specialization_type"))
@LazyCollection(LazyCollectionOption.FALSE)
@Cascade(org.hibernate.annotations.CascadeType.ALL)
@JoinTable(
name="product_specialization_relationship",
joinColumns=@JoinColumn(name="product"),
inverseJoinColumns=@JoinColumn(name="product_specialization")
)
private List<ISpecialization<Product>> productSpecializations = new ArrayList<>();
// and some string and numerical fields
}
@Table(name="unused_product_specialization")
@Entity
public class UnusedProductSpecialization extends BaseObject implements ISpecialization<Product>{
// some string and numerical fields
}
@Table(name="used_product_specialization")
@Entity
public class UsedProductSpecialization extends BaseObject implements ISpecialization<Product>{
// some string and numerical fields
}
// package-info definition for ManyToAny relationship
@AnyMetaDef(name="ProductSpecMetaDef", metaType="string", idType="string",
metaValues= {@MetaValue(value = "Unused", targetEntity=UnusedProductSpecialization.class),
@MetaValue(value = "Used", targetEntity=UsedProductSpecialization.class)
})
// End of Product Model
// Product Detail Model
@Table(name="product_detail")
@Entity
public class ProductDetail extends BaseObject{
@ManyToOne
@JoinColumn(name="product",referencedColumnName="id",nullable=false, foreignKey=@ForeignKey(name="product_detail_product_fkey"))
@JsonIgnoreProperties(value="productDetails", allowSetters=true)
private Product product;
@ManyToAny(metaDef="ProductDetailSpecMetaDef", metaColumn=@Column(name="product_detail_specialization_type"))
@LazyCollection(LazyCollectionOption.FALSE)
@Cascade(org.hibernate.annotations.CascadeType.ALL)
@JoinTable(
name="product_detail_specialization_relationship",
joinColumns=@JoinColumn(name="product_detail"),
inverseJoinColumns=@JoinColumn(name="product_detail_specialization")
)
private List<ISpecialization<ProductDetail>> productDetailSpecializations = new ArrayList<>();
// and some string and numerical fields
}
@Table(name="flying_product_detail_specialization")
@Entity
public class FlyingProductDetailSpecialization extends BaseObject implements ISpecialization<ProductDetail>{
// some string and numerical fields
}
@Table(name="walking_product_detail_specialization")
@Entity
public class WalkingProductDetailSpecialization extends BaseObject implements ISpecialization<ProductDetail>{
// some string and numerical fields
}
// package-info definition for ManyToAny relationship
@AnyMetaDef(name="ProductDetailSpecMetaDef", metaType="string", idType="string",
metaValues= {@MetaValue(value = "Flying", targetEntity=FlyingProductDetailSpecialization.class),
@MetaValue(value = "Walking", targetEntity=WalkingProductDetailSpecialization.class)
})
// End of Product Detail Model