1 Reply Latest reply: Dec 19, 2010 3:15 AM by 800439 RSS

    toplink.ddl-generation doesn't seem to be working

    800439
      I can't get my project to persist. Why is this?
      I have a skeleton project using JSF2, EJB3, Glassfish3, JPA/toplink, Helios. When I run the deployed project, I get this error:
      +Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.DatabaseException+
      Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'test.bar' doesn't exist
      Error Code: 1146

      A blank DB called test exists on mySQL
      I have a connection pool in glassfish with a URL=jdbc:mysql://localhost:3306/test
      user=root, pass=root
      A mySQL connector jar exists in $glassfishHome/glassfish/lib

      My code is as follows:
      newBook.xhtml
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml"
           xmlns:h="http://java.sun.com/jsf/html">
      <h:head>
           <title>Creates a new book</title>
      </h:head>
      <h:body>
           <h1>Create a new book</h1>
           <hr />
           <h:form>
                <h:panelGrid columns="2">
                     <h:outputLabel value="ISBN : " />
                     <h:inputText value="#{bookController.book.isbn}" />
                     <h:outputLabel value="Title :" />
                     <h:inputText value="#{bookController.book.title}" />
                     <h:outputLabel value="Price : " />
                     <h:inputText value="#{bookController.book.price}" />
                     <h:outputLabel value="Description : " />
                     <h:inputTextarea value="#{bookController.book.description}" cols="20"
                          rows="5" />
                     <h:outputLabel value="Number of pages : " />
                     <h:inputText value="#{bookController.book.nbOfPage}" />
                     <h:outputLabel value="Illustrations : " />
                     <h:selectBooleanCheckbox value="#{bookController.book.illustrations}" />
                </h:panelGrid>
                <h:commandButton value="Create a book"
                     action="#{bookController.doCreateBook}" />
           </h:form>
           <hr />
           <i>APress - Beginning Java EE 6</i>
      </h:body>
      </html> 
      BookController.java
       package controllers;
      
      import java.util.ArrayList;
      import java.util.List;
      
      import javax.ejb.EJB;
      import javax.faces.bean.ManagedBean;
      import javax.faces.bean.RequestScoped;
      
      
      import ejb.BookEJB;
      import entities.Book;
      
      @ManagedBean
      @RequestScoped
      public class BookController {
           @EJB
           private BookEJB bookEJB;
           private Book book = new Book();
           private List<Book> bookList = new ArrayList<Book>();
      
           public String doCreateBook() {
                book = bookEJB.createBook(book);
                bookList = bookEJB.findBooks();
                return "listBooks.xhtml";
           }
      
           public BookController() {
           }
      
           public BookEJB getBookEJB() {
                return bookEJB;
           }
      
           public void setBookEJB(BookEJB bookEJB) {
                this.bookEJB = bookEJB;
           }
      
           public Book getBook() {
                return book;
           }
      
           public void setBook(Book book) {
                this.book = book;
           }
      
           public List<Book> getBookList() {
                return bookList;
           }
      
           public void setBookList(List<Book> bookList) {
                this.bookList = bookList;
           }
      
      }
       
      BookEJB.java
      package ejb;
      
      import java.util.List;
      
      import javax.ejb.Stateless;
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;
      import javax.persistence.TypedQuery;
      
      import entities.Book;
      
      
      @Stateless
      public class BookEJB {
           @PersistenceContext(unitName = "jdbc/test") //java:comp/env/jdbc/
           private EntityManager em;
      
           public List<Book> findBooks() {
                TypedQuery<Book> query = em
                          .createNamedQuery("findAllBooks", Book.class);
                return query.getResultList();
           }
      
           public Book createBook(Book book) {
                em.persist(book);
                return book;
           }
      }
      Book.java
      package entities;
      
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.NamedQuery;
      import javax.persistence.Table;
      
      @Table(name="bar")
      @Entity
      @NamedQuery(name = "findAllBooks", query = "SELECT b FROM Book b")
      public class Book {
           @Id
           @GeneratedValue(strategy=GenerationType.IDENTITY)
           private Long id;
           @Column(nullable = false)
           private String title;
           private Float price;
           @Column(length = 2000)
           private String description;
           private String isbn;
           private Integer nbOfPage;
           private Boolean illustrations;
      
           public Book() {
           }
      
           public Long getId() {
                return id;
           }
      
           public void setId(Long id) {
                this.id = id;
           }
      
           public String getTitle() {
                return title;
           }
      
           public void setTitle(String title) {
                this.title = title;
           }
      
           public Float getPrice() {
                return price;
           }
      
           public void setPrice(Float price) {
                this.price = price;
           }
      
           public String getDescription() {
                return description;
           }
      
           public void setDescription(String description) {
                this.description = description;
           }
      
           public String getIsbn() {
                return isbn;
           }
      
           public void setIsbn(String isbn) {
                this.isbn = isbn;
           }
      
           public Integer getNbOfPage() {
                return nbOfPage;
           }
      
           public void setNbOfPage(Integer nbOfPage) {
                this.nbOfPage = nbOfPage;
           }
      
           public Boolean getIllustrations() {
                return illustrations;
           }
      
           public void setIllustrations(Boolean illustrations) {
                this.illustrations = illustrations;
           }
      
      }
      persistence.xml (located under src/META-INF)
      <?xml version="1.0" encoding="UTF-8"?>
      <persistence version="2.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_2_0.xsd">
           <persistence-unit name="jdbc/test">
                <jta-data-source>jdbc/test</jta-data-source>
                <class>entities.Book</class>
                <properties>
                     <property name="toplink.ddl-generation" value="drop-and-create-tables"></property>
                     <property name="toplink.ddl-generation.output-mode" value="database"></property>
                     <property name="toplink.logging.level" value="finest"/>
                     <property name="toplink.logging.logger" value="DefaultLogger"/>
                     <property name="toplink.jdbc.user" value="root"/>
                     <property name="toplink.jdbc.password" value="root"/>
                </properties>
           </persistence-unit>
      </persistence>