title: Hibernate transaction and session with multiple save
body: <p>Thanks, let me completely change it.</p>
  
  <p>Using:
  Spring Boot, Hibernate JPA</p>
  
  <p>I have created a link table with a composite primary key across all 3 columns(event_attendee_link_program)</p>
  
  <p>I used the JPA tools in STS IDE to generate Entities from my tables and it came up with the below code. I removed some of the columns to save space.</p>
  
  <p><strong>EventAttendee.java</strong></p>
  
  <pre><code>@Entity
  @Table(name="event_attendee")
  @NamedQuery(name="EventAttendee.findAll", query="SELECT e FROM EventAttendee e")
  public class EventAttendee implements Serializable {
  private static final long serialVersionUID = 1L;
  
  @Id
  @Column(name="attendee_id")
  private long attendeeId;
  
  
  
  //bi-directional many-to-one association to EventAttendeeLinkProgram
  @OneToMany(mappedBy="eventAttendee")
  private List&lt;EventAttendeeLinkProgram&gt; eventAttendeeLinkPrograms;
  
  
  
  public List&lt;EventAttendeeLinkProgram&gt; getEventAttendeeLinkPrograms() {
      return this.eventAttendeeLinkPrograms;
  }
  
  public void setEventAttendeeLinkPrograms(List&lt;EventAttendeeLinkProgram&gt; eventAttendeeLinkPrograms) {
      this.eventAttendeeLinkPrograms = eventAttendeeLinkPrograms;
  }
  
  public EventAttendeeLinkProgram addEventAttendeeLinkProgram(EventAttendeeLinkProgram eventAttendeeLinkProgram) {
      getEventAttendeeLinkPrograms().add(eventAttendeeLinkProgram);
      eventAttendeeLinkProgram.setEventAttendee(this);
  
      return eventAttendeeLinkProgram;
  }
  
  public EventAttendeeLinkProgram removeEventAttendeeLinkProgram(EventAttendeeLinkProgram eventAttendeeLinkProgram) {
      getEventAttendeeLinkPrograms().remove(eventAttendeeLinkProgram);
      eventAttendeeLinkProgram.setEventAttendee(null);
  
      return eventAttendeeLinkProgram;
  }
  }
  </code></pre>
  
  <p><strong>EventAttendeeLinkProgram.java</strong></p>
  
  <pre><code>@Entity
  @Table(name="event_attendee_link_program")
  @NamedQuery(name="EventAttendeeLinkProgram.findAll", query="SELECT e FROM EventAttendeeLinkProgram e")
  public class EventAttendeeLinkProgram implements Serializable {
  private static final long serialVersionUID = 1L;
  
  @EmbeddedId
  private EventAttendeeLinkProgramPK id;
  
  //bi-directional many-to-one association to EventAttendee
  @ManyToOne
  @JoinColumn(name="attendee_id", insertable=false, updatable=false)
  private EventAttendee eventAttendee;
  
  //bi-directional many-to-one association to EventOptionsAttendeeType
  @ManyToOne
  @JoinColumn(name="attendee_type_id", insertable=false, updatable=false)
  private EventOptionsAttendeeType eventOptionsAttendeeType;
  
  //bi-directional many-to-one association to EventProgram
  @ManyToOne
  @JoinColumn(name="program_id", insertable=false, updatable=false)
  private EventProgram eventProgram;
  
  public EventAttendeeLinkProgram() {
  }
  
  public EventAttendeeLinkProgramPK getId() {
      return this.id;
  }
  
  public void setId(EventAttendeeLinkProgramPK id) {
      this.id = id;
  }
  
  public EventAttendee getEventAttendee() {
      return this.eventAttendee;
  }
  
  public void setEventAttendee(EventAttendee eventAttendee) {
      this.eventAttendee = eventAttendee;
  }
  
  public EventOptionsAttendeeType getEventOptionsAttendeeType() {
      return this.eventOptionsAttendeeType;
  }
  
  public void setEventOptionsAttendeeType(EventOptionsAttendeeType eventOptionsAttendeeType) {
      this.eventOptionsAttendeeType = eventOptionsAttendeeType;
  }
  
  public EventProgram getEventProgram() {
      return this.eventProgram;
  }
  
  public void setEventProgram(EventProgram eventProgram) {
      this.eventProgram = eventProgram;
  }
  
  }
  </code></pre>
  
  <p><strong>EventAttendeeLinkProgramPK.java</strong></p>
  
  <pre><code>@Embeddable
  public class EventAttendeeLinkProgramPK implements Serializable {
  //default serial version id, required for serializable classes.
  private static final long serialVersionUID = 1L;
  
  @Column(name="attendee_id", insertable=false, updatable=false)
  private int attendeeId;
  
  @Column(name="attendee_type_id", insertable=false, updatable=false)
  private int attendeeTypeId;
  
  @Column(name="program_id", insertable=false, updatable=false)
  private int programId;
  
  public EventAttendeeLinkProgramPK() {
  }
  public int getAttendeeId() {
      return this.attendeeId;
  }
  public void setAttendeeId(int attendeeId) {
      this.attendeeId = attendeeId;
  }
  public int getAttendeeTypeId() {
      return this.attendeeTypeId;
  }
  public void setAttendeeTypeId(int attendeeTypeId) {
      this.attendeeTypeId = attendeeTypeId;
  }
  public int getProgramId() {
      return this.programId;
  }
  public void setProgramId(int programId) {
      this.programId = programId;
  }
  
  public boolean equals(Object other) {
      if (this == other) {
          return true;
      }
      if (!(other instanceof EventAttendeeLinkProgramPK)) {
          return false;
      }
      EventAttendeeLinkProgramPK castOther = (EventAttendeeLinkProgramPK)other;
      return 
          (this.attendeeId == castOther.attendeeId)
          &amp;&amp; (this.attendeeTypeId == castOther.attendeeTypeId)
          &amp;&amp; (this.programId == castOther.programId);
  }
  
  public int hashCode() {
      final int prime = 31;
      int hash = 17;
      hash = hash * prime + this.attendeeId;
      hash = hash * prime + this.attendeeTypeId;
      hash = hash * prime + this.programId;
  
      return hash;
  }
  }
  </code></pre>
  
  <p><strong>EventAttendeeServiceImpl.java</strong></p>
  
  <pre><code>@Service
  @Primary
  public class EventAttendeeServiceImpl implements EventAttendeeService {
  
  @Autowired
  private EventAttendeeRepository eventAttendeeRepository;
  
  @Autowired
  private EventOptionsAttendeeTypeRepository eventOptionsAttendeeTypeRepository;
  
  @Autowired
  private EventProgramRepository eventProgramRepository;
  
  @Override
  @Transactional
  public String addEventAttendee(EventAttendee eventAttendee) {
  
          EventAttendeeLinkProgram ep = new EventAttendeeLinkProgram();
          ep.setEventOptionsAttendeeType(eventOptionsAttendeeTypeRepository.findOne(2L));
          ep.setEventProgram(eventProgramRepository.findOne(2L));
  
          eventAttendee.setEventAttendeeLinkPrograms(new ArrayList&lt;&gt;());
          eventAttendee.getEventAttendeeLinkPrograms().add(ep);
  
    eventAttendeeRepository.save(eventAttendee);
  
      return "";
  }
  </code></pre>
  
  <p>With this in place, my code is not throwing any errors. It is saving the EventAttendee, but nothing is being saved to the EventAttendeeLinkProgram. Please Note: I am trying so save both EventAttendee and EventAttendeeLinkProgram entities. So I think hibernate should be smart enought to forst save EventAttendee and generating the Id for it, then use that Id to store in EventAttendeeLinkProgram.</p>