EntityManager(@PersistenceContext)始终在'find'上返回null

时间:2016-10-22 17:13:54

标签: java hibernate rest jpa wildfly

我开始使用JEE。我抓住了一个新的学生IntelliJ IDEA许可证,并以Wildfly 10.0作为AS创建了一个新的JEE项目。

在我宁静的api课程的某个地方:

import com.zarkopafilis.beans.Post;
import javax.ejb.Stateless;
import javax.persistence.*;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import javax.ws.rs.core.MediaType;


@Path("/posts")
@Stateless
public class GetPost {

@PersistenceContext
private EntityManager em;

@GET
@Path("/{id}")
@Produces(MediaType.TEXT_PLAIN)
public String getPost(@PathParam("id") Integer id){

    Post post = em.find(Post.class ,  id , LockModeType.READ);

    if(post == null){
        return "No post found with id " + id;
    }

    return "Title: " + post.getTitle() + '\n' + "Content: " + post.getContent();
}

@GET
@Produces(MediaType.TEXT_PLAIN)
public String getRoot(){
    return "root";
}
}

这个'Post'类如下所示:(它是一个带有hibernate的orm-mapped bean)

@Entity
@Table(name = "posts")
public class Post implements Serializable{

@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name="id")
private int id;

@Column(name="uni_id")
private long uniId;

@NotNull
@NotBlank
@NotEmpty
@Column(name="title")
private String title;

@NotNull
@NotBlank
@NotEmpty
@Column(name = "content")
private String content;

@NotNull
@NotBlank
@NotEmpty
@Column(name = "uni_name")
private String universityName;

@NotNull
@NotBlank
@NotEmpty
@Column(name = "department_name")
private String departmentName;

@NotNull
@Min(0)
@Column(name = "upvotes")
private int upvotes;

.... getters and setters ....

我手动创建了表和一些用于测试的条目。我还在intellij内部以及wildfly的standalone.xml中正确配置了数据源。我总是得到em.find返回的空帖。

0 个答案:

没有答案