6

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.

Sofia Paixão
  • 309
  • 2
  • 16
Shailu
  • 163
  • 1
  • 5
  • 16

1 Answers1

7

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.

Community
  • 1
  • 1
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
  • I have doubt like how to give annotation for "inverse = true" – Shailu Jul 31 '15 at 09:48
  • 1
    you can simply add the mappedBy attribute to the `@OneToMany` annotation, it will replace the `inverse="true"`. – cнŝdk Jul 31 '15 at 09:51
  • It is showing like "The attribute orphanRemoval is undefined for the annotation type OneToMany".. – Shailu Jul 31 '15 at 10:06
  • Maybe you have an old version of Hibernate? are you sure it's 4.1.4? you can see in [the OneToMany Documentation](https://docs.jboss.org/hibernate/jpa/2.1/api/javax/persistence/OneToMany.html#orphanRemoval()) it states that it's there since JPA2.0, and make sure it's correctly spelled `orphanRemoval`, also make sure you imported `javax.persistence.OneToMany; `. – cнŝdk Jul 31 '15 at 10:20
  • so true if you use mappedBy you have to remove cascade – pv1 Apr 14 '16 at 16:04
  • In your EDIT you've forgotten the "cascade = { CascadeType.ALL }" - to replace "all-delete-orphan" and not only "delete-orphan" – olivmir Sep 03 '18 at 10:23