显示JPA实体的Java

时间:2017-08-01 12:00:40

标签: java hibernate jpa

我在使用Java显示一些JPA实体时遇到问题。我对这门语言并不熟悉,所以不确定我是否可以将我现在所做的事情结合起来。我有这个Student课,这显然代表了一个学生:

@Entity
@Table
public class Student implements Serializable {

    @Id
    @GeneratedValue
    private Integer id;

    @Email
    private String email;

    @Column(unique=true)
    private String studentNumber;

    private String firstName;

    private String lastName;

    public Student() {}

    public Student(String studentNumber, String firstName, String lastName, String email) {
        this.studentNumber = studentNumber;
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
    }

现在的想法是拥有一个RESTFul服务,并能够通过查找他们的学号来显示所有学生。因此,我在持久性课程中有这个:

@Override
public Student findByStudentNumber(String studentNumber) {
    Object entity = em.createQuery("SELECT * FROM students st where st.studentNumber = :val")
            .setParameter("val", studentNumber).getSingleResult();
    return (Student) entity;
}

@Override
public List<Student> getAll() {
    List<Student> students = em.createQuery(criteriaQuery).getResultList();
    return students;
}

我的控制器:

@Path("/students")
public class StudentController {
    @Inject
    private StudentPersistence studentPersistence;

@GET
@Path("/get")
public List<Student> getAll() {
    List<Student> all = studentPersistence.getAll();
    return all;
}

@GET
@Path("{studentNr}")
public Student getStudent(@PathParam("studentNr") String id) {
    return studentPersistence.findByStudentNumber(id);
}

但是,我似乎无法让他们进入控制器,因为它不断抛出500内部服务器错误。我的问题:这是结合这个的方法,还是有更好的方式来显示这些实体?

编辑:我的500错误的堆栈跟踪:

01-Aug-2017 14:04:24.769 SEVERE [http-nio-8081-exec-1] com.sun.jersey.spi.container.ContainerResponse.logException Mapped exception to response: 500 (Internal Server Error)
 javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class models.Student, and Java type class models.Student, and MIME media type application/xml was not found
    at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:285)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1479)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1391)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1381)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at filters.CorsFilter.doFilter(CorsFilter.java:33)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1434)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: com.sun.jersey.api.MessageException: A message body writer for Java class models.Student, and Java type class models.Student, and MIME media type application/xml was not found
    ... 33 more

3 个答案:

答案 0 :(得分:0)

500表示内部服务器错误,似乎抛出了意外异常,我可以看到原因,您需要为生成的值指定策略,我相信您的目标是自动增量,以便您可以使用AUTO

Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip
{
    param([string]$zipfile, [string]$outpath)

    [System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}

Unzip "C:\a.zip" "C:\a"

检查此link以获取有关可以使用的策略的更多信息

答案 1 :(得分:0)

到目前为止,您已经完成了可以显示用户数据的NOTHING ..您创建了一个模型类(Student),您实现了CRUD方法(它们应该使用DAO模式定义),您实现了控制器,但是在哪里应该显示数据的html / jsp? 我建议你使用spring来定义Web服务,它更容易和更好;您还应该考虑调用DAO层的服务层,然后控制器调用该服务。这样你可以直接从控制器调用CRUD方法,这样你就可以将web层连接到集成(db)层:这是错误的。

实现一个jsp或html文件,你可以显示一些东西。使用jsp标签或更新的东西(spring)来显示你想要的数据,然后看一下Service层,DAO模式(但我认为你已经知道了)和spring(你需要的模块是Ioc,依赖项)和mvc)。

更新: 我看到你的堆栈跟踪..我不知道是不是同样的问题,看看这个: A message body writer for Java class not found

答案 2 :(得分:0)

必须添加一些修改:

1 - 您必须指定表名@Table(name = "table_name")

2 - 您必须指定列名称,例如:

   @Column(name="firstName")
   private String firstName;

3 - 使用HQL时,必须指定实体名称而不是表名称。在您的情况下,"SELECT * FROM students....将更改"SELECT * FROM Student