HibernateSpatialJPA类:
public class HibernateSpatialJPA {
private static final EntityManagerFactory emFactory;
static {
try {
emFactory = Persistence.createEntityManagerFactory("org.hibernate.events.jpa");
}catch(Throwable ex){
System.err.println("Cannot create EntityManagerFactory.");
throw new ExceptionInInitializerError(ex);
}
}
public static EntityManager createEntityManager() {
return emFactory.createEntityManager();
}
public static void close(){
emFactory.close();
}
}
这是经理班:
public class SavegeojsonManager {
SavegeojsonEntity theEvent = new SavegeojsonEntity();
public boolean insert(JSONObject json)
{
GeoJSON item = new GeoJSON(json);
return insert(item);
}
public boolean insert(GeoJSON item)
{
boolean success=false;
try {
String vectorType = item.getType();
EntityManager em = HibernateSpatialJPA.createEntityManager();
em.getTransaction().begin();
theEvent.setVectorType(vectorType);
em.persist(theEvent);
em.getTransaction().commit();
em.close();
success=true;
}
catch (Exception ex) {
System.out.println(ex.getMessage());
success = false;
}
HibernateSpatialJPA.close();
return success;
}
}
这是GeoJsonClass:
public class GeoJSON
{
private int id;
private String type;
private String data;
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getType() {return type;}
public void setType(String type) {this.type = type;}
public String getData() {return data;}
public void setData(String data) { this.data = data; }
public GeoJSON() {
}
public GeoJSON(JSONObject json) {
parse(json);
}
public GeoJSON parse(JSONObject json) {
StringWriter out = new StringWriter();
json.write(out);
this.data = out.toString();
this.type = json.getString("type");
try {
out.close();
} catch (IOException ex) {
System.out.println(ex.getMessage());
throw new RuntimeException(ex);
}
return this;
}
}
但我无法保存到数据库中,因为错误:
在此输入代码:“提交事务时出错。”
您对此错误有任何疑问吗?
以下是异常堆栈跟踪: -
javax.persistence.RollbackException: Error while committing the transaction
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:92)
at com.springapp.model.SavegeojsonManager.insert(SavegeojsonManager.java:32)
at com.springapp.model.SavegeojsonManager.insert(SavegeojsonManager.java:19)
at com.springapp.mvc.HSpatialController.saveGeoJson(HSpatialController.java:46)
...
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: ERROR: column "vectortype" of relation "savegeojson" does not exist Position: 76 at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1377) at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1300) at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:80) ... 40 more Caused by: org.hibernate.exception.SQLGrammarException: ERROR: column "vectortype" of relation "savegeojson" does not exist Position: 76 at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:122) at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110) at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129) at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81) at com.sun.proxy.$Proxy26.executeUpdate(Unknown Source) at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:56) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2962) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3403) at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:88) at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362) at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:354) at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:275) at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326) at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52) at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1214) at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:403) at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101) at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175) at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:75) ... 40 more Caused by: org.postgresql.util.PSQLException: ERROR: column "vectortype" of relation "savegeojson" does not exist Position: 76 at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2062) at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1795) at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257) at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:479) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:367) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:321) 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:497) at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122) ... 56 more
答案 0 :(得分:0)
您正在将实体SavegeojsonEntity
保存到postgresql数据库,此错误表示:
Caused by: org.postgresql.util.PSQLException: ERROR: column "vectortype" of relation "savegeojson" does not exist
Position: 76
SavegeojsonEntity
映射到的表没有列vectortype
。检查数据库中表中是否存在该列。
如果它不存在,则需要将该列添加到表中。