Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with reverse engineering a many-to-one unidirectional association with hibernate tools


I'm using Hibernate tools 3.40 in Eclipse (Helios). I'm trying to generate POJOs from my DB (MSSQL 2008) with EJB3 style (i.e. JPA2.0 annotations).
Let's say I have two tables A and B where there is a foreign key from A to B.
This generates, by default, a POJO for A which has B as a member (its "parent") and a POJO for B which has a Set<A> as a member (its "children").
What I'd like is to know how I can control the rev-eng so that only one side of the association is created (I have different use cases so basically all three options are important for me).
I do not want to use hbm.xml files as I'm working with annotations and JPA2.0 but I can specify some metadata on the reverse engineering process to hibernate via hibernae.reveng.xml

I've tried configuring the foreign-key attribute and defining there the exclude=true but that only provided me with a half an answer for one scenario. That generated an A POJO with a bPK int member which is tolerable and understandable but the generated POJO of B now does not compile as the one-to-many annotation has an invalid attribute; The mappedby="unresolved" due to the fact that A no longer has a property which hibernate reveng can map back to.

So, I currently cannot create uni-directional associations and I'd appreciate any help.

like image 290
Ittai Avatar asked Mar 09 '11 14:03

Ittai


1 Answers

Create a class for reveng. strategy at Hibernate Code Generation Configuration

Example :

public class MyReverseEngineeringStrategy extends DelegatingReverseEngineeringStrategy {

   public MyReverseEngineeringStrategy(ReverseEngineeringStrategy delegate) {
       super(delegate);
   }

   @Override
   public void setSettings(ReverseEngineeringSettings settings) {
       super.setSettings(settings);
   }

   @Override
   public boolean excludeForeignKeyAsCollection(String keyname, 
    TableIdentifier fromTable, java.util.List fromColumns, 
    TableIdentifier referencedTable, java.util.List referencedColumns) {

    // TODO : Your work here
    if (keyname.equals("___") && 
        fromTable.getName().equals("___") && 
        fromColumns.contains("___") && 
        referencedTable.getName().equals("___") && 
        referencedColumns.contains("___")) {

        return true;
    }

    return false;
   }
}

JavaDoc for method excludeForeignKeyAsCollection

Should this foreignkey be excluded as a oneToMany 

and there also have another method call excludeForeignKeyAsManytoOne

Should this foreignkey be excluded as a many-to-one 
like image 157
lschin Avatar answered Oct 05 '22 01:10

lschin