我有以下两个实体:
class Role {
@Id Long id;
@Column String email;
String role;
@ManyToOne User user;
}
class User {
@Id Long id;
@Column(unique=true) String email;
String password;
@OneToMany(mappedBy = "user")
@OrderBy("role asc")
List<Role> roles;
}
SQL 是:
CREATE TABLE USER (
ID BIGINT NOT NULL,
EMAIL VARCHAR(255) UNIQUE,
PASSWORD VARCHAR(255),
PRIMARY KEY (ID)
)
CREATE TABLE ROLE (
ID BIGINT NOT NULL,
EMAIL VARCHAR(255),
ROLE VARCHAR(255),
USER_ID BIGINT,
PRIMARY KEY (ID),
FOREIGN KEY (USER_ID) REFERENCES USER(ID)
)
以下 JUnit 在倒数第二行失败。我从孩子那里自动加载了用户(父母),但我没有从父母那里自动加载孩子。我做错了什么?
Role r = em.createQuery("select r from Role r where r.email = :email", Role.class)
.setParameter("email", "john@google.com")
.getSingleResult();
assertEquals("registered", r.role());
assertEquals("john@google.com", r.user().email());
User u = em.createQuery("select u from User u where u.email = :email", User.class)
.setParameter("email", "john@google.com")
.getSingleResult();
assertEquals("john@google.com", u.email());
assertEquals("asdf", u.password());
assertEquals(1, u.roles().size()); FAILS HERE!!!
assertEquals("registered", u.roles().get(0).role());
我正在使用 EclipseLink 2.3.2 (JPA)