title: JPA, Hibernate can I do composite primary key which one element is foreign kay @OneToMany?
body: <p>I would like to have in my entity composite primary key consisting of 2 columns (attributes), and have one of them to be foreign key simultaneously. </p>
  
  <p>I write something like this, but don't know whether it works cause foreign key is marked as generated value in IntelliJ data source</p>
  
  <pre><code>@Entity
  @Table(name = "service_point")
  @Access(AccessType.PROPERTY)
  @IdClass(ServicePointId.class)
  public class ServicePoint {
  
      private Long providerId;
      private Integer servicePointNumber;
  
      private Provider provider;
  
      @Id
      @Basic(optional = false)
      @Column(name = "provider_id", nullable = false, insertable = false,
              updatable = false, columnDefinition = "BIGINT UNSIGNED")
      public Long getProviderId() {
          return providerId;
      }
  
      public void setProviderId(Long providerId) {
          this.providerId = providerId;
      }
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Basic(optional = false)
      @Column(name = "service_point_no", nullable = false, columnDefinition = "BIGINT UNSIGNED")
      public Integer getServicePointNumber() {
          return servicePointNumber;
      }
  
      public void setServicePointNumber(Integer servicePointNumber) {
          this.servicePointNumber = servicePointNumber;
      }
  
      @ManyToOne(fetch = FetchType.EAGER)
      @JoinColumn(name = "provider_id")
      public Provider getProvider() {
          return provider;
      }
  
      public void setProvider(Provider provider) {
          this.provider = provider;
      }
  }
  </code></pre>
  
  <p><strong>UPDATED:</strong></p>
  
  <p>I have tested Brian Vosburgh and it works:</p>
  
  <pre><code>transaction.begin();
  
  em.persist(provider);
  
  ServicePoint servicePoint = new ServicePoint(provider, 1);
  
  em.persist(servicePoint);
  
  transaction.commit();
  
  ServicePoint servicePoint2 = em.find(ServicePoint.class,
          new ServicePointId(provider.getUserId(), servicePoint.getServicePointNumber()));
  
  assertTrue("Service point provider id and Provider provider id should be the same.",
          servicePoint2.getProvider().getUserId() == provider.getUserId());
  assertNotNull("Service point number can not be null", servicePoint2.getServicePointNumber());
  assertEquals(servicePoint2.getProvider(), provider);
  
  transaction.begin();
  em.remove(servicePoint);
  em.remove(provider);
  transaction.commit();
  </code></pre>
  
  <p><strong>UPDATE 2 - new problem in next relationship composite PK (3 columns) and 2 of them are composite FK</strong>
  I have been trying to resemble the below solution and couldn't get through
   how to write ServicePointPhotoId @IdClass </p>
  
  <p><img src="https://i.stack.imgur.com/nzhTw.png" alt="extentions of previous example"></p>