title: Why is JPA entity given an id by Spring Data JPA when ConstraintViolationException thrown (bean validation)
body: <p>A failed save attempt (<code>EntityManager</code> persist) leaves my entity with an id.</p>
  
  <p>In my case, I'm returning to the view with any <code>ConstraintViolationException</code> thrown as error messages along with the original entity.  The view checks to see if the entity has an id to include in the form action url used when submitting an event.  The controller then attempts to load the entity by id.</p>
  
  <p>When this code is run within my Spring MVC action method on a new unsaved event:</p>
  
  <pre><code>eventRepository.save(event);
  </code></pre>
  
  <p>and throws an exception (caused by <code>ConstraintViolationException</code>) the event entity which is in Spring's <code>org.springframework.ui.Model</code> <strong>now contains an id!</strong>  Why?</p>
  
  <p>The <code>EventRepository</code> used exetends from Spring Data JPA's <code>JpaRepository</code>:</p>
  
  <pre><code>public interface EventRepository extends JpaRepository&lt;Event, Long&gt; {}
  </code></pre>
  
  <p>Here's a snippet of the <code>Event</code> entity bean:</p>
  
  <pre><code>@Entity
  public class Event {
  
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "EVENT_SEQ")
    @SequenceGenerator(name = "EVENT_SEQ", sequenceName = "ID_SEQ", allocationSize = 1)
    private Long id;
  
    @NotNull(message = "{event.proctor.null}")
    @ManyToOne
    @JoinColumn(name="PROCTOR_EMPLOYEE_ID")
    private Employee proctor;
  
    @NotNull(message = "{event.date.time.null}")
    @Column(name="EVENT_DATE_TIME")
    @Convert(converter = LocalDateTimePersistenceConverter.class)
    @DateTimeFormat(pattern = "yyyy-MM-dd HHmm")
    private LocalDateTime eventDateTime;
  ...
  </code></pre>