不可重复的@NamedQuery

时间:2016-09-09 10:36:32

标签: java hibernate

我想定义几个命名查询,如:

@Entity
@Table(name = "TableA")
@NamedQuery(query = "SELECT i FROM TableA i WHERE i.Id = :Id", name = "findAById")
@NamedQuery(query = "SELECT i FROM TableA i WHERE i.name = :name", name = "findAByName")
public class TableA  
{   
    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    private Long id;

    @Column(name="Name")
    String name = "";

\\... more code


}

但是这样做,我在eclipse中收到以下错误信息:

  

重复注释非可重复类型@NamedQuery。只要   标记为@Repeatable的注释类型可以多次使用一次   目标

并在汇编时:

  

java.lang.annotation.AnnotationFormatError:复制注释   class:interface javax.persistence.NamedQuery:   @ javax.persistence.NamedQuery(lockMode = NONE,hints = [],query = SELECT i   FROM TableA i WHERE i.name =:name,   name = findAByName)at   sun.reflect.annotation.AnnotationParser.parseAnnotations2(未知   源)

对我来说,每个类只有一个名为Query的限制没有多大意义。这是一个错误还是一个功能?是否有解决方法来为每个类定义更多命名查询?

2 个答案:

答案 0 :(得分:2)

嘿,你需要定义如下:

@NamedQueries({
     @NamedQuery(query = "SELECT i FROM TableA i WHERE i.Id = :Id", name = "findAById"),
     @NamedQuery(query = "SELECT i FROM TableA i WHERE i.name = :name", name = "findAByName")
})

答案 1 :(得分:0)

使用以下内容:-

@NamedQueries( 
  value={
     @NamedQuery(name = "findAById", query = "SELECT i FROM TableA i WHERE i.Id = :Id"),
     @NamedQuery(name = "findAByName", query = "SELECT i FROM TableA i WHERE i.name = :name")
  }
)