NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

Creating a MySQL table and Entity from Database.

 
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users
View previous topic :: View next topic  
Author Message
ceyezumma



Joined: 02 Feb 2009
Posts: 122

PostPosted: Sat Mar 21, 2009 4:57 am    Post subject: Creating a MySQL table and Entity from Database. Reply with quote


Topic
The topic of this question is: Creating a MySQL table and Entity from Database.

[icode]
Hello and Thank you in advance for any assistance.
[/icode]



Purpose:



This is another attempt to render data in a visual web javaserver faces table.
The purpose of this code is to create a MySQL table that will use an auto incremented id and then create an Entity from the database that is able to use this id.




Question:



My question concerning this code is the table deploys and the headers are correct based on the content table but why isn’t the data generating.




Functionality:



The actual functionality of this code is based on the tutorial at
[url]
http://www.netbeans.org/kb/60/web/web-jpa.html#03
[/url]
and it seems to be constructed the same.






errors:



The errors related to this code is that I am assuming that there is a problem with the generated id in the class. Is this possible?







Description



Code description: content table MySQL


Code:

CREATE TABLE content(
id INTEGER GENERATED ALWAYS AS IDENTITY,

publisher_code          CHAR(2),
book_isbn      char(20),
book_title              char(100),
artist                  char(150),
song                    char(150),
page_num                INT(4)
);


ALTER TABLE content ADD CONSTRAINT idPK PRIMARY KEY(id);

INSERT INTO content (publisher_code,book_isbn,book_title,artist,song,page_num) VALUES ('HL','0-634-05315-9','Guitar Tab White Pages Vol 2','Bryan adams','Its Only Love',371);




Description



Code description :Content.java @Entity class generated from database


Code:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mlpapp.app;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
 *
 * @author depot
 */
@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.findByPublisherCode", query = "SELECT c FROM Content c WHERE c.publisherCode = :publisherCode"), @NamedQuery(name = "Content.findByBookIsbn", query = "SELECT c FROM Content c WHERE c.bookIsbn = :bookIsbn"), @NamedQuery(name = "Content.findByBookTitle", query = "SELECT c FROM Content c WHERE c.bookTitle = :bookTitle"), @NamedQuery(name = "Content.findByArtist", query = "SELECT c FROM Content c WHERE c.artist = :artist"), @NamedQuery(name = "Content.findBySong", query = "SELECT c FROM Content c WHERE c.song = :song"), @NamedQuery(name = "Content.findByPageNum", query = "SELECT c FROM Content c WHERE c.pageNum = :pageNum")})
public class Content implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Column(name = "publisher_code")
    private String publisherCode;
    @Column(name = "book_isbn")
    private String bookIsbn;
    @Column(name = "book_title")
    private String bookTitle;
    @Column(name = "artist")
    private String artist;
    @Column(name = "song")
    private String song;
    @Column(name = "page_num")
    private Integer pageNum;

    public Content() {
    }

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

    public Integer getId() {
        return id;
    }

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

    public String getPublisherCode() {
        return publisherCode;
    }

    public void setPublisherCode(String publisherCode) {
        this.publisherCode = publisherCode;
    }

    public String getBookIsbn() {
        return bookIsbn;
    }

    public void setBookIsbn(String bookIsbn) {
        this.bookIsbn = bookIsbn;
    }

    public String getBookTitle() {
        return bookTitle;
    }

    public void setBookTitle(String bookTitle) {
        this.bookTitle = bookTitle;
    }

    public String getArtist() {
        return artist;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }

    public String getSong() {
        return song;
    }

    public void setSong(String song) {
        this.song = song;
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    @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 "mlpapp.app.Content[id=" + id + "]";
    }

}






Description



Code description:persistence.xml


Code:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <persistence-unit name="MLPContentPU" transaction-type="RESOURCE_LOCAL">
    <provider>oracle.toplink.essentials.PersistenceProvider</provider>
    <class>mlpapp.app.Content</class>
    <properties>
      <property name="toplink.jdbc.user" value="root"/>
      <property name="toplink.jdbc.password" value="ceyezuma"/>
      <property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/splashbookdb"/>
      <property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
    </properties>
  </persistence-unit>
</persistence>
 




Description



Code description ContentController.java


Code:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mlpapp.app;

import javax.persistence.EntityManager;


import javax.persistence.Persistence;

import javax.persistence.EntityManagerFactory;

/**
 *
 * @author depot
 */
public class ContentController {

    private EntityManagerFactory emf;

    private EntityManager getEntityManager() {
        if (emf == null) {
            emf = Persistence.createEntityManagerFactory("MLPContentPU");
        }
        return emf.createEntityManager();
    }

    public Content[] getContent() {
        EntityManager em = getEntityManager();
        try {
            javax.persistence.Query q = em.createQuery("select c from content as c");
            return (Content[]) q.getResultList().toArray(new Content[0]);
        } finally {
            em.close();
        }

    }
}







Thanks again.
-ceyesuma





Solution:



The solutions related to this code are





Note:



Note:
Back to top
ceyezumma



Joined: 02 Feb 2009
Posts: 122

PostPosted: Mon Mar 23, 2009 11:48 pm    Post subject: Netbeans6.0 tutorial Reply with quote

Rumor is that this tutorial can not be done with Netbeans6.5
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo