public class UpdateTimestampTest extends BaseEntityManagerFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] {CustomerJpa.class, AbstractPerson.class};
}
@Test
public void updateParentClassProperty() {
final String id = "1";
TransactionUtil.doInJPA( this::entityManagerFactory, entityManager -> {
CustomerJpa customerJpa = new CustomerJpa();
customerJpa.setIdentity( id );
entityManager.persist( customerJpa );
} );
TransactionUtil.doInJPA( this::entityManagerFactory, entityManager -> {
CustomerJpa customerJpa = entityManager.find( CustomerJpa.class, id );
assertThat( customerJpa.getCreatedAt(), is( not( nullValue() ) ) );
assertThat( customerJpa.getModifiedAt(), is( not( nullValue() ) ) );
assertThat( customerJpa.getCreatedAt(), is( customerJpa.getModifiedAt() ) );
customerJpa.setName( "xyz" );
} );
TransactionUtil.doInJPA( this::entityManagerFactory, entityManager -> {
CustomerJpa customerJpa = entityManager.find( CustomerJpa.class, id );
assertThat( customerJpa.getCreatedAt(), is( not( nullValue() ) ) );
assertThat( customerJpa.getModifiedAt(), is( not( nullValue() ) ) );
assertThat( customerJpa.getCreatedAt(), is( not( customerJpa.getModifiedAt() ) ) );
} );
}
@Test
public void updateSubClassProperty() {
final String id = "1";
TransactionUtil.doInJPA( this::entityManagerFactory, entityManager -> {
CustomerJpa customerJpa = new CustomerJpa();
customerJpa.setIdentity( id );
entityManager.persist( customerJpa );
} );
TransactionUtil.doInJPA( this::entityManagerFactory, entityManager -> {
CustomerJpa customerJpa = entityManager.find( CustomerJpa.class, id );
assertThat( customerJpa.getCreatedAt(), is( not( nullValue() ) ) );
assertThat( customerJpa.getModifiedAt(), is( not( nullValue() ) ) );
assertThat( customerJpa.getCreatedAt(), is( customerJpa.getModifiedAt() ) );
customerJpa.setEmail( "xyz@" );
} );
TransactionUtil.doInJPA( this::entityManagerFactory, entityManager -> {
CustomerJpa customerJpa = entityManager.find( CustomerJpa.class, id );
assertThat( customerJpa.getCreatedAt(), is( not( nullValue() ) ) );
assertThat( customerJpa.getModifiedAt(), is( not( nullValue() ) ) );
assertThat( customerJpa.getCreatedAt(), is( not( customerJpa.getModifiedAt() ) ) );
} );
}
@Entity(name = "person")
@Inheritance(strategy = InheritanceType.JOINED)
public static abstract class AbstractPerson {
@Id
@Column(name = "id")
private String identity;
private String name;
@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created_at", nullable = false, updatable = false)
private Date createdAt;
@UpdateTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "modified_at", nullable = false)
private Date modifiedAt;
public void setIdentity(String identity) {
this.identity = identity;
}
public Date getCreatedAt() {
return createdAt;
}
public Date getModifiedAt() {
return modifiedAt;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Entity
@Table(name = "customer")
public static class CustomerJpa extends AbstractPerson {
private String email;
public void setEmail(String email) {
this.email = email;
}
}
}