Annotations for all hibernating exclusion orphans 4.1.4
I am new to this sleeping annotation. I want to convert this xml mapping to 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>
Here's what I did:
@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
deprecated, so how can I represent all-delete-orphan
via annotations? I am using Hibernate 4.1.4.
source to share
Yes in Hibernate 4.1.4
version is delete-orphan
deprecated, now in Hibernate
and JPA 2.0
you can use instead orphanRemoval
:
@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 annotation @Cascade
which you can use as the attribute of the annotation @OneToMany
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 provide a property inverse="true"
in your mapping, you just need to specify an attribute mappedBy
in your @ annotation OneToMany
to refer to the owned part of the relationship, for example:
@OneToMany(fetch = FetchType.LAZY, targetEntity=com.TextPO.class, orphanRemoval = true, mappedBy= "theOneSide")
theOneSide
Used here as an example, you just need to specify the field name used in the other side mapping class, for example:
@ManyToOne
private MyClass theOneSide;
Take a look at inverse = true in the JPA annotation .
source to share