Where to perform transformations?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Where to perform transformations?

gerryK
I know this is not strictly a Thymeleaf question however I figure Thymeleaf users are the best people to ask...

I am using SpringMVV and Hibernate to create my model objects in a publishing app. A single article can have 1 or more authors. This is modeled as a one-to-many relationship in the database. In my views I have to display the list of authors (first  middle and last names with a comma separating authors). Not all authors have middle names and of course we don't have a comma after the last in the list.

I thought I would use Hibernate's @Formula to build the individual 'fullname's however concatenting these has me stumped.
Hibernate's @PostLoad looked like the way to go. Unfortunately that does not work with SessionFactory. I could drop Hibernate and get my head around Spring JPA and yet there must be a way to do derive a nicely formatted author list?

Changing the database is not an option because it is used by other applications. I am happy to mess with the data in the service or controllers if I have to. Having a 9 way switch in the template sucks and besides this is just one example of messing with the data.

Gerry

 
Reply | Threaded
Open this post in threaded view
|

Re: Where to perform transformations?

Zemi
Administrator
Hello,

at the end you will set a model attribute (Java object) in your controller so as to print the data.

I would add a Java method in that class:

     public String getFormattedName() {
          return firstName + " " + middleName ...
     }

That model attribute class could be indeed your @Entity, but for maintainabilty it's better to use a different class.

Reply | Threaded
Open this post in threaded view
|

Re: Where to perform transformations?

gerryK
On 06/02/2013 15:12, Zemi [via Thymeleaf - User Forum] wrote:

> Hello,
>
> at the end you will set a model attribute (Java object) in your
> controller so as to print the data.
>
> I would add a Java method in that class:
>
>      public String getFormattedName() {
>           return firstName + " " + middleName ...
>      }
>
> That model attribute class could be indeed your @Entity, but for
> maintainabilty it's better to use a different class.
Thanks Zemi - a decorated @Entity makes sense