3

Please, consider the following example:

import org.hibernate.annotations.NaturalId;

import javax.persistence.Column;
import javax.persistence.Table;

@javax.persistence.Entity
@Table(name = "Entity", uniqueConstraints = {
        @javax.persistence.UniqueConstraint(columnNames = {"firstNaturalId"}),
        @javax.persistence.UniqueConstraint(columnNames = {"secondNaturalIdPart1", "secondNaturalIdPart2"})
})
class Entity {

    @NaturalId
    @Column(name = "firstNaturalId")
    private String firstNaturalId;

    @NaturalId
    @Column(name = "secondNaturalIdPart1")
    private String secondNaturalIdPart1;

    @NaturalId
    @Column(name = "secondNaturalIdPart2")
    private String secondNaturalIdPart2;

    // The remainder is omitted.

}

The desired functionality is to be able to retrieve an identified uniquely entity either by providing ('firstNaturalId') or the ('secondNaturalIdPart1', 'secondNaturalIdPart2') group.

Is it possible in Hibernate to have several natural identifiers combinations (groups) that uniquely identify an entity within a table?

Pavel
  • 4,912
  • 7
  • 49
  • 69
  • Possible duplicate of [How to map a composite key with Hibernate?](http://stackoverflow.com/questions/3585034/how-to-map-a-composite-key-with-hibernate) – 1615903 Nov 03 '16 at 09:47
  • Thank you for the reference! Unfortunately, the question that you referred doesn't answer this question. – Pavel Nov 03 '16 at 09:53

1 Answers1

0

A long time has passed. Maybe you've got your answer. I had this question in my mind. Found your question with no answer. Searched more. Found this Natural Id Mapping.

public class Entity{
    
    @NaturalId @Embedded
    private EntityNI entityNI;

    public Entity(){}
    
//    Setter and Getter is omitted
//    .
//    .
//    .

    @Override
    public int hashCode(){
        return (entityNI != null ? entityNI.hashCode() : 31);
    }

    @Override
    public boolean equals(final Object object){
        if(this == object){
            return true;
        }
        if( !(object instanceof Entity) || (getClass() != object.getClass()) ){
            return false;
        }
        return this.entityNI.equals( ((Entity)object).entityNI );
    }
}

And the embedded natural ids:

@Embeddable
public class EntityNI{
    
    @NotBlank @Basic(optional = false) @Column(name = "firstNaturalId")
    private String firstNaturalId;

    @NotBlank @Basic(optional = false) @Column(name = "secondNaturalIdPart1")
    private String secondNaturalIdPart1;

    @NotBlank @Basic(optional = false) @Column(name = "secondNaturalIdPart2")
    private String secondNaturalIdPart2;
    
    public EntityNI(){}
    
//    Setters and Getters are omitted
//    .
//    .
//    .

    @Override
    public int hashCode(){
        return Objects.hash(firstNaturalId, secondNaturalIdPart1, secondNaturalIdPart2);
    }

    @Override
    public boolean equals(Object object){
        if(this == object){
            return true;
        }
        if( !(object instanceof EntityNI) || (getClass() != object.getClass()) ){
            return false;
        }
        final EntityNI other = (EntityNI) object;
        return (this.firstNaturalId == other.firstNaturalId) && (this.secondNaturalIdPart1 == other.secondNaturalIdPart1) && (this.secondNaturalIdPart2 == other.secondNaturalIdPart2);
    }
}
Arash
  • 696
  • 8
  • 24
  • Thank you for your answer. Unfortunately, it doesn't look like it answers the question. The natural IDs are considered as you can see in the example provided in the question, it's not a solution, unfortunately. – Pavel Apr 26 '21 at 21:14
  • @Pavel, sorry I could not help you. – Arash May 09 '21 at 14:57