Я новичок в Spring Data JPA и работаю над двумя объектами, User и Role, с отношениями ManyToOne. Пользовательский JpaRepository может извлекать Роль при выполнении запроса для Пользователя, но ни одно из полей Роли не установлено, кроме @Id Роли, даже если оператор выбора включает в себя выбор соединения.
Что я должен сделать, чтобы убедиться, что контрольный пример может быть пройден?
Вопрос:
java.lang.AssertionError:
Expecting
<com.lenovo.dcg.epic.server.user.Role@36511c97>
to have a property or a field named <"displayName"> with value
<"Administrator">
but value was:
<null>
и журналы:
2018-09-11 23:22:10.885 INFO 56280 --- [ Test worker] o.h.h.i.QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory
Hibernate:
/* select
generatedAlias0
from
User as generatedAlias0
where
generatedAlias0.username=:param0 */ select
user0_.userID as userID1_2_0_,
role1_.roleTypeID as roleType1_1_1_,
user0_.createdBy as createdB2_2_0_,
user0_.createdDate as createdD3_2_0_,
user0_.email as email4_2_0_,
user0_.firstName as firstNam5_2_0_,
user0_.lastMaintainedBy as lastMain6_2_0_,
user0_.lastMaintainedDate as lastMain7_2_0_,
user0_.lastName as lastName8_2_0_,
user0_.RoleTypeID as RoleTyp11_2_0_,
user0_.title as title9_2_0_,
user0_.username as usernam10_2_0_,
role1_.createdBy as createdB2_1_1_,
role1_.createdDate as createdD3_1_1_,
role1_.displayName as displayN4_1_1_,
role1_.lastMaintainedBy as lastMain5_1_1_,
role1_.lastMaintainedDate as lastMain6_1_1_,
role1_.roleTypeDesc as roleType7_1_1_
from
users user0_
left outer join
role_types role1_
on user0_.RoleTypeID=role1_.roleTypeID
where
user0_.username=?
2018-09-11 23:22:11.054 TRACE 56280 --- [ Test worker] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [VARCHAR] - [xxx]
Вот мой пользовательский объект.
@NamedEntityGraph(name = "User.detail", includeAllAttributes = true)
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Integer userID;
@Column(nullable = false, unique = true)
String username;
@Column(nullable = false)
String firstName;
@Column(nullable = false)
String lastName;
@Column(nullable = false)
String email;
@ManyToOne
@JoinColumn(name = "RoleTypeID", nullable = false)
@JsonProperty(value = "RoleTypeID")
@JsonDeserialize(using = UserRoleComponent.UserRoleDeserializer.class)
@JsonSerialize(using = UserRoleComponent.UserRoleSerializer.class)
Role role;
String title;
@JsonIgnore
@CreationTimestamp
Timestamp createdDate;
@Column(nullable = false)
String createdBy;
@Column(nullable = false)
String lastMaintainedBy;
@JsonIgnore
@UpdateTimestamp
Timestamp lastMaintainedDate;
}
и вот моя ролевая сущность.
@Entity
@Table(name = "role_types")
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Integer roleTypeID;
String roleTypeDesc;
String displayName;
@CreationTimestamp
Timestamp createdDate;
String createdBy;
@UpdateTimestamp
Timestamp lastMaintainedDate;
String lastMaintainedBy;
}
Мой репозиторий:
public interface UserRepository extends JpaRepository<User, Integer> {
@EntityGraph(value = "User.detail", type = EntityGraphType.LOAD)
Optional<User> findByUsername(String username);
}
и мой контрольный пример:
@RunWith(SpringRunner.class)
@DataJpaTest(properties = {"spring.jpa.properties.hibernate.format_sql=true",
"spring.jpa.properties.hibernate.use_sql_comments=true",
"logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE"})
public class UserRepositoryTest {
@Autowired
private TestEntityManager entityManager;
@Autowired
private UserRepository repository;
private User user;
private void initRoleType() throws ParseException {
Role admin = new Role();
admin.setRoleTypeDesc("Administrator");
admin.setDisplayName("Administrator");
admin.setCreatedBy("xxx");
admin.setLastMaintainedBy("xxx");
admin = entityManager.persist(admin);
Role manager = new Role();
manager.setRoleTypeDesc("Manager");
manager.setDisplayName("Manager");
manager.setCreatedBy("xxx");
manager.setLastMaintainedBy("xxx");
manager = entityManager.persist(manager);
Role user = new Role();
user.setRoleTypeDesc("User");
user.setDisplayName("User");
user.setCreatedBy("xxx");
user.setLastMaintainedBy("xxx");
user = entityManager.persist(user);
entityManager.flush();
}
@Before
public void setUp() throws Exception {
resetIdentity();
initRoleType();
user = new User();
user.setUsername("xxx");
user.setFirstName("xxx");
user.setLastName("xxx");
user.setEmail("xxx@xxx.com");
Role admin = new Role();
admin.setRoleTypeID(1);
user.setRole(admin);
user.setTitle("Developer");
user.setCreatedBy("admin");
user.setLastMaintainedBy("admin");
}
private void resetIdentity() {
EntityManager em = entityManager.getEntityManager();
Query query = null;
query = em
.createNativeQuery("ALTER TABLE role_types ALTER COLUMN roleTypeID RESTART WITH 1");
query.executeUpdate();
}
@Test
public void testFindUserByName() {
repository.save(user);
entityManager.flush();
Optional<User> user = repository.findByUsername("xxx");
assertThat(user).isNotEmpty();
assertThat(user).hasValueSatisfying(u -> {
assertThat(u).hasFieldOrPropertyWithValue("userID", 1);
assertThat(u.getRole()).isNotNull();
assertThat(u.getRole()).hasFieldOrPropertyWithValue("roleTypeID", 1);
assertThat(u.getRole()).hasFieldOrPropertyWithValue("displayName", "Administrator");
assertThat(u.getDisplayName()).isEqualTo("Administrator");
});
}
}