我们正在使用弹簧启动1.5.3提供的弹簧数据jpa。在存储库类中,我想从表中获取少量列,包括来自子表的数据(这是onetomany映射)所以,我用查询编写了一个方法。 但该查询方法不起作用,并在日志中看到下面的错误/警告
SQL Error: -104, SQLState: 42601
DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=.;ARRAY + - ROW NEXTVAL PREVVAL NEXT PREVIOUS ( <INTEGER>, DRIVER=4.19.26
这是我的父实体:
@Entity
@Table(name = "parent_table")
public Class Parent {
// no-param constructor
Parent(int id, String name, Child childData) {
// assign these accordingly
}
// id, name, and couple of other mappings
@OneToMany(mappedBy = "parent", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval=true)
private Set<Child> childData;
}
子实体:
@Entity
@Table(name = "child_table")
public class Child {
// id and couple of other columns
@ManyToOne (fetch = FetchType.LAZY)
@JoinColumn(name = "p_id", nullable = false, insertable = false, updatable = false)
private Parent parent;
}
存储库在这里:
@Transactional
public interface ParentRepository extends CrudRepository<Parent, Integer> {
@Query("select new Parent(id, name, p.childData) from Parent p where p.id=?1")
public Parent findOnlyChildDataById(final int id);
}
答案 0 :(得分:0)
不要使用自定义方法通过其'id'获取Entity,请使用本机方法findOne
:
public interface ParentRepository extends CrudRepository<Parent, Integer> {
}
@Service
public ParentService {
@Authoware
prvate ParentRepository repo;
public Parent getParentById(int id) {
return repo.getOne(id);
}
}