This is the solution I use:
I changed the file Ejb3PropertyGetAnnotation.ftl of hibernateTools. I added
the line "@org.hibernate.annotations.Cascade
({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.MERGE})" and "${property.setCascade("MERGE")}"
to generate the cascade element:
Code:
...
<#if c2h.isManyToOne(property)>
<#--TODO support @OneToOne true and false-->
${pojo.generateManyToOneAnnotation(property)}
@org.hibernate.annotations.Cascade
({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.MERGE})
<#--TODO support optional and targetEntity-->
${pojo.generateJoinColumnsAnnotation(property, cfg)}
<#elseif c2h.isCollection(property)>
${property.setCascade("MERGE")}
${pojo.generateCollectionAnnotation(property, cfg)}
<#else>
${pojo.generateBasicAnnotation(property)}
${pojo.generateAnnColumnAnnotation(property)}
</#if>
...
This is an example of the code that hibernateTools generates with the changes I've made:
Code:
@ManyToOne(fetch = FetchType.LAZY)
@org.hibernate.annotations.Cascade( {
org.hibernate.annotations.CascadeType.SAVE_UPDATE,
org.hibernate.annotations.CascadeType.MERGE })
@JoinColumn(name = "ALLERGEN_TYPE_CODE_FK")
public Cat0127AllergyType getCat0127AllergyType() {
return this.cat0127AllergyType;
}
...
@OneToMany(cascade = CascadeType.MERGE, fetch = FetchType.LAZY, mappedBy = "segAl1")
public Set<SegRelAl1> getSegRelAl1s() {
return this.segRelAl1s;
}