Creating a MySQL table and Entity from Database.

Reply

Join Date: Aug 2007
Posts: 238
Reputation: ceyesuma is an unknown quantity at this point 
Solved Threads: 0
ceyesuma ceyesuma is offline Offline
Posting Whiz in Training

Creating a MySQL table and Entity from Database.

 
0
  #1
Mar 21st, 2009

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://<br /> http://www.netbeans.o....html#03<br />
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


  1. CREATE TABLE content(
  2. id INTEGER GENERATED ALWAYS AS IDENTITY,
  3.  
  4. publisher_code CHAR(2),
  5. book_isbn char(20),
  6. book_title char(100),
  7. artist char(150),
  8. song char(150),
  9. page_num INT(4)
  10. );
  11.  
  12.  
  13. ALTER TABLE content ADD CONSTRAINT idPK PRIMARY KEY(id);
  14.  
  15. 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


  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package mlpapp.app;
  7.  
  8. import java.io.Serializable;
  9. import javax.persistence.Basic;
  10. import javax.persistence.Column;
  11. import javax.persistence.Entity;
  12. import javax.persistence.GeneratedValue;
  13. import javax.persistence.GenerationType;
  14. import javax.persistence.Id;
  15. import javax.persistence.NamedQueries;
  16. import javax.persistence.NamedQuery;
  17. import javax.persistence.Table;
  18.  
  19. /**
  20.  *
  21.  * @author depot
  22.  */
  23. @Entity
  24. @Table(name = "content")
  25. @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")})
  26. public class Content implements Serializable {
  27. private static final long serialVersionUID = 1L;
  28. @Id
  29. @GeneratedValue(strategy = GenerationType.IDENTITY)
  30. @Basic(optional = false)
  31. @Column(name = "id")
  32. private Integer id;
  33. @Column(name = "publisher_code")
  34. private String publisherCode;
  35. @Column(name = "book_isbn")
  36. private String bookIsbn;
  37. @Column(name = "book_title")
  38. private String bookTitle;
  39. @Column(name = "artist")
  40. private String artist;
  41. @Column(name = "song")
  42. private String song;
  43. @Column(name = "page_num")
  44. private Integer pageNum;
  45.  
  46. public Content() {
  47. }
  48.  
  49. public Content(Integer id) {
  50. this.id = id;
  51. }
  52.  
  53. public Integer getId() {
  54. return id;
  55. }
  56.  
  57. public void setId(Integer id) {
  58. this.id = id;
  59. }
  60.  
  61. public String getPublisherCode() {
  62. return publisherCode;
  63. }
  64.  
  65. public void setPublisherCode(String publisherCode) {
  66. this.publisherCode = publisherCode;
  67. }
  68.  
  69. public String getBookIsbn() {
  70. return bookIsbn;
  71. }
  72.  
  73. public void setBookIsbn(String bookIsbn) {
  74. this.bookIsbn = bookIsbn;
  75. }
  76.  
  77. public String getBookTitle() {
  78. return bookTitle;
  79. }
  80.  
  81. public void setBookTitle(String bookTitle) {
  82. this.bookTitle = bookTitle;
  83. }
  84.  
  85. public String getArtist() {
  86. return artist;
  87. }
  88.  
  89. public void setArtist(String artist) {
  90. this.artist = artist;
  91. }
  92.  
  93. public String getSong() {
  94. return song;
  95. }
  96.  
  97. public void setSong(String song) {
  98. this.song = song;
  99. }
  100.  
  101. public Integer getPageNum() {
  102. return pageNum;
  103. }
  104.  
  105. public void setPageNum(Integer pageNum) {
  106. this.pageNum = pageNum;
  107. }
  108.  
  109. @Override
  110. public int hashCode() {
  111. int hash = 0;
  112. hash += (id != null ? id.hashCode() : 0);
  113. return hash;
  114. }
  115.  
  116. @Override
  117. public boolean equals(Object object) {
  118. // TODO: Warning - this method won't work in the case the id fields are not set
  119. if (!(object instanceof Content)) {
  120. return false;
  121. }
  122. Content other = (Content) object;
  123. if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
  124. return false;
  125. }
  126. return true;
  127. }
  128.  
  129. @Override
  130. public String toString() {
  131. return "mlpapp.app.Content[id=" + id + "]";
  132. }
  133.  
  134. }





Description



Code description:persistence.xml


  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <persistence-unit name="MLPContentPU" transaction-type="RESOURCE_LOCAL">
  4. <provider>oracle.toplink.essentials.PersistenceProvider</provider>
  5. <class>mlpapp.app.Content</class>
  6. <properties>
  7. <property name="toplink.jdbc.user" value="root"/>
  8. <property name="toplink.jdbc.password" value="ceyezuma"/>
  9. <property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/splashbookdb"/>
  10. <property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
  11. </properties>
  12. </persistence-unit>
  13. </persistence>



Description



Code description ContentController.java


  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package mlpapp.app;
  6.  
  7. import javax.persistence.EntityManager;
  8.  
  9.  
  10. import javax.persistence.Persistence;
  11.  
  12. import javax.persistence.EntityManagerFactory;
  13.  
  14. /**
  15.  *
  16.  * @author depot
  17.  */
  18. public class ContentController {
  19.  
  20. private EntityManagerFactory emf;
  21.  
  22. private EntityManager getEntityManager() {
  23. if (emf == null) {
  24. emf = Persistence.createEntityManagerFactory("MLPContentPU");
  25. }
  26. return emf.createEntityManager();
  27. }
  28.  
  29. public Content[] getContent() {
  30. EntityManager em = getEntityManager();
  31. try {
  32. javax.persistence.Query q = em.createQuery("select c from content as c");
  33. return (Content[]) q.getResultList().toArray(new Content[0]);
  34. } finally {
  35. em.close();
  36. }
  37.  
  38. }
  39. }





Thanks again.
-ceyesuma





Solution:



The solutions related to this code are





Note:



Note:
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 238
Reputation: ceyesuma is an unknown quantity at this point 
Solved Threads: 0
ceyesuma ceyesuma is offline Offline
Posting Whiz in Training

Re: Creating a MySQL table and Entity from Database.

 
0
  #2
Mar 23rd, 2009
Rumor is that this tutorial can not be done with Netbeans6.5
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JSP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC