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

  • Home
  • SEARCH
  • 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 8143317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:05:30+00:00 2026-06-06T13:05:30+00:00

I have a problem with Ebean. I have the usual Objects PsecUser, PsecRoles and

  • 0

I have a problem with Ebean. I have the usual Objects PsecUser, PsecRoles and PsecPermission.
A user can have many Permissions or Roles and a Role can have many Permission.
Here the code (extract):

@Entity
public class PsecPermission {

    @Id
    @GeneratedValue
    private Long id;

    @Column(unique=true, nullable=false)
    private String name;

    @Column(nullable=false)
    private String type = PsecBasicPermission.class.getName();

    @Column(nullable=false)
    private String target;

    @Column(nullable=false)
    private String actions;

}


@Entity
public class PsecRole  {

    @Id
    @GeneratedValue
    private Long id;

    @Column(unique=true, nullable=false)
    private  String name;

    @Temporal(TemporalType.TIMESTAMP)
    private Date lastUpdate;

    @ManyToMany(fetch=FetchType.EAGER)
    private List<PsecPermission> psecPermissions;

    private boolean defaultRole = false;

}

I wrote the following helper-method:

public PsecRole createOrUpdateRole(String name, boolean defaultRole, String... permissions) {
        PsecRole result = server.find(PsecRole.class).
                where().eq("name", name).findUnique();
        if (result == null) {
            result = new PsecRole();
            result.setName(name);
        }
        final List<PsecPermission> permissionObjects = server.find(PsecPermission.class).
                where().in("name", (Object[])permissions).findList();
        result.setPsecPermissions(permissionObjects);
        result.setDefaultRole(defaultRole);
        final Set <ConstraintViolation <PsecRole>> errors = 
                Validation.getValidator().validate(result);
        if (errors.isEmpty()) {
            server.save(result);
            server.saveManyToManyAssociations(result, "psecPermissions");
        } else {
            log.error("Can't save role: " + name +"!");
            for (ConstraintViolation <PsecRole> constraintViolation : errors) {
                log.error("   " + constraintViolation);
            }
        }
        return result;
}

and try the following test:

@Test
public void testCreateOrUpdateRole() {
    String[] permNames = {"Test1", "Test2", "Test3"};
    List <PsecPermission> permissions = new ArrayList <PsecPermission>();
    for (int i = 0; i < permNames.length; i++) {
        helper.createOrUpdatePermission(permNames[i], "target"+ i, "actions" +i);
        PsecPermission perm = server.find(PsecPermission.class).where().eq("name", permNames[i]).findUnique();
        assertThat(perm.getTarget()).isEqualTo("target" + i);
        assertThat(perm.getActions()).isEqualTo("actions" + i);
        permissions.add(perm);
    }

    PsecRole orgRole = helper.createOrUpdateRole(ROLE, false, permNames);
    testRole(permNames, orgRole);
    PsecRole role = server.find(PsecRole.class).where().eq("name", ROLE).findUnique();
    testRole(permNames, role);

}
private void testRole(String[] permNames, PsecRole role) {
    assertThat(role).isNotNull();
    assertThat(role.getName()).isEqualTo(ROLE);
    assertThat(role.isDefaultRole()).isEqualTo(false);
    assertThat(role.getPermissions()).hasSize(permNames.length);
}

Which fails if it checks the number of permissions at the readed role. It’s always 0.
I looked into the database and found that psec_role_psec_permission is alway empty.

Any idea what’s wrong with the code?

You can get a pure Ebean-example from https://github.com/opensource21/ebean-samples/downloads it uses the eclipse-plugin from ebean.

  • 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-06-06T13:05:32+00:00Added an answer on June 6, 2026 at 1:05 pm

    There are two solutions for this problem:

    Simply add cascade option at PsceRole

    @ManyToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    private List<PsecPermission> psecPermissions;
    

    and remove server.saveManyToManyAssociations(result, "psecPermissions"); you find it in the cascade-solution-branch.

    The cleaner solution, because you don’t need to define cascase- perhaps you don’t want it:
    Just don’t replace the list, just add your entries to the list. Better is to add new and remove old one. This mean in createOrUpdateRole:

    result.getPsecPermissions().addAll(permissionObjects);
    

    instead of

    result.setPsecPermissions(permissionObjects);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have problem with reflection, dynamic invoking objects and reading collection values. In Referenced
I have problem with saving many to many relationship. When I run the code,
I have problem understanding shared preferences. I have activity where user will insert password,
I have problem with detecting when user reached the bottom of a ScrollViewer. My
I have problem with http://abfoodpolicy.com/ . In IE 8 and 9 the right sidebar
I have problem with my query on C, I’m using the oci8 driver. This
I have problem with repopulating form_upload after validation. Other input fields or selectboxes are
I have problem with show or hide form in Window Form Application. I start
I have problem with UIWebView delay when the load image from url. In my

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.