Primary key: -identifies a row uniquely in a table Composite Primary Key -combination of fields that uniquely identify a row 2 ways to create composite key: 1. @Embeddable 2.@Idclass @Embeddable Key class> -separate class for key fields with @Embeddable in class -the composite key class must be public,implements Serializable, have no args constructor, implement equals and hashcode. Entity class> -Use the key class instead of separate fields in entity for keys -Use @EmbeddedId for composite key class field -for one of the composite key fields having a relation with another entity @MapsId("field_name") must be used for mapping FK -Postman gives Object hierarchy in response body of entity @Idclass Key class> -key fields specified in a separate class Entity class> -key fields specified AGAIN with @Id for each field -@IdClass(KeyClass) -Postman response for entity gives a flat hierarchy of fields
@hapatworld2 жыл бұрын
can we do in same file ?
@roshnisannapu25133 жыл бұрын
Can you share the schema of the tables
@goku62723 жыл бұрын
As we know we can define one primary key in a table , can we use primary key with composite primary key .
@ladeharish55144 жыл бұрын
As usual great explanation. Interview question - if our table doesn't have primary key, then how will write repository interface for that table. Pls answer...
@ByteProgrammingVedantHarish4 жыл бұрын
@Lade Harish : This is a great question. It really test your depth understanding about how @Id works in JPA. Scenario : You want to access some data from third party/external system table. But they have exposed a View to you, for read-only. Solution: 1. You need to identify a column or set of columns that can act as unique for this View. (Hint : If you observe, it is same definition of Primarykey). 2. Once you identify, Mark the field/fields with @Id annotation 3. If single field, we can directly pass datatype in the interface but if multiple fields, do like as we do for composite primary key. Few points i want to highlight : 1. Every entity should have Primary Key defined. Your scenario might be of View..That's why Interviewer has used "table" instead of Entity of View. 2. @Id annotation does not need to be used only when you have primary key in table. 3. If we are able to uniquely identify data with particular field/fields in a table, we can still use those fields with @Id annotation. Hope this helps!
@pratik199014 жыл бұрын
Hello, its nice explanation..thank you. I have some other queries on springboot and Rest. Can you plz share your emailId?