1

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?

Sandiip Patil
  • 456
  • 1
  • 4
  • 21

1 Answers1

1

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)
lgu
  • 2,342
  • 21
  • 29
  • Thank you @Igu. This is something I was looking for. Small help with the link name: link.formattedFieldType(10) gives me my field as List. I want it to be like List. I checked the documentation but couldnt find anything to append the name with 'DTO' – Sandiip Patil Jun 01 '22 at 19:02
  • 1
    See "complement" in my previous answer – lgu Jun 02 '22 at 10:42
  • @Igu I added some more to the problem in main question. Can you please help on that as well. Thank you so much. – Sandiip Patil Jun 03 '22 at 20:25
  • Answer updated for the 2nd part of the question – lgu Jun 08 '22 at 10:41
  • Thank you Igu. I resolved the field name part just like you suggested. Sorry I missed out on updating. For getter n setters I explicitly imported lombak libraries as it was already in use in my application. Now I am stuck at changing the entity class names. As you see my entity names are weird because of some legacy db naming convention used. It looks like we cannot change it because telosys forces to use BEANNAME as java class name. Is there any way to resolve that? – Sandiip Patil Jun 08 '22 at 17:16
  • 1
    Answer updated with response to your 3rd question – lgu Jun 15 '22 at 15:00