表不是使用JPA 2.0创建的

时间:2012-09-12 10:12:10

标签: jpa-2.0 ejb-3.0

我在Netbeans中使用JPA 2.0。我正在使用实体。如果我的数据库没有表,那么它应该从实体创建表。这是我的代码

public class BankServlet extends HttpServlet {

     @EJB
     private BankServiceBeanRemote bankServiceBean;

     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

        int custId = Integer.parseInt(request.getParameter("id"));

        bankServiceBean.createCustomers();
        Customer cust = bankServiceBean.findCustomer(custId);

        response.setContentType("text/html;charset=UTF-8");
        ....

    } //end of processRequest

} //end of class BankServlet

这是我的豆子

@Stateless
public class BankServiceBean implements BankServiceBeanRemote {

    @PersistenceContext(unitName = "Bank_JPA-ejbPU")
    private EntityManager em;

    @Override
    public void createCustomers() {

        Referee r1 = new Referee();
        r1.setId(1);
        r1.setName("SIR JOHN DEED");
        r1.setComments("JUDGE");     
        em.persist(r1);

        Customer c1 = new Customer();
        c1.setId(1);
        c1.setFirstName("SIMON");
        c1.setLastName("KING");
        c1.setReferee(r1);

        ......
    }

} //end of class BankServiceBean

这是我的裁判实体

@Entity
public class Referee implements Serializable {

    private static final long serialVersionUID = 1L;

    private int id;
    private String name;
    private String comments;

    public Referee() {

    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public int getId() {
        return id;
    }

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

    // other getter setters

} //end of class Referee

当我运行代码时,我得到以下异常

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     'dbbank.referee' doesn't exist
Error Code: 1146
Call: INSERT INTO REFEREE (ID, NAME, COMMENTS) VALUES (?, ?, ?)
    bind => [3, MICHAEL ELLIS, MAJOR SHAREHOLDER OF THIS BANK]
Query: InsertObjectQuery(pk.mazars.basitMahmood.entity.Referee[id=3])
    at     org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:324)

如果表不存在则应自动创建表。我做错了吗?

谢谢

3 个答案:

答案 0 :(得分:1)

你的假设是错误的。如果表不存在,则不会神奇地创建表。大多数应用程序使用现有数据库。

EclipseLink 可以为您创建架构,但它不是默认的。阅读this page以了解如何启用此功能:

  

EclipseLink可用于自动生成表和   持久性单元的数据库模式。这是通过   “eclipselink.ddl-generation”持久性单元属性,设置为   “create-tables”或“drop-and-create-tables”。表和   将为其中定义的所有类生成约束   持久性单位。

答案 1 :(得分:1)

您应该在persistence.xml中更改表生成策略。

只需将这些行添加到persistence.xml文件中:

<properties>
  <property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>

答案 2 :(得分:1)

以下是要添加到persistence.xml以使 openjpa 为您创建表格的代码段。当使用带有内存数据库(如HyperSQL)的openejb进行容器单元测试时,这很方便。

    <properties>
         <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
    </properties>