使用JPA进行数据库操作

时间:2018-07-17 13:45:11

标签: java jpa

代码工作正常,但是当我尝试添加提交命令时,代码开始出现异常。任何建议都会有所帮助。

transaction.commit();行引起了问题。

public class MovieDAO 
{
    /**
     * Adds an movie record to database from the given {@link Movie}
     * Displays a success message upon successful execution
     * @param movie
     * @throws Exception
     */

    public void addMovie(Movie movie) throws Exception 
    {
        EntityManagerFactory factory = null;
        EntityManager manager = null;
        MovieEntity movieobj = null;

        try
        {
            factory = Persistence.createEntityManagerFactory("Demo");
            manager = factory.createEntityManager();
            movieobj = new MovieEntity();
            movieobj.setLanguage(movie.getLanguage());
            movieobj.setMovieId(movie.getMovieId());
            movieobj.setMovieName(movie.getMovieName());
            movieobj.setReleasedIn(movie.getReleasedIn());
            movieobj.setRevenueInDollars(movie.getRevenueInDollars());
            EntityTransaction transaction = manager.getTransaction(); 
            transaction.begin();
            manager.persist(movieobj);
            transaction.commit();
        }
        catch(Exception e)
        {
            DOMConfigurator.configure("src/resources/log4j.xml");
            Logger logger = Logger.getLogger(this.getClass());
            logger.error(e.getMessage(), e);
            //throw new Exception("DAO.TECHNICAL_ERROR");
            e.printStackTrace();
        }
        finally
        {
            if(manager != null)
                manager.close();

            if(factory != null)
                factory.close();
        }
    }
}

我得到的错误是:

javax.persistence.RollbackException: Error while committing the transaction
    at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:94)
    at dao.MovieDAO.addMovie(MovieDAO.java:44)
    at ui.UserInterface.addMovie(UserInterface.java:23)
    at ui.UserInterface.main(UserInterface.java:39)
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute statement
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1763)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
    at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:82)
    ... 3 more
Caused by: org.hibernate.exception.SQLGrammarException: could not execute statement
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:80)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211)
    at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:62)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3124)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3581)
    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:104)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:465)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:351)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1258)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
    at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:77)
    ... 3 more
Caused by: java.sql.SQLSyntaxErrorException: ORA-00942: table or view does not exist

    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:450)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:399)
    at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1059)
    at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:522)
    at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:257)
    at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:587)
    at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:225)
    at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:53)
    at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:943)
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1150)
    at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:4798)
    at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:4875)
    at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeUpdate(OraclePreparedStatementWrapper.java:1361)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
    ... 16 more

1 个答案:

答案 0 :(得分:0)

似乎您缺少一张桌子:

ORA-00942: table or view does not exist

检查实体的声明表,并验证该表是否存在于Oracle数据库中。