我有一个正常工作的jpa实体,但在我添加了新的getter和setter之后,它返回以下异常:
Caused by: java.lang.IllegalArgumentException: NamedQuery of name: BedOccupation.findByDateofstay not found. at org.eclipse.persistence.internal.jpa.QueryImpl.getDatabaseQueryInternal(QueryImpl.java:351) at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createNamedQuery(EntityManagerImpl.java:1124) at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createNamedQuery(EntityManagerImpl.java:1144) at tcyh.BedOccupationModel.fetchBO(BedOccupationModel.java:42) at tcyh.BedOccupationModel.fetchBO(BedOccupationModel.java:48) at tcyh.MainWindowController.selectTab(MainWindowController.java:382) at tcyh.MainWindowController.initialize(MainWindowController.java:184) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548) ... 14 more
我肯定在实体BedOccupation中定义了命名查询,所以我想这只是因为获取或设置List而不是String的getter和setter。 以下是一些代码:
BedOccupation fetchBO(Date date) {
EntityManager em = ConnectTCYHPU.getInstance().getEM();
TypedQuery tquery = em.createNamedQuery("BedOccupation.findByDateofstay", BedOccupation.class);
tquery.setParameter("dateofstay", date);
return (BedOccupation) tquery.setHint(QueryHints.REFRESH, HintValues.TRUE).getSingleResult();
}
....
@Entity
@Table(name = "BEDOCCUPATION")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "BedOccupation.findByDateofstay", query = "SELECT r FROM BedOccupation r WHERE r.dateofstay = :dateofstay"),
....
public class BedOccupation implements Serializable, Cloneable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "DATEOFSTAY")
@Temporal(TemporalType.DATE)
private Date dateofstay;
@Basic(optional = false)
@Column(name = "MDORM")
private String mdorm;
....
//These are getter/setter generated by NetBeans
public String getMDorm() {
return mdorm;
}
public void setMDorm(String mdorm) {
this.mdorm = mdorm;
}
//These are getter/setter I added.
public List<String> getMDormList() {
return CSVToList(mdorm);
}
public void setMDormList(List<String> mdormList) {
this.mdorm = listToCSV(mdormList);
}
....
private List<String> CSVToList(String str) {
List list = new ArrayList<>();
list.addAll(Arrays.asList(str.split(",", -1)));
return list;
}
private String listToCSV(List list) {
StringBuilder strb = new StringBuilder();
for (int i = 0; i < list.size(); i++) {
strb.append(list.get(i));
if (i < list.size() - 1) {
strb.append(",");
}
}
return strb.toString();
}
我是否需要添加一些注释或执行其他操作,添加与字段无关的setter / getter?
答案 0 :(得分:0)
我发现我的第一篇文章中的错误是由实体类BedOccupation中定义的这些方法引起的:
static Comparator<String> compare(){
return (String a, String b) -> {
if (a.equals("")) {
return b.equals("") ? 0 : 1;
} else if (b.equals("")) {
return -1;
} else {
return a.compareTo(b);
}
};
}
static Comparator<String> compareOrderDesc(){
return (String aStr,String bStr) -> {
if(aStr.equals("")) return 1;
else if (bStr.equals("")) return -1;
String[] a = aStr.split("_");
String[] b = bStr.split("_");
if (a[0].equals(b[0]) && a.length >= 2 && b.length >= 2){
return - Integer.decode(a[1])
.compareTo(Integer.decode(b[1]));
}
return Integer.decode(a[0])
.compareTo(Integer.decode(b[0]));
};
}
我想在这个方法中做的是根据每个第一个值的第二个值,用“_”分隔数字。
当我按如下所示更改这些代码时,不会发生错误:
static Comparator<String> compare(){
return new Comparator() {
@Override
public int compare(Object o1, Object o2) {
throw new UnsupportedOperationException("Not supported yet.");
}
};
}
static Comparator<String> compareOrderDesc(){
return new Comparator() {
@Override
public int compare(Object o1, Object o2) {
throw new UnsupportedOperationException("Not supported yet.");
}
};
}
我需要更多地搜索它,但它会是一个错误吗? 无论如何,谢谢你的帮助,我会试着自己解决这个问题。