我试图将我的客户的姓名和地址发送到我的数据库,但它似乎不想工作。该程序有效,但它不会在我的数据库中输入任何内容。
我的数据库如下所示:
CREATE TABLE PIZZA.CUSTOMER(
ID INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
NAME VARCHAR(255) NOT NULL,
ADRESS VARCHAR(255) NOT NULL);
在这里,我将我的姓名和地址发送到客户对象
private void sendOrder(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher rd = request.getRequestDispatcher("thanku.jsp");
rd.forward(request, response);
String name = request.getParameter("name");
String adress = request.getParameter("adress");
DBHandler dbHandler = new DBHandler();
Customer customer = new Customer(adress,name);
dbHandler.storeInTable(customer);
}
在这里我尝试将其发送到数据库,但它不起作用
public class DBHandler {
private final String PERSISTENCE_UNIT_NAME = "Customer";
public boolean storeInTable(Customer customer) {
boolean result = false;
EntityManagerFactory factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
try {
em.getTransaction().begin();
em.persist(customer);
em.getTransaction().commit();
result = true;
} finally {
if (em.getTransaction().isActive())
em.getTransaction().rollback();
em.close();
}
return result;
}
这是jpa实体类Customer
@Entity
@Table(schema = "PIZZA", name="CUSTOMER")
@NamedQuery(name="Customer.findAll", query="SELECT c FROM Customer c")
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String adress;
private String name;
public Customer() {
}
public Customer(String adress,String name) {
this.adress = adress;
this.name = name;
}
这是JPA提供程序日志
[EL Config]: metadata: 2015-12-09 17:30:28.993--ServerSession(24639535)-- **Thread(Thread[http-bio-8080-exec-10,5,main])--The access type for the persistent class [class model.Customer] is set to [FIELD].
[EL Config]: metadata: 2015-12-09 17:30:29.023--ServerSession(24639535)--Thread(Thread[http-bio-8080-exec-10,5,main])--The alias name for the entity class [class model.Customer] is being defaulted to: Customer.
[EL Config]: metadata: 2015-12-09 17:30:29.047--ServerSession(24639535)--Thread(Thread[http-bio-8080-exec-10,5,main])--The column name for element [name] is being defaulted to: NAME.
[EL Config]: metadata: 2015-12-09 17:30:29.048--ServerSession(24639535)--Thread(Thread[http-bio-8080-exec-10,5,main])--The column name for element [adress] is being defaulted to: ADRESS.
[EL Config]: metadata: 2015-12-09 17:30:29.048--ServerSession(24639535)--Thread(Thread[http-bio-8080-exec-10,5,main])--The column name for element [id] is being defaulted to: ID.
[EL Info]: 2015-12-09 17:30:29.688--ServerSession(24639535)--Thread(Thread[http-bio-8080-exec-10,5,main])--EclipseLink, version: Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd
[EL Fine]: connection: 2015-12-09 17:30:30.13--Thread(Thread[http-bio-8080-exec-10,5,main])--Detected database platform: org.eclipse.persistence.platform.database.JavaDBPlatform
[EL Config]: connection: 2015-12-09 17:30:30.145--ServerSession(24639535)--Connection(4727047)--Thread(Thread[http-bio-8080-exec-10,5,main])--connecting(DatabaseLogin(
platform=>JavaDBPlatform
user name=> "PIZZA"
datasource URL=> "jdbc:derby:/var/lib/tomcat7/webapps/db/MyDBgrp2;create=true"
))
[EL Config]: connection: 2015-12-09 17:30:30.147--ServerSession(24639535)-- Connection(180962)--Thread(Thread[http-bio-8080-exec-10,5,main])--Connected: jdbc:derby:/var/lib/tomcat7/webapps/db/MyDBgrp2
User: PIZZA
Database: Apache Derby Version: 10.11.1.1 - (1616546)
Driver: Apache Derby Embedded JDBC Driver Version: 10.11.1.1 - (1616546)
[EL Info]: connection: 2015-12-09 17:30:30.267--ServerSession(24639535)--Thread(Thread[http-bio-8080-exec-10,5,main])--file:/C:/Users/isac/workspace mars/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/OnlinePizza/WEB-INF/classes/_Customer login successful
[EL Fine]: sql: 2015-12-09 17:30:30.577--ServerSession(24639535)--Connection(180962)--Thread(Thread[http-bio-8080-exec-10,5,main])--SELECT * FROM SEQUENCE WHERE SEQ_NAME = SEQ_GEN
[EL Fine]: sql: 2015-12-09 17:30:30.636--ClientSession(28727725)--Connection(180962)--Thread(Thread[http-bio-8080-exec-10,5,main])--UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ?
bind => [50, SEQ_GEN]
[EL Fine]: sql: 2015-12-09 17:30:30.674--ClientSession(28727725)--Connection(180962)--Thread(Thread[http-bio-8080-exec-10,5,main])--SELECT SEQ_COUNT FROM SEQUENCE WHERE SEQ_NAME = ?
bind => [SEQ_GEN]
[EL Fine]: sql: 2015-12-09 17:30:30.687--ClientSession(28727725)--Connection(180962)--Thread(Thread[http-bio-8080-exec-10,5,main])--INSERT INTO PIZZA.CUSTOMER (ID, ADRESS, NAME) VALUES (?, ?, ?)
bind => [601, asd, sdf]**
答案 0 :(得分:0)
对我而言,似乎总是将您刚写入的数据回滚到finally块
中的数据库中finally {
if (em.getTransaction().isActive()) //is always active
em.getTransaction().rollback(); //so it always rolls back
em.close();
}
答案 1 :(得分:0)
从提供的日志中,我可以看到插入发生但似乎没有提交。因此,在persistence.xml中添加以下属性
<property name="eclipselink.target-database" value="Derby"/> and add this attribute to persistence-unit tag: transaction-type='RESOURCE_LOCAL'
Eclipselink需要知道与之通信的数据库,以确保正确的交易管理。