gpt4 book ai didi

java - 不可重复的@NamedQuery

转载 作者:行者123 更新时间:2023-11-29 04:40:29 27 4
gpt4 key购买 nike

我想定义一些命名查询,例如:

@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 中收到以下错误消息:

Duplicate annotation of non-repeatable type @NamedQuery. Only annotation types marked @Repeatable can be used multiple times at one target.

在编译时:

java.lang.annotation.AnnotationFormatError: Duplicate annotation for 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(Unknown Source)

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

最佳答案

嘿,你需要定义如下:

@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")
})

关于java - 不可重复的@NamedQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39409622/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com