【问题标题】:Table is not creating using JPA 2.0表未使用 JPA 2.0 创建
【发布时间】:2012-09-12 10:12:09
【问题描述】:

我在 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)

如果表不存在,那么它应该自动创建表。我是不是做错了什么?

谢谢

【问题讨论】:

    标签: jpa-2.0 ejb-3.0


    【解决方案1】:

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

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

    EclipseLink 可用于自动生成表格和 持久性单元的数据库模式。这是通过 “eclipselink.ddl-generation”持久性单元属性,设置为 “创建表”或“删除并创建表”。桌子和 将为其中定义的所有类生成约束 持久性单元。

    【讨论】:

    • hhmm 所以如果我有一个数据库,就像我使用 netbeans 在 mysql 中创建了一个名为 dbbank 的数据库。然后我创建了一个实体并将其与 dbbank 数据库链接。现在我有数据库并且实体与之链接。现在持久化 API 会自动为我创建表?
    • 仅当您告诉 EclipseLink 为您创建数据库表时。阅读我给你的链接。那里都有解释。
    【解决方案2】:

    你应该在persistence.xml中改变你的表生成策略。

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

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

    【讨论】:

      【解决方案3】:

      这是一个 sn-p 添加到您的 persistence.xml 中,以使 openjpa 为您创建表。这在使用 openejb 和 HyperSQL 等内存数据库进行容器单元测试时会派上用场。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-09
        • 2018-04-13
        • 2017-05-29
        • 2017-08-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多