Java 使用带有“的方法”@命名为;其参数中的注释
我和JPA一起工作,我有一个方法Java 使用带有“的方法”@命名为;其参数中的注释,java,jpa,Java,Jpa,我和JPA一起工作,我有一个方法 /** * This method gets the entity having primary key id. It uses HTTP GET method. * * @param id the primary key of the java bean. * @return The entity with primary key id. */ @ApiMethod(name = "getNote") public Note getNote(@Nam
/**
* This method gets the entity having primary key id. It uses HTTP GET method.
*
* @param id the primary key of the java bean.
* @return The entity with primary key id.
*/
@ApiMethod(name = "getNote")
public Note getNote(@Named("id") String id) {
EntityManager mgr = getEntityManager();
Note note = null;
try {
note = mgr.find(Note.class, id);
} finally {
mgr.close();
}
return note;
}
但是我不知道如何使用这个方法。我已经阅读了很多关于这个的教程,但仍然感到困惑。
我试着这样使用它
Note newResult = endpoint.getNote(noteID).execute();
但是它返回
Null
如果给定ID的实体不存在,find()将返回Null。这就是为什么你会得到空值。但我是不是想用正确的方法得到实体?我试图理解getNote(@named(“id”)String id)
的含义,以及我是否正确地使用endpoint.getNote(noteID)
传递参数。NoteID包含实体的id。此处的注释与Hibernate/JPA无关。它们被其他框架使用。无论如何,注释不会改变调用方法的方式。它们只是一些框架可以使用的元数据。好吧,那为什么它会返回null呢?我似乎做得很好,它返回null,因为表中没有一行具有作为参数传递的ID。