34

I have two questions concerning @JoinFormula and @OneToMany annotations:

  1. How can I limit the number of result with @JoinFormula and @OneToMany annotations?

  2. How can I define that id in expression author = id refers to Author.id?

    Author {
    
        @Id
        private Long id;
    
        @OneToMany
        @JoinFormula(value = "SELECT a FROM Article a WHERE author = id AND schedule < CURRENT_TIMESTAMP()") // limit = 15
        private List<Article> pastArticles;
    }
    

Like this, I keep having the pastArticles empty, even when I remove the schedule < part of the clause.

Thanks!

DataNucleus
  • 15,497
  • 3
  • 32
  • 37
Vojtěch
  • 11,312
  • 31
  • 103
  • 173
  • For 2 you can't, don't know if 1 is possible. Maybe 1 is possible using an SQL query limit. – siebz0r Jun 01 '12 at 12:43
  • As for the limitation I found this: http://stackoverflow.com/questions/7894931/onetomany-mapping-list-size-limit – siebz0r Jun 01 '12 at 12:48

2 Answers2

20

Answer 1 :

@Size(max=10)
private List<Comment> commentList;

Answer 2 :(just example like that)

public class A{

    @Id
    @GeneratedValue
    private Integer id;

    private String uuid;

    ...
  }

other class

public class B{
      @Id 
      @GeneratedValue
      private Integer id;

      private String uuidOfA;



  @ManyToOne
  @JoinColumnsOrFormulas({
  @JoinColumnOrFormula(formula=@JoinFormula(value="(SELECT a.id FROM A a WHERE a.uuid = uuid)", referencedColumnName="id")),
  @JoinColumnOrFormula(column = @JoinColumn("uuidOfA", referencedColumnName="uuid"))
})

     private A a;      
}
Jubin Patel
  • 1,959
  • 19
  • 38
  • 11
    Some explanation would be nice. I know that I am probably just too stupid, but I dont understand how the annotations in Answer 2 work together – phil294 Feb 21 '17 at 12:17
  • 1
    Perfect! I had to read multiple time your other class of answer 2. But once assimilated, I was able to fix my problem. Thanks a lot for your contribution! – Camille Mar 13 '22 at 11:13
3

you'd be better off using the @Where annotation to limit the results

Micha Roon
  • 3,957
  • 2
  • 30
  • 48