Two id in hibernate table



  • Is there possibility of two fields being id in a hibernate table? I have two ids that need to be unica in par in the table. But I don't know how to implement it.



  • How to do using @EmbeddedId

    You can do this:

    @Embeddable
    class ClassePrimeira implements Serializable {
    

    @Id
    Integer chaveUm;
    @Id
    Integer chaveDois;

    // get’s e set’s

    }

    @Entity
    public class Classe {

    @EmbeddedId
    private ClassePrimeira id;
    // get’s e set’s
    }

    Note @Id, @IdClass, or @EmbeddedId for Composite Keys: When
    a primary key consists of multiple columns, we need a
    different strategy to group them so that we can allow
    that the persistence tool manipulates key values as a
    only object. In this way, we need to create a class that represents
    that primary key. This class as a general rule must be public,
    must have a default constructor, must be serializable, and must
    implement the hashCode() and equals() methods that allows Hibernate
    test collisions in primary keys.

    Source: https://www.devmedia.com.br/mapeamento-no-hibernate-com-anotacoes/29472

    Although above show the basics of how to declare two id's in one model, you can enter the hibernate documentation: http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/#mapping-declaration-id , and despite being in English in item 5.1.2.1 has a good explanation for what you want to do.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2