我的servlet有一个令人沮丧的问题。这是非常基本的,我应该在我的数据库表中添加一个博客条目,其中包含3列(id,tittel(title),main(mainbody))。我可以从我的数据库加载并显示在我的jsp文件中。但是我无法将数据保存到数据库中,我无法弄清楚原因。
我使用postgres作为数据库。
这是我的“saveBlog” - 方法:
public void lagreBlogg(Blogg b){
EntityManager em = Persistence.createEntityManagerFactory("kimsBlogg").createEntityManager();
EntityTransaction transaction = em.getTransaction();
try {
transaction.begin();
em.persist(b);
transaction.commit();
} catch (Exception e) {
transaction.rollback();
} finally {
em.close();
}
}
}
这是我的实体类:
@Entity
@Table(schema = "blogg", name = "blogg")
public class Blogg {
@Id
@GeneratedValue
private int id;
private String tittel;
private String main;
public Blogg() {
}
public Blogg(String tittel, String main){
this.tittel= tittel;
this.main = main;
}
public String getTittel() {
return tittel;
}
public void setTittel(String tittel) {
this.tittel = tittel;
}
public String getMain() {
return main;
}
public void setMain(String main) {
this.main = main;
}
}
我的persistence.xml
<?xml version="1.0" encoding="UTF-8" ?>
<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_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="kimsBlogg">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl
</provider>
<class>no.kim.model.Blogg</class>
<properties>
<property name="javax.persistence.jdbc.logging.level" value="INFO" />
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/postgres" />
<property name="javax.persistence.jdbc.user" value="postgres" />
<property name="javax.persistence.jdbc.password" value="krheggen" />
<property name="openjpa.RuntimeUnenhancedClasses" value="supported" />
</properties>
</persistence-unit>
这是我得到的错误:
HTTP状态500 - 只能在事务处于活动状态时执行操作。
输入例外报告
message只能在事务处于活动状态时执行操作。
说明服务器遇到内部错误,导致无法完成此请求。
例外
org.apache.openjpa.persistence.InvalidStateException:只能在事务处于活动状态时执行操作。 org.apache.openjpa.kernel.BrokerImpl.assertTransactionOperation(BrokerImpl.java:4732) org.apache.openjpa.kernel.BrokerImpl.rollback(BrokerImpl.java:1543) org.apache.openjpa.kernel.DelegatingBroker.rollback(DelegatingBroker.java:940) org.apache.openjpa.persistence.EntityManagerImpl.rollback(EntityManagerImpl.java:598) no.kim.helpers.BloggDAO.lagreBlogg(BloggDAO.java:37) no.kim.Controller.BloggServlet.doPost(BloggServlet.java:33) javax.servlet.http.HttpServlet.service(HttpServlet.java:650) javax.servlet.http.HttpServlet.service(HttpServlet.java:731) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
栈跟踪
引起:org.apache.openjpa.persistence.PersistenceException:错误:关系“blogg.openjpa_sequence_table”不存在 位置:28 {prepstmnt 166406485 SELECT SEQUENCE_VALUE from blogg.OPENJPA_SEQUENCE_TABLE WHERE ID =? FOR UPDATE} [code = 0,state = 42P01] 在org.apache.openjpa.jdbc.sql.DBDictionary.narrow(DBDictionary.java:5001) at org.apache.openjpa.jdbc.sql.DBDictionary.newStoreException(DBDictionary.java:4961) 在org.apache.openjpa.jdbc.sql.SQLExceptions.getStore(SQLExceptions.java:133) 在org.apache.openjpa.jdbc.sql.SQLExceptions.getStore(SQLExceptions.java:107) 在org.apache.openjpa.jdbc.sql.SQLExceptions.getStore(SQLExceptions.java:59) 在org.apache.openjpa.jdbc.kernel.AbstractJDBCSeq.next(AbstractJDBCSeq.java:63) 在org.apache.openjpa.util.ImplHelper.generateValue(ImplHelper.java:159) 在org.apache.openjpa.util.ImplHelper.generateFieldValue(ImplHelper.java:143) 在org.apache.openjpa.jdbc.kernel.JDBCStoreManager.assignField(JDBCStoreManager.java:774) 在org.apache.openjpa.util.ApplicationIds.assign(ApplicationIds.java:492) 在org.apache.openjpa.util.ApplicationIds.assign(ApplicationIds.java:468) at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.assignObjectId(JDBCStoreManager.java:758) 在org.apache.openjpa.kernel.DelegatingStoreManager.assignObjectId(DelegatingStoreManager.java:135) at org.apache.openjpa.kernel.StateManagerImpl.assignObjectId(StateManagerImpl.java:595) at org.apache.openjpa.kernel.StateManagerImpl.preFlush(StateManagerImpl.java:3051) 在org.apache.openjpa.kernel.PNewState.beforeFlush(PNewState.java:44) 在org.apache.openjpa.kernel.StateManagerImpl.beforeFlush(StateManagerImpl.java:1072) 在org.apache.openjpa.kernel.BrokerImpl.flush(BrokerImpl.java:2143) 在org.apache.openjpa.kernel.BrokerImpl.flushSafe(BrokerImpl.java:2103) 在org.apache.openjpa.kernel.BrokerImpl.beforeCompletion(BrokerImpl.java:2021) 在org.apache.openjpa.kernel.LocalManagedRuntime.commit(LocalManagedRuntime.java:81) 在org.apache.openjpa.kernel.BrokerImpl.commit(BrokerImpl.java:1526) 在org.apache.openjpa.kernel.DelegatingBroker.commit(DelegatingBroker.java:932) 在org.apache.openjpa.persistence.EntityManagerImpl.commit(EntityManagerImpl.java:569)
答案 0 :(得分:1)
问题在于catch方法中的" transaction.rollback();"
。如果提交因异常而失败,则连接不再处于活动状态。
答案 1 :(得分:1)
relationship&#34; blogg.openjpa_sequence_table&#34;不存在
这似乎很简单,只需为表blogg
创建一个序列,更多信息here。