Hi all,
I've got an entity with a column (which is also the candidate key and the field used for database equality) defined as follows:
Code:
@Column(name = "ROLE_NAME", updatable = false, nullable = false, unique = true)
/** The role name */
private final String roleName;
I defined the column as
updatable = false. I was expecting that whenever I set a different value in roleName for the same PK, HB would throw an exception (pls note I haven't got a setter method for this column). I was able to "hack" the value with the following code:
Code:
@Test(groups = { "integration" })
public void testUpdateRoleNameGeneratesException() throws Exception {
Role role = new Role(NULL_ID, ROLE_NAME);
Long rolePk = roleDao.saveOrUpdate(role);
Role actualRole = this
.validateAndReturnRoleAfterInsertion(rolePk, role);
System.err.println("actualRole: " + actualRole);
// It now tries to update the ROLE name and this should result in an
// exception
Field roleName = Role.class.getDeclaredField("roleName");
Assert.assertNotNull(roleName,
"Couldn't find the role name field in the Role entity!");
// Hack
roleName.setAccessible(true);
roleName.set(role, "hackedValue");
// This should generate an exception, since the name is a read-only
// field, but it doesn't
Long updatedRolePk = roleDao.saveOrUpdate(role);
Role hackedRole = roleDao.findByPk(Role.class, updatedRolePk);
System.err.println("hacked role: " + hackedRole);
}
The test above generates the following output:
actualRole: Role ( id = 25 roleName = roleName version = 0 )
hacked role: Role ( id = 25 roleName = hackedValue version = 0 )
Is this normal?