[英]How to properly delete a complex object using Hibernate?
Here is the problem. 这是问题所在。 When I'm trying to delete a Catalog object from database, Hibernate also removing all Catalog objects with associated Type and Genre Ids.
当我尝试从数据库中删除目录对象时,Hibernate还会删除所有具有关联的类型和类型ID的目录对象。 For example, if I'm removing Catalog with Type.id=1 and Genre.id=1 Hibernate delete every Catalogs with such Ids.
例如,如果我要删除Type.id = 1和Genre.id = 1的Catalog,则Hibernate删除带有此类ID的每个Catalog。 Any ideas how to fix it?
任何想法如何解决? I need to delete only one Catalog object without deleting Type and Genre objects with id=1.
我只需要删除一个Catalog对象,而不删除id = 1的Type和Genre对象。
@Entity
@Table(name = "catalog", catalog = "media_store_db")
public class Catalog implements Serializable {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(name = "product_name", length = 100)
private String productName;
@ManyToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "genre_id", referencedColumnName = "genre_id")
private Genre genre;
@ManyToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "type_id", referencedColumnName = "type_id")
private Type type;
@Entity
@Table(name = "genres", catalog = "media_store_db")
public class Genre implements Serializable {
@Id
@Column(name = "genre_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(name = "genre_name")
private String name;
@OneToMany(mappedBy = "genre", cascade = CascadeType.ALL)
Collection<Catalog> catalogs = new ArrayList<Catalog>();
@Entity
@Table(name = "types", catalog = "media_store_db")
public class Type implements Serializable {
@Id
@Column(name = "type_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(name = "type_name")
private String name;
@OneToMany(mappedBy = "type", cascade = CascadeType.ALL)
Collection<Catalog> catalogs = new ArrayList<Catalog>();
My method which delete a Catalog object 我的删除目录对象的方法
public void deleteCatalog(Integer catalogId) {
Session session = config.getSession();
Transaction tx = session.beginTransaction();
session.delete(session.get(Catalog.class, catalogId));
tx.commit();
session.close();
}
This is because of Cascade.ALL. 这是由于Cascade.ALL。 If you delete a parent if would also delete all related child if you are using Cascade.ALL.
如果删除父级,则在使用Cascade.ALL时也会删除所有相关的子级。
Instead ALL choose only what you need from the below 而是ALL从下面仅选择您需要的东西
CascadeType.PERSIST: cascades the persist (create) operation to associated entities if persist() is called or if the entity is managed CascadeType.PERSIST:如果调用persist()或管理实体,则将持久化(创建)操作级联到关联的实体
CascadeType.MERGE: cascades the merge operation to associated entities if merge() is called or if the entity is managed CascadeType.MERGE:如果调用merge()或管理实体,则将合并操作级联到关联的实体
CascadeType.REMOVE: cascades the remove operation to associated entities if delete() is called CascadeType.REMOVE:如果调用delete(),则将删除操作级联到关联的实体
CascadeType.REFRESH: cascades the refresh operation to associated entities if refresh() is called CascadeType.REFRESH:如果调用refresh(),则将刷新操作级联到关联的实体
CascadeType.DETACH: cascades the detach operation to associated entities if detach() is called CascadeType.DETACH:如果调用detach(),则将分离操作级联到关联的实体
CascadeType.ALL: all of the above CascadeType.ALL:以上全部
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.