使用JPA和hibernate在数据库中插入数据..但插入数据时出现问题

时间:2015-01-20 09:09:25

标签: java spring hibernate spring-mvc jpa

我正在使用JPA和Hibernate,无法插入数据.. serviceImpl类:

package home;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;


import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import com.order.OrderBook;

public class OrderServiceImpl implements OrderService {
@Override
public void add(OrderBook orderbook) {


    EntityManagerFactory emf=Persistence.createEntityManagerFactory("PU");
    System.out.println("in Get Users");
    EntityManager em=emf.createEntityManager();
    EntityTransaction et=em.getTransaction();
    et.begin();
    String timeStamp = 
            new SimpleDateFormat("dd-MM-yyyy:HH.mm.ss").
            format(new Timestamp(System.currentTimeMillis()));
    orderbook.setOrder_date(timeStamp);

    em.persist(orderbook);
    System.out.println("after persist");
        et.commit();
        System.out.println("Inserted");
    }
}

OrderBook.java

package com.order;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import javax.persistence.Table;

@Entity
@Table(name = "order_book")

public class OrderBook {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int order_id;
    private int client_id;
    private int broker_id;
    private String order_date;
    private int stock_id;
    private int price_per_share;
    private int no_of_share;
    private int total_amount;
    private String trade_type;

    public int getOrder_id() {
        return order_id;
    }
    public void setOrder_id(int order_id) {
        this.order_id = order_id;
    }


    public int getClient_id() {
        return client_id;
    }
    public void setClient_id(int client_id) {
        this.client_id = client_id;
    }
    public int getBroker_id() {
        return broker_id;
    }
    public void setBroker_id(int broker_id) {
        this.broker_id = broker_id;
    }

    public String getOrder_date() {
        return order_date;
    }
    public void setOrder_date(String order_date) {
        this.order_date =order_date;
    }

    public int getStock_id() {
        return stock_id;
    }
    public void setStock_id(int stock_id) {
        this.stock_id = stock_id;
    }
    public int getPrice_per_share() {
        return price_per_share;
    }
    public void setPrice_per_share(int price_per_share) {
        this.price_per_share = price_per_share;
    }
    public int getNo_of_share() {
        return no_of_share;
    }
    public void setNo_of_share(int no_of_share) {
        this.no_of_share = no_of_share;
    }
    public int getTotal_amount() {
        return total_amount;
    }
    public void setTotal_amount(int total_amount) {
        this.total_amount = total_amount;
    }
    public String getTrade_type() {
        return trade_type;
    }
    public void setTrade_type(String trade_type) {
        this.trade_type = trade_type;
    }
}

但在运行时我收到此错误。 upto persist()方法正在执行但它没有执行commit()方法。

exception 

org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoSuchMethodError: org.hibernate.event.PreInsertEvent.getSource()Lorg/hibernate/engine/SessionImplementor;
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:812)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:709)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:613)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:536)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)


root cause 

java.lang.NoSuchMethodError: org.hibernate.event.PreInsertEvent.getSource()Lorg/hibernate/engine/SessionImplementor;
    org.hibernate.validator.event.ValidateEventListener.onPreInsert(ValidateEventListener.java:156)
    org.hibernate.action.EntityInsertAction.preInsert(EntityInsertAction.java:178)
    org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:72)
    org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
    org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
    org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
    org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
    org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
    org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
    org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
    org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:53)
    home.OrderServiceImpl.add(OrderServiceImpl.java:32)
    com.order.web.OrderController.onSubmit(OrderController.java:31)
    org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:415)
    org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:387)
    org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:272)
    org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:268)
    org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
    org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:763)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:709)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:613)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:536)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)


note The full stack trace of the root cause is available in the Apache Tomcat/7.0.0 logs.

的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="PU" transaction-type="RESOURCE_LOCAL">
   <provider>org.hibernate.ejb.HibernatePersistence</provider>
     <class>com.order.OrderBook</class>
     <!-- <exclude-unlisted-classes /> -->
     <properties>
       <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
       <property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:XE"/>
       <property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver"/>
       <property name="hibernate.connection.username" value="system"/>
       <property name="hibernate.connection.password" value="newuser123#"/>
        <property name="hibernate.show_sql" value="true"/>
       <property name="hibernate.format_sql" value="true"/>
    </properties>
  </persistence-unit>
</persistence>

调度-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"    
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:context="http://www.springframework.org/schema/context"    
xmlns:security="http://www.springframework.org/schema/security"    
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:p="http://www.springframework.org/schema/p"   
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd                
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd                
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd ">      

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />

   <bean id="orderService" class="home.OrderServiceImpl" />

    <bean name="/placeOrder.htm" class="com.order.web.OrderController" p:orderService-ref="orderService"
     p:formView="userForm" p:successView="userSuccess" />


</beans>

0 个答案:

没有答案