-->
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.  [ 11 posts ] 
Author Message
 Post subject: POJO gen using Freemarker templates
PostPosted: Wed Aug 13, 2008 5:16 pm 
Newbie

Joined: Wed Aug 13, 2008 5:02 pm
Posts: 7
I am generating POJO using the following code
My aim is to access the fields length , precision,null /not null , Is primary key check in the ftl file.

I have access to the pojo .Now how do i get to the above properties?

Note : Iam able to do the following
    pojo.getAllPropertiesIterator()
    <#foreach field in pojo.getAllPropertiesIterator()>
    field.getColumnIterator()
    <#foreach columnMeta in field.getColumnIterator()>
    columnMeta.getLength()

But this breaks

columnMeta.getPrecision() .

Can you please explain this .Thanks

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2x.TemplateProducer;
import org.hibernate.tool.hbm2x.pojo.POJOClass;

Public class HibernateAllCodeExporter extends HibernateCodeExporter
{

private File outputDirLoc;

public HibernateAllCodeExporter()
{
super();
}

public HibernateAllCodeExporter(
Configuration cfg_,
File outputdir_)
{
super(cfg_, outputdir_);
}

@Override
protected void exportPOJO(
Map additionalContext_, POJOClass element_)
{
TemplateProducer producer = new TemplateProducer(getTemplateHelper(), getArtifactCollector());
additionalContext_.put("pojo", element_);
additionalContext_.put("clazz", element_.getDecoratedObject());
additionalContext_.put("fieldNaming", fieldNaming);
extraSettings.setExtraPropertiesPath(getExtraPropertiesPath());
additionalContext_.put("extraSettings", extraSettings);
additionalContext_.put("md5sum", md5sum);[/b]


    Top
     Profile  
     
     Post subject:
    PostPosted: Wed Aug 13, 2008 6:01 pm 
    Newbie

    Joined: Wed May 03, 2006 5:09 pm
    Posts: 17
    You probably need to use column.columnSize or column.decimalDigits to get what you want. Look at the hibernate source for the ColumnMetaData class for more info.

    Drew


    Top
     Profile  
     
     Post subject:
    PostPosted: Thu Aug 14, 2008 9:50 am 
    Newbie

    Joined: Wed Aug 13, 2008 5:02 pm
    Posts: 7
    when i try column.columnSize or column.decimalDigits i get freemarker error .
    ColumnMetaData is part of org.hibernate.tool.hbm2ddl package.
    What iam doing is generating POJO.


    How do i get the Columns Meta data info similiar to org.hibernate.tool.hbm2ddl.ColumnMetadata ?


    Appreciate your help!


    Top
     Profile  
     
     Post subject:
    PostPosted: Thu Aug 14, 2008 4:06 pm 
    Newbie

    Joined: Wed May 03, 2006 5:09 pm
    Posts: 17
    I guess you'll have to explain what you mean when you say that column.getPrecision() breaks then. In the ftl files I normally access things like this column.precision and hibernate will use the getter method for precision to get me the value. Does it tell you that the property doesn't exist?

    Every property in the Column class (sorry, I was thrown off by your naming :) ) should be available to you in the ftl files.

    Drew


    Top
     Profile  
     
     Post subject:
    PostPosted: Thu Aug 14, 2008 5:29 pm 
    Newbie

    Joined: Wed Aug 13, 2008 5:02 pm
    Posts: 7
    ${columnMeta.precision} -->works well
    if i do ${columnMeta.nullable} with & with out quote i get the following :

    Expecting a string, date or number here, Expression columnMeta.nullable is instead a freemarker.ext.beans.BooleanModel
    at freemarker.core.Expression.getStringValue(Expression.java:126)
    at freemarker.core.Expression.getStringValue(Expression.java:93)
    at freemarker.core.DollarVariable.accept(DollarVariable.java:76)
    at freemarker.core.Environment.visit(Environment.java:196)
    at freemarker.core.MixedContent.accept(MixedContent.java:92)
    at freemarker.core.Environment.visit(Environment.java:196)
    at freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:160)

    Also for columnMeta.decimalDigits i get
    freemarker.core.InvalidReferenceException: Expression columnMeta.decimalDigits i
    s undefined on line 102, column 4 in codegen/template/GwtFormField.ftl.


    can you paste your ftl file here and the code exporter class


    Top
     Profile  
     
     Post subject:
    PostPosted: Thu Aug 14, 2008 5:39 pm 
    Newbie

    Joined: Wed May 03, 2006 5:09 pm
    Posts: 17
    You had the right idea with the column.precision. Ignore my comment about decimalDigits.

    Since that's a restricted keyword in freemarker what I would do is something like this:

    If you wanted to output nullable=false then you could put
    <#if !column.nullable>
    nullable=false
    <#else>
    nullable=true
    </#if>

    Referencing nullable in the if check works fine, it's just a problem when you try to access it with the ${} notation.

    Drew


    Top
     Profile  
     
     Post subject:
    PostPosted: Thu Aug 14, 2008 5:50 pm 
    Newbie

    Joined: Wed Aug 13, 2008 5:02 pm
    Posts: 7
    Super!! Nullable works great .

    How do i deal with decimal precision..


    Top
     Profile  
     
     Post subject:
    PostPosted: Thu Aug 14, 2008 5:51 pm 
    Newbie

    Joined: Wed Aug 13, 2008 5:02 pm
    Posts: 7
    Could be a version issue I think ..
    Anyways thanks for the excellent support!!


    Top
     Profile  
     
     Post subject:
    PostPosted: Thu Aug 14, 2008 5:51 pm 
    Newbie

    Joined: Wed Aug 13, 2008 5:02 pm
    Posts: 7
    Quote:
    .


    Last edited by DhivyaRamasamy123 on Thu Aug 14, 2008 5:52 pm, edited 1 time in total.

    Top
     Profile  
     
     Post subject:
    PostPosted: Thu Aug 14, 2008 6:15 pm 
    Newbie

    Joined: Wed May 03, 2006 5:09 pm
    Posts: 17
    Per math terms:

    precision is the maximum number of digits so in the number 1234.56 the precision is 6

    scale is the number of digits to the right of the decimal point so in the number 1234.56 scale is 2.

    using column.precision should give you 6 and column.scale should give you 2. I believe column.scale is what you are looking for.

    Drew


    Top
     Profile  
     
     Post subject:
    PostPosted: Fri Aug 15, 2008 10:03 am 
    Newbie

    Joined: Wed Aug 13, 2008 5:02 pm
    Posts: 7
    I was loking into this API

    http://www.hibernate.org/hib_docs/v3/ap ... adata.html

    where are you refering the API


    Top
     Profile  
     
    Display posts from previous:  Sort by  
    Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 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.