Two id in hibernate table

2

Is there a possibility of two fields being id in a hibernate table? I have two ids that need to be unique in pair on the table. But I do not know how to implement it.

    
asked by anonymous 15.12.2017 / 00:25

1 answer

2

You can do it 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  
}
  

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

Source: link

Although above shows the basics of how to declare two ids in the same template, you can enter the hibernate documentation: link , and although it is in English in item 5.1.2.1 has a good explanation for what you want to do.

    
15.12.2017 / 02:40