我正试图从我使用jpa存储库的列表中获取分页。 List实际上是另一个对象的属性。它是,有点像,好消息,留言板...长话短说,Tiket类有一个Poruka列表,我想要检索和分页。这是它的样子:
@Entity
@Table(name = "tiket")
public class Tiket implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name="id")
private Long id;
@Column(name="naslov")
@NotEmpty(message= "Morate unijeti naslov tiketa")
private String naslov;
@Column(name = "tiket_datum")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@NotNull
private Date tiketDatum;
@Column(name = "rijesen_datum")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date rijesenDatum;
@ManyToOne(cascade = {CascadeType.REFRESH}, fetch=FetchType.EAGER )
@JoinColumn(nullable=false)
@NotNull
private Korisnik korisnik;
@OneToMany(cascade = {CascadeType.ALL}, fetch=FetchType.EAGER )
@GenericGenerator(name="uuid-gen", strategy = "increment")
@CollectionId(columns = @Column(name = "collection_id"), generator = "uuid-gen", type = @Type(type = "long"))
private List<Poruka> poruke = new ArrayList<Poruka>();
行。 Poruka清单是我需要的。 所以我尝试在PorukaRepository中创建自定义查询,通过这样做得到Poruka的分页列表:
public interface PorukaRepository extends JpaRepository<Poruka, Long> {
@Query("select t.poruke from Tiket t where t.id=?1")
Page<Poruka> findAllPoruke(Long tid, Pageable pageable);
}
并收到以下错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') as col_0_0_ from tiket2 tiket2x0_, tiket2_poruka poruke1_, poruka poruka2_ whe' at line 1
然后尝试更改查询,因为数据库有tiket_poruka表,对此:
@Query(value="SELECT * FROM poruka p where p.id IN (SELECT tp.poruke_id FROM tiket_poruka tp WHERE tp.tiket_id=?0)", nativeQuery = true)
每次在编译期间收到以下错误:
No property find found for type ba.fit.vms.pojo.Poruka
然后试图把它放在TiketRepository中......没有运气。然后我添加了order by并排序到我的查询,仍然没有运气。
这次错误是它无法从Tiket转换为Poruka
当然,我可以检索列表并在我的控制器中对其进行分页,但我想学习并查看是否可行,因为它似乎合乎逻辑。我需要创建自定义存储库吗?或者只是我的查询错了?
这是Poruka.class:
@Entity
@Table(name = "poruka")
public class Poruka implements Serializable{ //, Comparable<Poruka>
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name="id")
private Long id;
@Column(name="sadrzaj", length = 255)
@NotEmpty(message= "Sadrzaj ne moze biti prazan")
private String sadrzaj;
@Column(name = "datum")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@NotNull
private Date datum;
@OneToOne(fetch=FetchType.EAGER)
@JoinColumn(name="korisnik_id", nullable=true)
private Korisnik korisnik;
@OneToOne(fetch=FetchType.EAGER)
@JoinColumn(name="prethodna_id", nullable=true)
private Poruka prethodni;
当然,课程没有getter和setter ......