我有两个使用OneToMany和ManyToOne关系的实体:
@Entity
@Table(name = TableName.PERSON)
public class Person {
@Id
@Column(name = FieldName.DB_KEY)
public String dbKey;
@Column(name = FieldName.ENTITY_ID)
public String entityId;
@Column(name = FieldName.SORT_KEY)
public String sortKey;
@JoinColumn(name = FieldName.ENTITY_ID, referencedColumnName =
FieldName.ENTITY_ID, insertable = false, updatable = false)
@ManyToOne(fetch = FetchType.EAGER)
@JoinFetch(value = JoinFetchType.INNER)
public WLEntity entity;
}
@Entity
@Table(name = TableName.WL_ENTITY)
public class WLEntity {
@Id
@Column(name = FieldName.ENTITY_ID)
public String entityId;
@Column(name = FieldName.HAS_ADDRESS)
public boolean hasAddress;
@OneToMany(mappedBy = "entity", targetEntity = PersonIndex.class, cascade = CascadeType.ALL)
public List<Person> persons;
}
定义一个findBy的JPA-Repository ...方法:
@Repository
public interface PersonRepository extends JpaRepository<Person, String> {
List<Person> findBySortKeyStartingWith(String sortKey);
}
如果我调用此方法,我可以在控制台中看到:
SELECT t1.DB_KEY, t1.ENTITY_ID, t1.SORT_KEY, t0.ENTITY_ID, t0.HAS_ADDRESS FROM WL_ENTITY t0, PERSON t1 WHERE (t1.SORT_KEY LIKE ? AND (t0.ENTITY_ID = t1.ENTITY_ID))
所以我想要的连接是正确执行的,但是在返回的数据中,实体字段仍为null,但所有其他字段都被填充:
List<Person> persons = personRepository.findBySortKeyStartingWith("Sort");
Person person = persons.get(0);
person.entity == null但是person.entityId已正确填充。
所以我必须做什么,才能让人满意?
我在eclipselink jpa 2.6.4
中使用spring boot