Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Annotation for all-delete-orphan in hibernate 4.1.4

I'm new to this hibernate annotation. I want to convert this xml mapping into annotations:

<map name="Text" table="JAV_TEXT" inverse="true" cascade="all-delete-orphan">
    <key column="FK_GUID"/>
    <map-key column="TEXT_GUID" type="string"/>
    <one-to-many class="com.TextPO"/>
</map>

This is what I have done:

@OneToMany(fetch = FetchType.LAZY, targetEntity=com.TextPO.class)
@Cascade({CascadeType.DELETE_ORPHAN})
@JoinColumn(name="FK_GUID")
@MapKey(name="TEXT_GUID")
private Map<String, PersistentObject> text = new HashMap<String, PersistentObject>();

But CascadeType.DELETE_ORPHAN is deprecated, so how do I represent all-delete-orphan through annotations? I'm using hibernate 4.1.4.

like image 426
Shailu Avatar asked Jul 31 '15 06:07

Shailu


People also ask

What is delete orphan in hibernate?

Orphan record is a record in relation table which doesn't have any association with its relationship owner. Example:Applicant has a list of address, suppose we delete this address list association with Applicant then all the address records becomes Orphan records.

What is the use of @entity annotation in hibernate?

@Entity annotation marks this class as an entity. @Table annotation specifies the table name where data of this entity is to be persisted. If you don't use @Table annotation, hibernate will use the class name as the table name by default. @Id annotation marks the identifier for this entity.

What is Cascade all Delete Orphan?

delete − it will cascade deletes. all-delete-orphan − it is a special one which is quite frequently used and is the same as All Except, if it finds Delete-orphan rows, it will delete those as well.

What is the use of orphanRemoval?

Such target entities are considered “orphans,” and the orphanRemoval attribute can be used to specify that orphaned entities should be removed. For example, if an order has many line items and one of them is removed from the order, the removed line item is considered an orphan.


1 Answers

Yes in Hibernate 4.1.4 version delete-orphan is deprecated, now in Hibernate and JPA 2.0 you can use orphanRemoval instead:

@OneToMany(orphanRemoval = true)

Your mapping should be like this:

@OneToMany(fetch = FetchType.LAZY, targetEntity=com.TextPO.class, orphanRemoval = true)
@JoinColumn(name="FK_GUID")
@MapKey(name="TEXT_GUID")
private Map<String, PersistentObject> text = new HashMap<String, PersistentObject>();

And also remove the @Cascade annotation you can use it as an attribute of the @OneToMany annotation like this:

@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, targetEntity=com.TextPO.class, orphanRemoval = true)

Take a look at this Example for further reading.

EDIT:

To give the inverse="true" property in your mapping you just need to specify the mappedBy attribute in your @OneToMany annotation to refer the owning part of the relation, like this:

@OneToMany(fetch = FetchType.LAZY, targetEntity=com.TextPO.class, orphanRemoval = true, mappedBy= "theOneSide")

Here the theOneSide is used as an example you just need to specify the field name used in the other side class of the mapping, for example:

@ManyToOne
private MyClass theOneSide;

Take a look at inverse=true in JPA annotations for further information.

like image 96
cнŝdk Avatar answered Sep 20 '22 20:09

cнŝdk