Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7565687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:13:06+00:00 2026-05-30T14:13:06+00:00

I’m a newbie in Spring. I think it’s better to explain my problem with

  • 0

I’m a newbie in Spring. I think it’s better to explain my problem with a little example. Let’s say I have two main classes: User and Group. A User can be part of more Groups and a Group, obviously, can have more Users. So the relationship between them is many-to-many. What I would like to show, is something like this (using JSTL):

<c:forEach items="${groups}" var="group">
    <c:out value="${group.name}"/> (<c:out value="${fn:length(group.users)}" />):<br />
    <c:forEach items="${groups.users}" var="user">
        <c:out value="${user.name}"/><br />
    </c:forEach><br />
</c:forEach>

Basically, the output should be something like:

Random (2):
Joe
Bloggs

Star wars (5):
Luke
Chewbacca
Darth Vader
Princess Leia
Yoda

Nintendo (3):
Super Mario
Metroid
Zelda

I initially coded it with the classic @ManyToMany annotation, using an additional table user_has_group (created and managed by JPA) and it was working perfectly.

I needed to modify the structure since I needed the user_has_group table to have the joined_date column. To achieve it, I read online that the best solution is to create another class (i.e. UserHasGroup) and add the one-to-many relationships to this class from User and group. Doing so, it’s possible to add additional attributes to the UserHasGroup class (and therefore additional columns to the user_has_group table). Something like:

User:

@Entity
@Table(name = "user")
public class User
{
    @Id
    @GeneratedValue
    @Column
    private int id;

    @Column
    private String alias;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
    private List<UserHasGroup> userHasGroup = new ArrayList<UserHasGroup>();

    // Constructors/getters/setters
}

Group:

@Entity
@Table(name = "`group`")
public class Group
{
    @Id
    @GeneratedValue
    @Column
    private int id;

    @Column
    private String name;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "group")
    private List<UserHasGroup> userHasGroup = new ArrayList<UserHasGroup>();

    // Constructors/getters/setters
}

UserHasGroup:

@Entity
@Table(name = "user_has_group")
public class UserHasGroup
{
    @Id
    @GeneratedValue
    @Column
    private int id;

    @ManyToOne
    @JoinColumn(name = "user_id")
    private User user;

    @ManyToOne
    @JoinColumn(name = "group_id")
    private Group group;

    @Column
    @Temporal(TemporalType.DATE)
    private Date joinedDate;

    // Constructors/getters/setters
}

So far, so good. All the tests run successfully and the functionality is maintained.

But I’m facing a problem with JSTL. In fact, with this new structure is obviously not possible to do group.users to iterate through the users.

What is the best way to reach the same functionality as before but with this new structure?

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:13:07+00:00Added an answer on May 30, 2026 at 2:13 pm

    I don’t see a reason why ${fn:length(group.userHasGroup)} should’n work.

    The only problem you might come accross is some no active session exception. You can solve it either by

    • using “open session in view” interceptor (which somebody calls an anti-pattern)

    • manualy iterating through the list in your service method

    • eager fetching the relationship – i would be very carefull here as this can lead to many queries to database

    Answer to additional question:

    It should look somehow like this:

    <c:forEach items="${groups}" var="group">
        <c:out value="${group.name}"/> (<c:out value="${fn:length(group.userHasGroup)}" />):<br />
        <c:forEach items="${groups.userHasGroup}" var="userHasGroup">
             <c:out value="${userHasGroup.user.name}"/><br />
      </c:forEach><br />
    

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.