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

+0

@Lion - これは良いですか? – Webnet

+0

さて、 'request.getAttribute(" content ");'何のため?それは何を取得していますか? – Lion

答えて

0

あなたがそれを行うとき、NetBeansはあなたのための持続性ユニットを作成していないのですか?

NetBeansの場合あなた自身でそれを作成し、永続性ファイルを作成してMETA-INFフォルダに置かなければなりません。persistence.xmlの中に永続性ユニットを定義します。これは、本質的に名前付きのデータベース接続または接続のプールです。あなたのjavacodeで、EntityManagerFactoryを使用して名前付き永続ユニットのEntityManagerを作成します。

また、 persistence.xmlのJPA-providerは、EclipseLinkまたはHibernateをお勧めします。

最後に、選択したJPAプロバイダのプロジェクトに依存関係を設定する必要があります。

+0

私はモデルを移行しましたが、persistance.xmlを忘れてしまったことを覚えています。なぜそれが機能していなかったのかです。ありがとう! – Webnet

関連する問題