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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:12:02+00:00 2026-05-23T14:12:02+00:00

I need something like a partial contraint for one of my entities. @Entity public

  • 0

I need something like a partial contraint for one of my entities.

@Entity
public class MyEntity 
{
  @NotNull
  private String name;

  @ManyToOne @NotNull
  private Type type;
}

Only for a sinlge type I need the name to be unique.

Is this possible with a @UniqueConstraint or do I need to implement this with a @PrePersist and @PreUpdate listener? So far I haven’t implemented such a listener, but even if I check the contraint in this listener, does it guarantee to prevent a duplicate entry?

Update

Let’s assume the constraint should only be active for type=special

  • Allowed {id=1,type=normal,name=Test},{id=2,type=normal,name=Test}
  • Not allowed: {id=3,type=special,name=Test},{id=4,type=special,name=Test}
  • 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-23T14:12:03+00:00Added an answer on May 23, 2026 at 2:12 pm

    My understanding of the @UniqueContraint is that it may contain a list of column names that together define the uniqueness.

    See: unique constraint check in JPA

       @UniqueConstraint(columnNames={"name", "type"}) 
    

    I would expect this to enforce uniqueness across name and type. (No time to test though.)

    Edit:

    Aha. So now I understand the problem. How about this for an approach introduce a third attribute:

     int mySpecialValue;
    

    This attribute has no public setter. Instead its value is set by the setType() with logic like this:

     public void setType(String theType){
    
          type = theType;          
    
          if ( "special".equals(type) ){
                  mySpecialValue = 0;
          } else {
                  mySpecialValue = makeUniqueInt(); // eg. some date twiddling
          }
    
     }
    

    Now set the unique constraint to include the mySpecialValue. Hence for all specials the uniqueness depends entirely on the name, for others there is always a differentiator so the names can duplicate.

           Allowed {id=1,type=normal,name=Test, msv = 343223 },
                    {id=2,type=normal,name=Test, msv = 777654 } <== not dup
           Not allowed: {id=3,type=special,name=Test, msv =0 },
                            {id=4,type=special,name=Test, msv =0} <== dup  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I every one i need to something like as shown in the attachment Can
I have a partial class in a dbml file. public partial class Comment string
I need to build a partial Inverted Index . Something like: l = {{x,
I need something like make i.e. dependencies + executing shell commands where failing command
I need something like this http://jonraasch.com/blog/a-simple-jquery-slideshow but w/o the absolute positioning. Is it possible?
I need something like a temporary in-memory key-value store. I know there are solutions
Ok, I need something like this: datediff(second, date_one, date_two) < 1 dates are stored
Normally, we get first value that way: $(#color option:first).val() But I need something like
I want to export a XtraGrid as an image. Pratically i need something like
I need something simple like date , but in seconds since 1970 instead of

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.