2012-01-02 154 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