A failed save attempt (EntityManager
persist) leaves my entity with an id.
In my case, I'm returning to the view with any ConstraintViolationException
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.
When this code is run within my Spring MVC action method on a new unsaved event:
eventRepository.save(event);
and throws an exception (caused by ConstraintViolationException
) the event entity which is in Spring's org.springframework.ui.Model
now contains an id! Why?
The EventRepository
used exetends from Spring Data JPA's JpaRepository
:
public interface EventRepository extends JpaRepository<Event, Long> {}
Here's a snippet of the Event
entity bean:
@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;
...