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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:35:29+00:00 2026-06-04T08:35:29+00:00

Hibernate UnUniqueify a column in table(Solved) I want a field set to be non-unique

  • 0

Hibernate UnUniqueify a column in table(Solved)


I want a field set to be non-unique on itself but to be unique in combination with the other field, I got this table with two columns(composite primary keys); id (primary key) and object_proxy_id (primary key), this is exactly what I need but hibernate sets the object_proxy_id to be unique on itself so that value cant be duplicate in the table, and I need this column to accept duplicate values. Because every user has its own object proxy and these proxy’s don’t have to be necessarily unique.

This is what I want to achieve:

|-------------------------------|
| tbl_object_proxy              |
| ------------------------------|
| Id (pk)| object_proxy_id (pk) |
|-------------------------------|
| 1      | 150 --               |
| 1      | 149  |= must be able to be DUPLICATE which is not the case right now.
| 2      | 150 --               |
| 2      | 151                  |
|-------------------------------|

Current code:

@Entity
@Table(name = "tbl_user_settings", uniqueConstraints = {@UniqueConstraint(columnNames={"user_id"})})
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)

public class Settings implements Serializable
{
@Id
@SequenceGenerator(name="someSequence", sequenceName="SEQ_SOMENAME", allocationSize =1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="someSequence")
@Column(name="id")
private int setting_id;

@OneToOne
private User user;

@ManyToOne
private SomeObject someobject;

@ElementCollection
@CollectionTable(name="tbl_collection_name", joinColumns=
@JoinColumn(name="id"), uniqueConstraints = {@UniqueConstraint(columnNames={"id", "object_proxy_id"})})
@Column(name="SomeObject")
private Set<SomeObject> objectProxy;

/*...constructors and methods...*/
}

Results in:

-- Table schema
|-------------------|                    
| tbl_user_settings |                        
|-------------------|                        
| id                |PK <<Unique>>                      
| user_id           |FK reference tbl_user <<Unique>>                        
| object_id         |FK reference tbl_object  
|-------------------|

|------------------|
| tbl_object_proxy |
|------------------|
| id               |PK reference tbl_user_settings 
| object_proxy_id  |PK reference tbl_object <<Unique>> BUT I DON'T WANT THIS TO BE UNIQUE ON ITSELF !!!!
|------------------|

EDIT:
The two primary key’s in tbl_object_proxy are composite primary key’s
I have tried Xeon’s solution but it didn’t work.

  • 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-04T08:35:30+00:00Added an answer on June 4, 2026 at 8:35 am

    Short answer: replace the @ElementCollection by a @ManyToMany relation with a @JoinTable like this:

    @ManyToMany
    @JoinTable(
    name="tbl_settings_objecteproxy_v2",
    joinColumns = @JoinColumn(name = "id"),
    inverseJoinColumns = @JoinColumn( name = "objectproxy_id"))
    private Set<SomeObject> objectproxy;
    

    See “2.2.5.3.2.1. Definition” in Hibernate Annotation Documentation

    This results in a same side table but then without the unique constraint.
    So now this is possible:

    |-------------------------------|
    | tbl_object_proxy              |
    | ------------------------------|
    | Id (pk)| object_proxy_id (pk) |
    |-------------------------------|
    | 1      | 150 --               |
    | 1      | 149  |= It works! The unique constraint is gone! 
    | 2      | 150 --               |
    | 2      | 151                  |
    |-------------------------------|
    


    Detailed answer and cause description:
    Somehow the @ElementCollection created a collectiontable with a one to many relation of the referenced key (collection | inverse join) which adds a unique constraint to the key referencing the other side table to reflect the one to many relationship which I didn’t want. So I dropped the @ElementCollection and replaced it by a @ManyToMany relation with a @JoinTable annotation. I have also tried to declare the @ManyToMany relation in the @ElementCollection but it kept adding the Unique constraint to the referenced key.

    My Settings class does now look like this:

    @Entity
    @Table(name = "tbl_user_settings", uniqueConstraints = {@UniqueConstraint(columnNames={"user_id"})})
    @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
    
    public class Settings
    {
    @Id
    @SequenceGenerator(name="someSequence", sequenceName="SEQ_SOMENAME", allocationSize =1)
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="someSequence")
    @Column(name="id")
    private int setting_id;
    
    @OneToOne
    private User user;
    
    @ManyToOne
    private SomeObject someobject;
    
    @ManyToMany
    @JoinTable(
    name="tbl_settings_objecteproxy_v2",
    joinColumns = @JoinColumn(name = "id"),
    inverseJoinColumns = @JoinColumn( name = "objectproxy_id"))
    private Set<SomeObject> objectProxy;
    
    /*...the rest...*/
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hibernate provides a mechanism to define multi column indexes via the Table . Is
So Hibernate Supports the latest Version of Firebird, which is really great. But... I
Using hibernate, how can I persist a class with a List<String> field? Consider the
JPA/Hibernate missing column I am using JPA with Hibernate as the provider. I had
In hibernate mapping, I have set the property lazy=false , and this fetches all
In hibernate I want to run this JPQL / HQL query: select new org.test.userDTO(
Hibernate: I want to have hibernate automatically generate my tables with names in lowercase.
Hibernate is not deleting orphans when I set the collection to null, although orphans
Most Hibernate tutorials use Maven for handling the build, but I'm looking for a
Assume Hibernate for the ORM. I'm not sure how to ask this. I want

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.