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 8222721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:22:30+00:00 2026-06-07T14:22:30+00:00

Pretend you have a class like this one: public class CreateUserRequest { … @NotNull

  • 0

Pretend you have a class like this one:

public class CreateUserRequest {
  ...
  @NotNull
  @Size(min = 4, max = 16)
  @Pattern(regexp = "^[a-zA-Z0-9]+$")
  private final String userName;
  ...
}

What we have here is a field “userName” that has some constraints, so that some values are OK and some are not. You also have a class EditUserRequest. You user userName as user identifier, so you’re going to have this field in EditUserRequest for sure:

public class EditUserRequest {
  ...
  @NotNull
  @Size(min = 4, max = 16)
  @Pattern(regexp = "^[a-zA-Z0-9]+$")
  private final String userName;
  ...
}

And for sure you’d like to be able to delete users:

public class DeleteUserRequest {
  ...
  @NotNull
  @Size(min = 4, max = 16)
  @Pattern(regexp = "^[a-zA-Z0-9]+$")
  private final String userName;
  ...
}

All these classes have a field “userName” with absolutely similar meaning and similar constraints. One day you decide that you also want to allow more symbols to be used in userNames. So, you have to manually fix all these 3 classes.

Solution #1

Make a base class for all these requests:

public abstract AbstractUserRequest {
  ...
  @NotNull
  @Size(min = 4, max = 16)
  @Pattern(regexp = "^[a-zA-Z0-9]+$")
  private final String userName;
  ...  
}

public class CreateUserRequest extends AbstractUserRequest {
  ... // OK, we have userName here
}

Pros:

  • You now only have to fix the constraints once
  • Creating new types of requests is pretty trivial, you just subclass AbstractUserRequest and it works.

Cons:

  • In case you need userName somewhere else, you’ll have to have a subclass of AbstractUserRequest (example: CreateUserGroupRequest which requires a list of userNames, for sure you want to validate them first)
  • Not excactly sure how to test this properly

Solution #2

Make separate classes for each type of field, so that when you talk about “user name”, you know it’s not just a String, it’s a constrained String:

public class UserNameField {
  @NotNull
  @Size(min = 4, max = 16)
  @Pattern(regexp = "^[a-zA-Z0-9]+$")
  private final String value;
  ...
}

public class CreateUserRequest {
  ...
  private UserNameField userName;
  ...
}

Pros:

  • In case user name constraints change, you’ll only have to fix it once
  • You have a simple class which is easy to test

Cons:

  • Feels like overkill (?)

Solution #3 (update)

Hibernate validator allows constraint composition, so that you can give a name for a group of constraints:

@NotNull
@Size(min = 4, max = 16)
@Pattern(regexp = "^[a-zA-Z0-9]+$")
@Target( { METHOD, FIELD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = {})
@Documented
public @interface UserName {
  String message() default "{com.loki2302.constraints.username}";
  Class<?>[] groups() default {};
  Class<? extends Payload>[] payload() default {};
}
...
public class CreateUserRequest {
  ...
  @UserName
  private final String userName;
  ...
}

Pros:

  • Simple yet powerful approach
  • Easy to use (no extra abstractions)
  • Easy to test

Cons:

  • None probably?

What is the common approach here? Does the Solution #2 look fine? How do you do it?

  • 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-07T14:22:32+00:00Added an answer on June 7, 2026 at 2:22 pm

    I would definitely go for Solution #2. As you demonstrated above, you may want to use UserNameField in a class that isn’t a request, which makes the AbstractRequest class feel like a slightly worse alternative.
    Creating a class to avoid duplicate code and get all logic in the same place sounds like an excellent idea to me, and not overkill at all.

    [EDIT after addition of Solution #3]
    Solution #3 looks like a very nice variation of Solution #2. I can’t really decide which one I like more, they both do a fine job of encapsulating the logic.

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

Sidebar

Related Questions

I have a Metamodel that's built like this: class ModelElement { string id; }
I have two tables, for simplicity lets pretend they're defined like this Stack {
i have a form, which you can pretend is laid out like Windows Explorer:
It seems like eveyone has this problem but no one has a laymans terms
Lets say we have something like: <div class=row> <div class=box> <a class=more href=#more/> </div>
If I have elements with classnames like this: .ses_0 .ses_1 .ses_2 .ses_3 How can
Let's pretend I have a large recipe-database. A table for recipes each with an
Hi can some one point me some guidance, i pretend to pass the value
How can I do these 2 scenarios. Currently I am doing something like this
I have a quick question, (I hope it is quick one). I have fancybox

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.