Topic
The topic of this question is: Creating a MySQL table and Entity from Database.
Hello and Thank you in advance for any assistance.

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

http://www.netbeans.org/kb/60/web/web-jpa.html#03

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

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

/*
 * 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

<?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

/*
 * 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:

Rumor is that this tutorial can not be done with Netbeans6.5

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.