Hibernate + JPA 5 - Java Web

Asked

Viewed 105 times

0

I’m in need of some help in the following problem.

When running my web java project on Glassfish Hibernate on every run try to recreate my tables.

Minha Entity.

@Entity
@Table(name = "Estados")
public class Estado implements Serializable {

@Id
@Column(name = "Id_Estado")
private long idEstado;

@Column(name = "Nome", length = 100, nullable = false)
private String nome;

@Column(name = "Sigla", length = 2, nullable = false)
private String sigla;


Meu persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/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_1_0.xsd">
<!-- oracle -->
<persistence-unit name="oracles" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>Estado</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
  <property name="hibernate.archive.autodetection" value="class"/>
  <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
  <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
  <property name="hibernate.connection.url" value="jdbc:oracle:thin:@192.168.0.68:1521:dbteste"/>
  <property name="hibernate.connection.username" value="teste"/>
  <property name="hibernate.connection.password" value="teste"/>
  <property name="hibernate.show_sql" value="true"/>
  <!-- <property name="hibernate.flushMode" value="FLUSH_AUTO"/> -->
  <property name="hibernate.hbm2ddl.auto" value="update"/>
  <property name="hibernate.c3p0.min_size" value="10"/>
  <property name="hibernate.c3p0.max_size" value="20"/>
  <property name="hibernate.c3p0.idle_test_period" value="1"/>
  <property name="hibernate.c3p0.idle_test_period" value="3000"/>
  <property name="hibernate.c3p0.max_statements" value="50"/>
  <property name="hibernate.c3p0.timeout" value="1800"/>
</properties>

The error that occurs below. (The table already exists in the database, in the last row I do a search to which returns the value Rondônia.)

Informações:   HHH000204: Processing PersistenceUnitInfo [
name: oracles...]
Informações:   HHH000412: Hibernate Core {5.2.12.Final}
Informações:   HHH000206: hibernate.properties not found
Informações:   HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Informações:   HHH010002: C3P0 using driver: oracle.jdbc.driver.OracleDriver at URL: jdbc:oracle:thin:@192.168.0.68:1521:dbteste
Informações:   HHH10001001: Connection properties: {user=teste, password=****}
Informações:   HHH10001003: Autocommit mode: false
Informações:   MLog clients using java 1.4+ standard logging.
Informações:   Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
Informações:   HHH10001007: JDBC isolation level: <unknown>
Informações:   Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@46edde46 [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@fdf96dcd [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, debugUnreturnedConnectionStackTraces -> false, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge12p9u1bevoz21uwg0p0|6c2b1700, idleConnectionTestPeriod -> 3000, initialPoolSize -> 10, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 1800, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 20, maxStatements -> 50, maxStatementsPerConnection -> 0, minPoolSize -> 10, nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@846def9c [ description -> null, driverClass -> null, factoryClassLocation -> null, identityToken -> 1hge12p9u1bevoz21uwg0p0|b35b671, jdbcUrl -> jdbc:oracle:thin:@192.168.0.68:1521:dbteste, properties -> {user=******, password=******} ], preferredTestQuery -> null, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false; userOverrides: {} ], dataSourceName -> null, factoryClassLocation -> null, identityToken -> 1hge12p9u1bevoz21uwg0p0|619c81d0, numHelperThreads -> 3 ]
Informações:   HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
Informações:   HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
Informações:   HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@5bd0f576] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
Informações:   Hibernate: create table Estados (Id_Estado number(19,0) not null, Nome varchar2(100 char) not null, Sigla varchar2(2 char) not null, primary key (Id_Estado))
WARN:   GenerationTarget encountered exception accepting command : Error executing DDL via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlString(AbstractSchemaMigrator.java:559)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlStrings(AbstractSchemaMigrator.java:504)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.createTable(AbstractSchemaMigrator.java:277)
    at org.hibernate.tool.schema.internal.GroupedSchemaMigratorImpl.performTablesMigration(GroupedSchemaMigratorImpl.java:71)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.performMigration(AbstractSchemaMigrator.java:207)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.doMigration(AbstractSchemaMigrator.java:114)
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:183)
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:72)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:313)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:452)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:889)
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
    at ConexaoOracle.<init>(ConexaoOracle.java:23)
    at ConexaoOracle.getEntityManager(ConexaoOracle.java:28)
    at ProdutoRosourcesw.getProduto(ProdutoRosourcesw.java:49)
    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 org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:76)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:148)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:191)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:243)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:103)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:493)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:415)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:104)
    at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:277)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:272)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:268)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:316)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:298)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:268)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:289)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:256)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:703)
    at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:416)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:370)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:389)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:342)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:229)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1580)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:258)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:652)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:591)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:371)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:238)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:463)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:168)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:242)
    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:539)
    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:593)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:573)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.sql.SQLSyntaxErrorException: ORA-00955: nome já está sendo usado por um objeto existente
    at oracle.jdbc.driver.T4CTTIoer11.processError(T4CTTIoer11.java:494)
    at oracle.jdbc.driver.T4CTTIoer11.processError(T4CTTIoer11.java:446)
    at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1054)
    at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:623)
    at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:252)
    at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:612)
    at oracle.jdbc.driver.T4CStatement.doOall8(T4CStatement.java:213)
    at oracle.jdbc.driver.T4CStatement.doOall8(T4CStatement.java:37)
    at oracle.jdbc.driver.T4CStatement.executeForRows(T4CStatement.java:896)
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1119)
    at oracle.jdbc.driver.OracleStatement.executeInternal(OracleStatement.java:1737)
    at oracle.jdbc.driver.OracleStatement.execute(OracleStatement.java:1692)
    at oracle.jdbc.driver.OracleStatementWrapper.execute(OracleStatementWrapper.java:300)
    at com.mchange.v2.c3p0.impl.NewProxyStatement.execute(NewProxyStatement.java:1006)
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:54)
    ... 71 more
