title: How should I insert into my table composed of a composite key with JPA?
body: <p>I'm a beginner in JPA-Hibernate stuff and I wanted to know how should I update my join table declared in my PostgreSQL database.</p>
  
  <p>There is a look on my tables :</p>
  
  <p><strong>Shelters Table</strong></p>
  
  <p><a href="https://i.stack.imgur.com/FWEYw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FWEYw.png" alt="enter image description here"></a></p>
  
  <p><strong>Images Table</strong></p>
  
  <p><a href="https://i.stack.imgur.com/ySZAH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ySZAH.png" alt="enter image description here"></a></p>
  
  <p><strong>SheltersXImages Table</strong></p>
  
  <p><a href="https://i.stack.imgur.com/QPQ1p.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QPQ1p.png" alt="enter image description here"></a></p>
  
  <p>I would like to update my table SheltersXImages which have a unique composite key composed of 2 foreign keys : "str_id" from the Shelters Table and "img_id" from the Images Table.</p>
  
  <p>Should I :</p>
  
  <ul>
  <li>Create an entity to represent SheltersXImages Table and create a repository to insert into the table SheltersXImages manually ?</li>
  <li>Find a better idea ? I found some subjects about @IdClass @EmbeddedId or @JoinTable annotations but as I said I'm a beginner and I don't really understand if I could use them to solve my problem and which solution is the most appropriate.</li>
  </ul>
  
  <p>There is a look on my entities at the moment :</p>
  
  <p><strong>Shelter Entity</strong></p>
  
  <pre><code>open class SheltersStrEntity {
  
  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "shelters_generator")
  @SequenceGenerator(name="shelters_generator", sequenceName = "v1.shelters_str_str_id_seq", initialValue = 1, allocationSize = 1)
  @Column(name = "str_id", nullable = false, insertable = false, updatable = false)
  open var strId: Long? = null
  @Basic
  @Column(name = "str_siren", nullable = false)
  open var strSiren: String? = null
  @Basic
  @Column(name = "str_nic", nullable = false)
  open var strNic: String? = null
  @Basic
  @Column(name = "str_name", nullable = false)
  open var strName: String? = null
  @Basic
  @Column(name = "str_email", nullable = false)
  open var strEmail: String? = null
  @Basic
  @Column(name = "str_address", nullable = false)
  open var strAddress: String? = null
  @Basic
  @Column(name = "str_zip_code", nullable = false)
  open var strZipCode: String? = null
  @Basic
  @Column(name = "str_city", nullable = false)
  open var strCity: String? = null
  @Basic
  @Column(name = "str_phone", nullable = false)
  open var strPhone: String? = null
  @Basic
  @Column(name = "str_active", nullable = false)
  open var strActive: Boolean? = null
  @Basic
  @Column(name = "str_creation_datetime", nullable = false)
  open var strCreationDatetime: OffsetDateTime? = null
  @Basic
  @Column(name = "str_edit_datetime", nullable = false)
  open var strEditDatetime: OffsetDateTime? = null
  
  //Equals and hashcode ...
  }
  </code></pre>
  
  <p><strong>Image Entity</strong></p>
  
  <pre><code>@Entity
  @Table(name = "images_img", schema = "v1", catalog = "hachiko")
  open class ImagesImgEntity {
  
  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "images_generator")
  @SequenceGenerator(name="images_generator", sequenceName = "v1.images_img_img_id_seq", initialValue = 1, allocationSize = 1)
  @Column(name = "img_id", nullable = false, insertable = false, updatable = false)
  open var imgId: Long? = null
  @Basic
  @Column(name = "img_name", nullable = false)
  open var imgName: String? = null
  @Basic
  @Column(name = "img_content", nullable = false)
  open var imgContent: ByteArray? = null
  
  //Equals and hashcode ...
  }
  </code></pre>
  
  <p>Thanks guys in advance</p>