2012-01-02 161 views
0

我试图让我的模型使用我的MySQL数据库。目前他们不是。它们是使用允许我从数据库表创建模型的选项在Netbeans中创建的。我不知道如何让它在运行时使用我的MySQL表。有什么建议么?mysql中的Java持久性单元

我的模型

@Entity 
@Table(name = "content") 
@NamedQueries({ 
    @NamedQuery(name = "Content.findAll", query = "SELECT c FROM Content c"), 
    @NamedQuery(name = "Content.findById", query = "SELECT c FROM Content c WHERE c.id = :id"), 
    @NamedQuery(name = "Content.findByUserId", query = "SELECT c FROM Content c WHERE c.userId = :userId")}) 
public class Content implements java.io.Serializable { 
    private static final long serialVersionUID = 1L; 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Basic(optional = false) 
    @NotNull 
    @Column(name = "id") 
    private Integer id; 
    @Basic(optional = false) 
    @NotNull 
    @Column(name = "user_id") 
    private int userId; 
    @Basic(optional = false) 
    @NotNull 
    @Lob 
    @Size(min = 1, max = 65535) 
    @Column(name = "body") 
    private String body; 

    public Content() { 
    } 

    public Content(Integer id) { 
     this.id = id; 
    } 

    public Content(Integer id, int userId, String body) { 
     this.id = id; 
     this.userId = userId; 
     this.body = body; 
    } 

    public Integer getId() { 
     return id; 
    } 

    public void setId(Integer id) { 
     this.id = id; 
    } 

    public int getUserId() { 
     return userId; 
    } 

    public void setUserId(int userId) { 
     this.userId = userId; 
    } 

    public String getBody() { 
     return body; 
    } 

    public void setBody(String body) { 
     this.body = body; 
    } 

    @Override 
    public int hashCode() { 
     int hash = 0; 
     hash += (id != null ? id.hashCode() : 0); 
     return hash; 
    } 

    @Override 
    public boolean equals(Object object) { 
     // TODO: Warning - this method won't work in the case the id fields are not set 
     if (!(object instanceof Content)) { 
      return false; 
     } 
     Content other = (Content) object; 
     if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public String toString() { 
     return "model.Content[ id=" + id + " ]"; 
    } 

} 

我希望这个代码列出所有在我的数据库中的内容,但它列出什么。我的假设是,它的使用一些其他的数据库,但我“不能完全肯定如何对它进行测试。

<ol> <% 
    List<Content> contentList = (List<Content>)request.getAttribute("content"); 
    if (contentList != null) { 
     for (Content content : contentList) { %> 
      <li> <%= content %> </li> <% 
     } 
    } %> 
</ol> 
+2

你想做什么?你面临什么问题?你的查询到底是什么?请具体提一下。 – Lion 2012-01-02 21:25:04

+0

@Lion - 这是更好吗? – Webnet 2012-01-02 21:30:02

+0

那么'request.getAttribute(“content”);'为了什么?什么是检索? – Lion 2012-01-02 21:34:58

回答

0

没有的NetBeans创建一个持久性单元为你当你这样做?

如果NetBeans的没有这样做,你必须自己创建它,创建一个persistence.xml并把它放在META-INF文件夹中。在persistence.xml里你定义了一个持久化单元,它本质上是一个命名的数据库连接或连接池。在你的javacode中,你使用EntityManagerFactory来为命名的持久化单元创建一个EntityManager。

你也必须指定在persistence.xml中的JPA提供者,我推荐EclipseLink或Hibernate。

最后,您必须在您的项目中为您选择的JPA提供程序设置依赖关系。

+0

我记得现在我已经迁移了模型,但忘记了persistance.xml,这是它不工作的原因。谢谢! – Webnet 2012-01-03 00:02:55