Caused by: Error : 955, Position : 13, Sql = create table Estados (Id_Estado number(19,0) not null, Nome varchar2(100 char) not null, Sigla varchar2(2 char) not null, primary key (Id_Estado)), OriginalSql = create table Estados (Id_Estado number(19,0) not null, Nome varchar2(100 char) not null, Sigla varchar2(2 char) not null, primary key (Id_Estado)), Error Msg = ORA-00955: nome já está sendo usado por um objeto existente
    at oracle.jdbc.driver.T4CTTIoer11.processError(T4CTTIoer11.java:498)
    ... 85 more

Informações:   Hibernate: select estado0_.Id_Estado as Id_Estado1_0_0_, estado0_.Nome as Nome2_0_0_, estado0_.Sigla as Sigla3_0_0_ from Estados estado0_ where estado0_.Id_Estado=?
Informações:   Rondônia

I have the same code running in a desktop project and error does not occur.

  • I already deleted the tables and let the system recreate again by Hibernate and even then in the next run it causes the same error.
  • Try to remove the existing table and let Hibernate create the table.

  • Allan, I have already performed this procedure, including creating a table from scratch and the same problem is occurring.

1 answer

0

You informed Update on the property "Hibernate.hbm2ddl.auto" in your persistence. When Hibernate was updating the tables it triggered the error "name is already being used by an existing object".

The error is related to the index name of the primary key. When Oracle was updating the table, it did not find/use the existing index. Tried to create a new index with the same name and triggered the error. Oracle does not allow to have two equal indexes.

You can remove the "Hibernate.hbm2ddl.auto" property from persistence and update your bank manually. In this situation I recommend that you use Flyway to versioning your database through SQL scripts to create the tables. Or you can change the property to create-drop, so Hibernate will create the tables when the project runs and when you stop the server Hibernate will delete the database. So it will always create database from scratch avoiding error.

If these solutions are not feasible for your case. Read more about the error in this link: http://eduardolegatti.blogspot.com/2011/11/desvendando-o-erro-ora-00955-durante.html#ixzz5CBktYGNw

  • Tiago, I use the same classes in a desktop project and this situation does not occur, ie he does not try to recreate the tables, the table mentioned was created by Hibernate itself.

  • The desktop project connects to the same Oracle database?

  • Connects in the same database/user and password besides using the same persistence.xml

Browser other questions tagged

You are not signed in. Login or sign up in order to post.