英文:
javax.ejb.EJBException: Transaction aborted while inserting record to Derby db table using Entity Manager
问题
这里是错误日志和相关代码的翻译:
错误日志:
javax.faces.el.EvaluationException: javax.ejb.EJBException: 事务已中止
...
Caused by: javax.transaction.RollbackException: 标记为回滚的事务。
...
Caused by: javax.persistence.PersistenceException: 异常 [EclipseLink-4002](Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd):org.eclipse.persistence.exceptions.DatabaseException
内部异常:java.sql.SQLSyntaxErrorException:语法错误:在第1行第13列遇到 "Order"。
错误代码:20000
查询:INSERT INTO Order(ID,Quantity,TIMECREATED,UNITPRICE,CARID,CUSTOMERID)VALUES(?,?,?,?,?,?)
...
实体类:
@Entity
@Table(name = "CUSTOMER")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "AllCustomer", query = "SELECT c FROM Customer c")
, @NamedQuery(name = "findCustomerByName", query = "SELECT c FROM Customer c WHERE c.name = :name")
, @NamedQuery(name="findCustomerById",query= "SELECT c FROM Customer c WHERE c.id = :id")})
public class Customer implements Serializable {
...
@OneToMany
private List<Order> carorders;
...
}
@Entity
@Table(name = "BRANDNEWCAR")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "AllNewCars", query = "SELECT b FROM NewCar b")
, @NamedQuery(name = "findNewCarByReferencenumber", query = "SELECT b FROM NewCar b WHERE b.referencenumber = :referencenumber")
, @NamedQuery(name="findNewCarById",query= "SELECT c FROM NewCar c WHERE c.id = :id")})
public class NewCar implements Serializable {
...
@OneToMany
private List<Order> carorders;
...
}
@Entity
@Table(name = "Order")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "AllOrders", query = "SELECT c FROM Order c")
, @NamedQuery(name = "findOrderById", query = "SELECT c FROM Order c WHERE c.id = :id")
, @NamedQuery(name = "DeleteOrder", query = "DELETE FROM Order c WHERE c.id = :id")})
public class Order implements Serializable {
...
@ManyToOne(cascade={
CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH})
@JoinColumn(name="CUSTOMERID", referencedColumnName = "ID")
private Customer customer;
@ManyToOne
@JoinColumn(name="CARID", referencedColumnName = "ID")
private NewCar car;
...
}
Java Managed Bean 代码:
order.setCustomer(selectedcustomer);
order.setCar(selectedcar);
order.unitPrice = selectedcar.getPrice();
order = orderEJB.CreateNewOrder(order);
persistence.xml 配置:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="CarSalesEnterpriseApplicationPU" transaction-type="JTA">
<jta-data-source>jdbc/carsalesenterpriseapp</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
</properties>
</persistence-unit>
</persistence>
希望这些翻译对您有帮助。如果您需要进一步的帮助,请随时提问。
英文:
hi i'm working on Java EE 7 web application everything works perfect but when i try to create Order which have Many-to-one relationship with two entities i.e. Car and Customer i have tried all other answers that are posted around. please take a look at the code below
Here is the Error Log
javax.faces.el.EvaluationException: javax.ejb.EJBException: Transaction aborted
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
at java.lang.Thread.run(Thread.java:748)
Caused by: javax.ejb.EJBException: Transaction aborted
at com.sun.ejb.containers.EJBContainerTransactionManager.completeNewTx(EJBContainerTransactionManager.java:725)
at com.sun.ejb.containers.EJBContainerTransactionManager.postInvokeTx(EJBContainerTransactionManager.java:503)
at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4566)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2074)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2044)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:220)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
at com.sun.proxy.$Proxy380.CreateNewOrder(Unknown Source)
at EJBModule.__EJB31_Generated__OrderEJB__Intf____Bean__.CreateNewOrder(Unknown Source)
at EJBModule.OrderController.doCreateOrder(OrderController.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.el.parser.AstValue.invoke(AstValue.java:289)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
... 36 more
Caused by: javax.transaction.RollbackException: Transaction marked for rollback.
at com.sun.enterprise.transaction.JavaEETransactionImpl.commit(JavaEETransactionImpl.java:490)
at com.sun.enterprise.transaction.JavaEETransactionManagerSimplified.commit(JavaEETransactionManagerSimplified.java:854)
at com.sun.ejb.containers.EJBContainerTransactionManager.completeNewTx(EJBContainerTransactionManager.java:719)
... 55 more
Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: Syntax error: Encountered "Order" at line 1, column 13.
Error Code: 20000
Call: INSERT INTO Order (ID, Quantity, TIMECREATED, UNITPRICE, CARID, CUSTOMERID) VALUES (?, ?, ?, ?, ?, ?)
bind => [6 parameters bound]
Query: InsertObjectQuery(EJBModule.Order[ id=3 ])
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl$1.handleException(EntityManagerSetupImpl.java:696)
at org.eclipse.persistence.transaction.AbstractSynchronizationListener.handleException(AbstractSynchronizationListener.java:275)
at org.eclipse.persistence.transaction.AbstractSynchronizationListener.beforeCompletion(AbstractSynchronizationListener.java:170)
at org.eclipse.persistence.transaction.JTASynchronizationListener.beforeCompletion(JTASynchronizationListener.java:68)
at com.sun.enterprise.transaction.JavaEETransactionImpl.commit(JavaEETransactionImpl.java:452)
... 57 more
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: Syntax error: Encountered "Order" at line 1, column 13.
Error Code: 20000
Call: INSERT INTO Order (ID, Quantity, TIMECREATED, UNITPRICE, CARID, CUSTOMERID) VALUES (?, ?, ?, ?, ?, ?)
bind => [6 parameters bound]
Query: InsertObjectQuery(EJBModule.Order[ id=3 ])
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:340)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:682)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:558)
at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(AbstractSession.java:2002)
at org.eclipse.persistence.sessions.server.ClientSession.executeCall(ClientSession.java:298)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:242)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:228)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.insertObject(DatasourceCallQueryMechanism.java:377)
at org.eclipse.persistence.internal.queries.StatementQueryMechanism.insertObject(StatementQueryMechanism.java:165)
at org.eclipse.persistence.internal.queries.StatementQueryMechanism.insertObject(StatementQueryMechanism.java:180)
at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.insertObjectForWrite(DatabaseQueryMechanism.java:489)
at org.eclipse.persistence.queries.InsertObjectQuery.executeCommit(InsertObjectQuery.java:80)
at org.eclipse.persistence.queries.InsertObjectQuery.executeCommitWithChangeSet(InsertObjectQuery.java:90)
at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.executeWriteWithChangeSet(DatabaseQueryMechanism.java:301)
at org.eclipse.persistence.queries.WriteObjectQuery.executeDatabaseQuery(WriteObjectQuery.java:58)
at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:899)
at org.eclipse.persistence.queries.DatabaseQuery.executeInUnitOfWork(DatabaseQuery.java:798)
at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWorkObjectLevelModifyQuery(ObjectLevelModifyQuery.java:108)
at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWork(ObjectLevelModifyQuery.java:85)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2896)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1804)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1786)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1737)
at org.eclipse.persistence.internal.sessions.CommitManager.commitNewObjectsForClassWithChangeSet(CommitManager.java:226)
at org.eclipse.persistence.internal.sessions.CommitManager.commitAllObjectsWithChangeSet(CommitManager.java:125)
at org.eclipse.persistence.internal.sessions.AbstractSession.writeAllObjectsWithChangeSet(AbstractSession.java:4207)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabase(UnitOfWorkImpl.java:1441)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabaseWithChangeSet(UnitOfWorkImpl.java:1531)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.issueSQLbeforeCompletion(UnitOfWorkImpl.java:3168)
at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.issueSQLbeforeCompletion(RepeatableWriteUnitOfWork.java:352)
at org.eclipse.persistence.transaction.AbstractSynchronizationListener.beforeCompletion(AbstractSynchronizationListener.java:158)
... 59 more
Caused by: java.sql.SQLSyntaxErrorException: Syntax error: Encountered "Order" at line 1, column 13.
at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
at org.apache.derby.client.am.Connection.prepareStatement(Unknown Source)
at com.sun.gjc.spi.base.ConnectionHolder.prepareStatement(ConnectionHolder.java:586)
at com.sun.gjc.spi.jdbc40.ConnectionWrapper40.prepareCachedStatement(ConnectionWrapper40.java:255)
at com.sun.gjc.spi.jdbc40.ConnectionWrapper40.prepareCachedStatement(ConnectionWrapper40.java:52)
at com.sun.gjc.spi.ManagedConnectionImpl.prepareCachedStatement(ManagedConnectionImpl.java:992)
at com.sun.gjc.spi.jdbc40.ConnectionWrapper40.prepareStatement(ConnectionWrapper40.java:173)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.prepareStatement(DatabaseAccessor.java:1556)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.prepareStatement(DatabaseAccessor.java:1505)
at org.eclipse.persistence.internal.databaseaccess.DatabaseCall.prepareStatement(DatabaseCall.java:778)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:619)
... 88 more
Caused by: org.apache.derby.client.am.SqlException: Syntax error: Encountered "Order" at line 1, column 13.
at org.apache.derby.client.am.Statement.completeSqlca(Unknown Source)
at org.apache.derby.client.net.NetStatementReply.parsePrepareError(Unknown Source)
at org.apache.derby.client.net.NetStatementReply.parsePRPSQLSTTreply(Unknown Source)
at org.apache.derby.client.net.NetStatementReply.readPrepareDescribeOutput(Unknown Source)
at org.apache.derby.client.net.StatementReply.readPrepareDescribeOutput(Unknown Source)
at org.apache.derby.client.net.NetStatement.readPrepareDescribeOutput_(Unknown Source)
at org.apache.derby.client.am.Statement.readPrepareDescribeOutput(Unknown Source)
at org.apache.derby.client.am.PreparedStatement.readPrepareDescribeInputOutput(Unknown Source)
at org.apache.derby.client.am.PreparedStatement.flowPrepareDescribeInputOutput(Unknown Source)
at org.apache.derby.client.am.PreparedStatement.prepare(Unknown Source)
at org.apache.derby.client.am.Connection.prepareStatementX(Unknown Source)
... 98 more
Here are the Entity Classes
@Entity
@Table(name = "CUSTOMER")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "AllCustomer", query = "SELECT c FROM Customer c")
, @NamedQuery(name = "findCustomerByName", query = "SELECT c FROM Customer c WHERE c.name = :name")
, @NamedQuery(name="findCustomerById",query= "SELECT c FROM Customer c WHERE c.id = :id")})
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Size(max = 30)
@Column(name = "NAME")
private String name;
@Size(max = 30)
@Column(name = "EMAIL")
private String email;
@Size(max = 30)
@Column(name = "PHONE")
private String phone;
@Size(max = 30)
@Column(name = "ADDRESS")
private String address;
@OneToMany
private List<Order> carorders;
@Entity
@Table(name = "BRANDNEWCAR")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "AllNewCars", query = "SELECT b FROM NewCar b")
, @NamedQuery(name = "findNewCarByReferencenumber", query = "SELECT b FROM NewCar b WHERE b.referencenumber = :referencenumber")
, @NamedQuery(name="findNewCarById",query= "SELECT c FROM NewCar c WHERE c.id = :id")})
public class NewCar implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Size(max = 30)
@Column(name = "REFERENCENUMBER")
private String referencenumber;
@Column(name = "MAKE")
private String make;
@Size(max = 30)
@Column(name = "MODEL")
private String model;
@Size(max = 30)
@Column(name = "DRIVETYPE")
private String drivetype;
@Size(max = 30)
@Column(name = "BODYCOLOUR")
private String bodycolour;
@Size(max = 30)
@Column(name = "TRANSMISSION")
private String transmission;
@Size(max = 30)
@Column(name = "ENGINE")
private String engine;
@Size(max = 30)
@Column(name = "FUELTYPE")
private String fueltype;
@Column(name = "DOORS")
private Integer doors;
@Column(name = "SEATS")
private Integer seats;
@Column(name = "PRICE")
private Float price;
@Column(name = "WARRANTY")
private Integer warranty;
@Column(name = "EXTENDEDWARRANTY")
private Integer extendedwarranty;
@Size(max = 30)
@Column(name = "ASSISTANCE")
private String assistance;
@Column(name = "StockQty")
private Integer stockqty;
@OneToMany
private List<Order> carorders;
@Entity
@Table(name = "Order")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "AllOrders", query = "SELECT c FROM Order c")
, @NamedQuery(name = "findOrderById", query = "SELECT c FROM Order c WHERE c.id = :id")
, @NamedQuery(name = "DeleteOrder", query = "DELETE FROM Order c WHERE c.id = :id")})
public class Order implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false)
protected Float unitPrice;
@Column(nullable = false)
@Temporal(TemporalType.TIMESTAMP)
protected Date timeCreated = new Date();
@Column(name = "Quantity")
private Integer Qty;
@ManyToOne(cascade={
CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH})
@JoinColumn(name="CUSTOMERID", referencedColumnName = "ID")
private Customer customer;
@ManyToOne
@JoinColumn(name="CARID", referencedColumnName = "ID")
private NewCar car;
Here is the code which used in Java Managed Bean to create record
order.setCustomer(selectedcustomer);
order.setCar(selectedcar);
order.unitPrice = selectedcar.getPrice();
order = orderEJB.CreateNewOrder(order);
here are the details of Persistance.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="CarSalesEnterpriseApplicationPU" transaction-type="JTA">
<jta-data-source>jdbc/carsalesenterpriseapp</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
</properties>
</persistence-unit>
</persistence>
I don't know why it doesn't work, thanks in advance.
答案1
得分: 3
这是Derby SQL中的保留字列表:http://db.apache.org/derby/docs/10.15/ref/rrefkeywords29722.html
ORDER
是其中的一个保留字。
您能否将您的应用程序更改为使用其他单词而不是ORDER
?
英文:
Here's the list of reserved words in Derby's SQL: http://db.apache.org/derby/docs/10.15/ref/rrefkeywords29722.html
ORDER
is one of those reserved words.
Can you change your application to use a different word instead of ORDER
?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论