-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Generating Annotated POJO: GeneratorType and Catalog annots
PostPosted: Wed Mar 01, 2006 4:27 pm 
Newbie

Joined: Wed Mar 01, 2006 3:23 pm
Posts: 8
Hi! I've been able to generate annotated POJOs from the database and now I need some help enhancing them a bit.

Hibernate version: 3.1.2

Tools version: 3.1.0.beta4

Annotations version: 3.1beta8


Here's my ant task:
Code:
<taskdef name="hibernatetool"
        classname="org.hibernate.tool.ant.HibernateToolTask"
        classpathref="lib.classpath"/>
    <target name="hibernategen">
        <hibernatetool destdir="${sourcedir}/WEB-INF/classes/">
            <jdbcconfiguration
             propertyfile="hibernateforant.properties"
             packagename="reger.dao.hibernate"
             reversestrategy="reger.hibernate.CustomReverseEngineeringStrategy"
             revengfile="${sourcedir}/WEB-INF/classes/hibernate.reveng.xml"
            />
            <hbm2java jdk5="true" ejb3="true"/>
            <hbm2cfgxml ejb3="true"/>
        </hibernatetool>
    </target>


And here's my custom reverse engineering class:
Code:
import org.hibernate.cfg.reveng.DelegatingReverseEngineeringStrategy;
import org.hibernate.cfg.reveng.ReverseEngineeringStrategy;

public class CustomReverseEngineeringStrategy extends DelegatingReverseEngineeringStrategy {
    public CustomReverseEngineeringStrategy(ReverseEngineeringStrategy delegate) {
        super(delegate);
    }
   
    public String getTableIdentifierStrategyName(org.hibernate.cfg.reveng.TableIdentifier tableIdentifier){
        return "GeneratorType.AUTO";
    }
}


This outputs a POJO like:
Code:
@Entity
@Table(name="eventtogroup"
    ,catalog="reger"
, uniqueConstraints = {  }
)

public class Eventtogroup  implements java.io.Serializable {


    // Fields   

     private int eventtogroupid;
     private Integer eventid;
     private Integer groupid;


    // Constructors

    /** default constructor */
    public Eventtogroup() {
    }

   
    /** full constructor */
    public Eventtogroup(Integer eventid, Integer groupid) {
        this.eventid = eventid;
        this.groupid = groupid;
    }
   

   
    // Property accessors
    @GenericGenerator(name="generator", strategy="GeneratorType.AUTO", parameters = { } )
@Id @GeneratedValue(generator="generator")
    @Column(name="eventtogroupid", unique=true, nullable=false, insertable=true, updatable=true)

    public int getEventtogroupid() {
        return this.eventtogroupid;
    }
   
    public void setEventtogroupid(int eventtogroupid) {
        this.eventtogroupid = eventtogroupid;
    }
    @Column(name="eventid", unique=false, nullable=true, insertable=true, updatable=true)

    public Integer getEventid() {
        return this.eventid;
    }
   
    public void setEventid(Integer eventid) {
        this.eventid = eventid;
    }
    @Column(name="groupid", unique=false, nullable=true, insertable=true, updatable=true)

    public Integer getGroupid() {
        return this.groupid;
    }
   
    public void setGroupid(Integer groupid) {
        this.groupid = groupid;
    }

}


I'm having two problems.

First, I can't seem to properly set the GeneratorType annotation on the identity column. This is what I get now:
Code:
@GenericGenerator(name="generator", strategy="GeneratorType.AUTO", parameters = { } )
@Id @GeneratedValue(generator="generator")
    @Column(name="eventtogroupid", unique=true, nullable=false, insertable=true, updatable=true)

    public int getEventtogroupid() {
        return this.eventtogroupid;
    }


But I'd like to see (basically, I want to use the AUTO type):
Code:
@Id @GeneratedValue(strategy=GeneratorType.AUTO)
    @Column(name="eventtogroupid", unique=true, nullable=false, insertable=true, updatable=true)

    public int getEventtogroupid() {
        return this.eventtogroupid;
    }


How would I go about this? Do I need to be working in my reverse engineering strategy (I'm passing a string with the value "GeneratorType.AUTO" now which isn't working) or is there some sort of mapping file that I need to work with?

Second, I don't like having the catalog name in the POJO:
Code:
@Table(name="eventtogroup"
    ,catalog="reger"
, uniqueConstraints = {  }
)


How would I go about the "catalog="reger"' piece?

My goal on both of these is to have an ant-runnable task that I can use to update the entities periodically without user intervention or tweaking.

Thanks,

Joe


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 4:43 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
the catalog/schema issue im fixing at the moment (the codegeneration does not strip out default schema/catalog as would be the solution)

The id generation strategy is done by returning "native" from the strategy method. This method should return hibernate names and the ejb3 generator converts that to the right thing...which in this case would be blank since AUTO is default.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 8:25 am 
Newbie

Joined: Wed Mar 01, 2006 3:23 pm
Posts: 8
Gotcha... thanks for all the hard work Max.... keeping up with a massive codebase and responding to users quickly is not easy.

Question: if I want to run two instances of an app (one codebase, separate db config) and have them point to different databases, do I need to strip out the catalog annotation myself manually? Or will the Hibernate session configuration override? In other words, is there any danger of keeping it in there (right now it's just the dev tier catalog name) when I launch to production?

Thanks again!

Joe


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 8:28 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you should remove it.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 18, 2007 3:20 pm 
Newbie

Joined: Sun Feb 18, 2007 3:18 pm
Posts: 3
Is there any chance to remove it automagically at generating time? We have round about 30 tables and the schema may change from time to time - so we do not want to have to do all that stuff manually.

Thanks for your great support.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 19, 2007 2:10 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
set hibernate.default_catalog and hibernate.default_schema when you run the tools to make the tools not insert those into the generated output.

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.