Search code examples
spring-data-jpatelosys

How to generate Records with links using Telosys


I am using Telosys templates to generate code for data entities. I used template where I can generate entity classes with links i.e. with relationships to other entities. However the DTO records generated do not have the links mapped correctly i.e. in entity class if there is ManytoOne relationship and collection as returntype from the getter, the same is not reflected in DTO record.

Is there any other template available or any change that I should do in the existing Record template?

Appreciate any help.

Question part 2 based on comment from @Igu I have generated entities like this:

//--- ENTITY LINKS ( RELATIONSHIP )
@OneToMany(fetch = FetchType.EAGER, mappedBy="mcophy", targetEntity=Pgnphy.class)
private Collection<Pgnphy> listOfPgnphy ; 

@OneToMany(fetch = FetchType.EAGER, mappedBy="mcophy", targetEntity=Pgaphy.class)
private Collection<Pgaphy> listOfPgaphy ; 

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="aiawtx", referencedColumnName="afawtx", insertable=false, updatable=false)
private Stnphy     stnphy ; 

which is as expected. However when I am generating my DTOs they are something like this:

//----------------------------------------------------------------------
// ENTITY LINKS ( RELATIONSHIP )
//----------------------------------------------------------------------
private List<PgaphyDTO> listOfPgaphy ;
private Stnphy stnphy       ;
private List<PgnphyDTO> listOfPgnphy ;

wherein I was expecting the second link be like:

private StnphyDTO stnphyDTO;

I used

private ${link.formattedFieldType(10).trim().replace(">","DTO>")} $link.formattedFieldName(12) ;

what am I not doing right?


Solution

  • Indeed, in this bundle of templates there's no relationship in DTO.

    If you want an example of basic Java bean with links you can try this bundle :

    https://github.com/telosys-templates-v3/java-domain-T300

    (see template "domain_entity_java.vm" )

    Complement :

    To convert the link type ( eg "List< Book >" to "List< BookDTO >" ) you can use the "replace" method (method of the class String in Java) to replace the char ">" by "DTO>".

    Example : private ${link.formattedFieldType(10).replace(">","DTO>")}

    ===== Regarding the 2nd question

    For the field type you just have to add "DTO" at the end if the link is not a collection (because there's no ">", so nothing to replace)

    For the field name, I'm not sure it's really useful to change the internal Java field name. But if you really want to do that you just have to add "DTO" string at the end of the field name ( ${link.fieldName} ).

    Examples:

    At class level (field type and field name):

    #if ($link.isCardinalityToMany() )  
       private ${link.fieldType.replace(">","DTO>")} ${link.fieldName}DTO ;
    #else 
       private ${link.fieldType}DTO ${link.fieldName}DTO ;
    #end
    

    In setter (field name only) :

    this.${link.fieldName}DTO = setterArgument;
    

    In getter (field name only) :

    return this.${link.fieldName}DTO;
    

    ===== Regarding the 3rd question about the generated file name

    In the "templates.cfg" file describing all the targets, you just have to add a suffix at the end of "${BEANNAME}" (2nd value of the target line)
    Example:

    your-label ; ${BEANNAME}DTO.java  ; your-dest-folder  ; your-template.vm
    

    If you need very specif transformations to build a complex target filename, you can also do that in the template file (.vm file) and force the target by using the "$target" object ( see https://www.telosys.org/doc/v400/objects/target.html )
    It will force the BEANNAME variable used in "templates.cfg"
    Example:

    #set( $newName = "${fn.uncapitalize($entity.name)}Foo" )  
    $target.forceEntityName($newName)