Java 使用JPA和hibernate在数据库中插入数据。。但在插入数据时存在问题
我正在使用带Hibernate的JPA,无法插入数据。。 serviceImpl类:Java 使用JPA和hibernate在数据库中插入数据。。但在插入数据时存在问题,java,spring,hibernate,spring-mvc,jpa,Java,Spring,Hibernate,Spring Mvc,Jpa,我正在使用带Hibernate的JPA,无法插入数据。。 serviceImpl类: package home; import java.sql.Timestamp; import java.text.SimpleDateFormat; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransactio
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;
}
}
但在运行时,我发现了这个错误。直到persist()方法,它正在执行,但没有执行commit()方法
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>
org.hibernate.ejb.HibernatePersistence
com.order.OrderBook
dispatcher-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>
共享您的Spring上下文。具体与JPA相关。@Rohit我已上载了必要的文件。您部署到哪个应用程序服务器?这是否有帮助:?您是否使用兼容JAR?你们有验证器罐吗?下载并重试一次-
<?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>