Java Persistence/Converting hibernate xml-mapping to JPA annotations

foo.hbm.xml Foo.java
<class name="Foo">
@Entity
public class Foo
// or:
@Entity(name="Foo")
public class CanBeNotFoo
// or:
<class name="Foo" table="NotFoo">
@Entity(name="Foo")
@Table(name="NotFoo")
public class Foo
<cache usage="read-write" region="directory" />
@Entity(name="Foo")
// ...
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "directory")
<many-to-one name="test1" column="test_id" class="Test" />
<!-- default is lazy -->
@ManyToOne(fetch = FetchType.LAZY) // default is EAGER
@JoinColumn(name="test_id")
Test test1;
<property name="applyIfDone" column="apply_if_done" />
@Column(name="apply_if_done")
Boolean applyIfDone;
<property name="apply" />
@Column(name="apply")
Boolean apply;
<set name="testGroups" cascade="all-delete-orphan">
	<cache usage="read-write" region="collection" />
	<key column="group_id" />
	<one-to-many class="GroupTest" />
</set>
@OneToMany(orphanRemoval = true, cascade = {CascadeType.ALL}) // default fetch type is LAZY
@JoinColumn(name = "foo_id") // backref column to owner in GroupTest (unidir/bidir)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "collection")
private Collection<GroupTest> testGroups = ...
<set name="targets" table="foo_targets">
	<cache usage="read-write" region="collection" />
	<key column="foo_id" />
	<many-to-many class="Target" column="target_id" />
</set>
// in Foo.java:
@ManyToMany // default fetch type is LAZY
@JoinTable(name = "foo_targets",
    joinColumns = { @JoinColumn(name = "foo_id") },
    inverseJoinColumns = { @JoinColumn(name = "target_id") })
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "collection")
private Collection<Target> targets = ...

// in target.hbm.xml property "foos" is annotated with inverse="true"
// in Target.java:
@ManyToMany(mappedBy = "targets")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "collection")
private Collection<Foo> foos = ...


<set name="valuesStrings" table="TestValue">
	<cache usage="read-write" region="collection" />
	<key column="id" />
	<element type="String" column="value" />
</set>
@ElementCollection
@CollectionTable(name = "TestValue", joinColumns = @JoinColumn(name = "id"))
@Column(name = "value")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "collection")
private Collection<String> valuesStrings = ...


<joined-subclass name="Simple">
</joined-subclass>
@Inheritance(strategy = InheritanceType.JOINED)
public class Simple ...


<set name="testGroups" ... inverse="true">
...
	<one-to-many class="GroupTest" />
</set>
// in file Foo.java:

@OneToMany(mappedBy="fooBackRef")
private Collection<GroupTest> testGroups = ...

// in file GroupTest.java :
class GroupTest {
@ManyToOne
@JoinColumn(name="foo_backref_id")
Foo fooBackRef;
... 
}


<set name="..." cascade="save-update">
...
</set>
// if you using pure JPA or Hibernate >= 3.x
@AnyToAny(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH })
// For old Hibernate or for Hibernate Session instead of JPA Entity Manager, add:
@Cascade(value = org.hibernate.annotations.CascadeType.SAVE_UPDATE)

Sources

edit