amorfis
amorfis

Reputation: 15770

Validation doesn't work on EntityManager.merge()

I have few validations on my entity, like @NotNull, and some generation, like

@Id
@GeneratedValue(strategy = AUTO)
@Column(name = "ID")
private Long id;

@Column
@GeneratedValue(strategy = GenerationType.AUTO)
private Long referenceNumber;

However when calling EntityManager.merge() this values are not generated. Null fields with @NotNull annotation are passed without any complain. Even id is not generated.

Should I switch this generation on somehow? How, and where?

Upvotes: 4

Views: 1362

Answers (2)

axtavt
axtavt

Reputation: 242686

In addition to kraftan's answer:

  • By default automatic bean validation in JPA 2.0 works if validation provider is "present in the environment", otherwise it silently doesn't work. You can add

    <validation-mode>CALLBACK</validation-mode>
    

    to persistence.xml in order to generate an error if validation provider is not found.

  • JPA doesn't support generation of arbitrary (non-id) properties. Some JPA providers may have extensions.

Upvotes: 1

kraftan
kraftan

Reputation: 6312

Merge() does not invoke pre-insert/pre-update event listeners by default. flush() after the merge() should do it.

Upvotes: 7

Related Questions