I've generated an entity with the command yo jhipster:entity MyEntity
and the following options
{
"relationships": [],
"fields": [
{
"fieldId": 1,
"fieldName": "title",
"fieldType": "String"
}
],
"changelogDate": "20150826154353",
"dto": "no",
"pagination": "no"
}
I've added the auditable columns on liquibase changelog file
<changeSet id="20150826154353" author="jhipster">
<createSequence sequenceName="SEQ_MYENTITY" startValue="1000" incrementBy="1"/>
<createTable tableName="MYENTITY">
<column name="id" type="bigint" autoIncrement="${autoIncrement}" defaultValueComputed="SEQ_MYENTITY.NEXTVAL">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="title" type="varchar(255)"/>
<!--auditable columns-->
<column name="created_by" type="varchar(50)">
<constraints nullable="false"/>
</column>
<column name="created_date" type="timestamp" defaultValueDate="${now}">
<constraints nullable="false"/>
</column>
<column name="last_modified_by" type="varchar(50)"/>
<column name="last_modified_date" type="timestamp"/>
</createTable>
</changeSet>
and modify the MyEntity class to extend AbstractAuditingEntity
@Entity
@Table(name = "MYENTITY")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class MyEntity extends AbstractAuditingEntity implements Serializable {
then run mvn test
and got the folowing exception
[DEBUG] com.example.web.rest.MyEntityResource - REST request to update MyEntity : MyEntity{id=2, title='UPDATED_TEXT'}
javax.validation.ConstraintViolationException: Validation failed for classes [com.example.domain.MyEntity] during update time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
ConstraintViolationImpl{interpolatedMessage='may not be null', propertyPath=createdBy, rootBeanClass=class com.example.domain.MyEntity, messageTemplate='{javax.validation.constraints.NotNull.message}'}
]
at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:160)
at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreUpdate(BeanValidationEventListener.java:103)
at org.hibernate.action.internal.EntityUpdateAction.preUpdate(EntityUpdateAction.java:257)
at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:134)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:349)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
at org.hibernate.event.internal.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:67)
at org.hibernate.internal.SessionImpl.autoFlushIfRequired(SessionImpl.java:1191)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1257)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573)
at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:449)
at org.hibernate.jpa.criteria.compile.CriteriaQueryTypeQueryAdapter.getResultList(CriteriaQueryTypeQueryAdapter.java:67)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:318)
this is the test that's failing
@Test
@Transactional
public void updateMyEntity() throws Exception {
// Initialize the database
myEntityRepository.saveAndFlush(myEntity);
int databaseSizeBeforeUpdate = myEntityRepository.findAll().size();
// Update the myEntity
myEntity.setTitle(UPDATED_TITLE);
restMyEntityMockMvc.perform(put("/api/myEntitys")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(myEntity)))
.andExpect(status().isOk());
// Validate the MyEntity in the database
List<MyEntity> myEntitys = myEntityRepository.findAll();
assertThat(myEntitys).hasSize(databaseSizeBeforeUpdate);
MyEntity testMyEntity = myEntitys.get(myEntitys.size() - 1);
assertThat(testMyEntity.getTitle()).isEqualTo(UPDATED_TITLE);
}
the line that's throwing the exception is this
List<MyEntity> myEntitys = myEntityRepository.findAll();
I've noticed the TestUtil.convertObjectToJsonBytes(myEntity)
method is returning the JSON object representation without the auditable properties -which is expected because of @JsonIgnore annotations- but I suppose the mockMVC.perform update operation isn't honoring the updatable = false
attribute set on createdBy field
@CreatedBy
@NotNull
@Column(name = "created_by", nullable = false, length = 50, updatable = false)
@JsonIgnore
private String createdBy;
how may I make an Entity auditable and have the tests passed?