我正在尝试使用hibernate在数据库中保存对象。当我第一次使用新的表名运行代码时,它会抛出错误,但是当我第二次运行它时它完全正常 -
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@61ce23ac] 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.
Sep 01, 2016 3:51:18 AM org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl handleException
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:62)
at org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlString(SchemaDropperImpl.java:374)
at org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlStrings(SchemaDropperImpl.java:359)
at org.hibernate.tool.schema.internal.SchemaDropperImpl.dropFromMetadata(SchemaDropperImpl.java:241)
at org.hibernate.tool.schema.internal.SchemaDropperImpl.performDrop(SchemaDropperImpl.java:154)
at org.hibernate.tool.schema.internal.SchemaDropperImpl.doDrop(SchemaDropperImpl.java:126)
at org.hibernate.tool.schema.internal.SchemaDropperImpl.doDrop(SchemaDropperImpl.java:112)
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:137)
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:65)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:307)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:490)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:710)
at org.sf.main.SessionFactoryBuilder.getSessionFactory(SessionFactoryBuilder.java:26)
at org.sf.main.SessionFactoryBuilder.main(SessionFactoryBuilder.java:36)
Caused by: java.sql.SQLSyntaxErrorException: ORA-00942: table or view does not exist
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:447)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:951)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:513)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:227)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:531)
at oracle.jdbc.driver.T4CStatement.doOall8(T4CStatement.java:195)
at oracle.jdbc.driver.T4CStatement.executeForRows(T4CStatement.java:1036)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1336)
at oracle.jdbc.driver.OracleStatement.executeInternal(OracleStatement.java:1916)
at oracle.jdbc.driver.OracleStatement.execute(OracleStatement.java:1878)
at oracle.jdbc.driver.OracleStatementWrapper.execute(OracleStatementWrapper.java:318)
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:49)
... 13 more
这是我的代码 -
public static SessionFactory getSessionFactory() {
Configuration configuration = new Configuration();
configuration.setProperty("hibernate.hbm2ddl.auto", "create");
configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
configuration.setProperty("hibernate.connection.driver_class", "oracle.jdbc.driver.OracleDriver");
configuration.setProperty("hibernate.connection.url", "jdbc:oracle:thin:@somehost:1521:orcl");
configuration.setProperty("hibernate.connection.username", "username");
configuration.setProperty("hibernate.connection.password", "password");
configuration.setProperty("show_sql", "true");
configuration.setProperty("hibernate.default_schema", "somechema");
configuration.setPhysicalNamingStrategy(new HelloWorldNamingStrategy());
configuration.addAnnotatedClass(Census2010.class);
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
SessionFactory sessionFactory = configuration.buildSessionFactory(builder.build());
return sessionFactory;
}
public static void main (String[] args) {
Census2010 obj = new Census2010();
obj.setData(100000L);
obj.setVariable("Population");
SessionFactory factory = getSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
session.save(obj);
session.getTransaction().commit();
factory.close();
}
这是我的实体类 -
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table
public class Census2010 {
@Id
@Column(name="VARIABLE")
private String variable;
@Column(name="DATA")
private long data;
public String getVariable() {
return variable;
}
public void setVariable(String variable) {
this.variable = variable;
}
public long getData() {
return data;
}
public void setData(long data) {
this.data = data;
}
}
这是NamingStrategy类 -
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
public class HelloWorldNamingStrategy extends PhysicalNamingStrategyStandardImpl {
private static final long serialVersionUID = 1L;
private static final String tableName = "A_12345_P";
@Override
public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
return new Identifier(tableName, true);
}
}
感谢任何帮助。
答案 0 :(得分:1)
在hibernate.cfg.xml文件中将hbm2ddl.auto属性设置为true时,您要求Hibernate框架创建与您尝试保存的对象相对应的所有表,然后保存对象。
但是,在创建表之前,hibernate首先尝试删除表,假设它们存在于数据库中。
因此,当您第一次运行应用程序时,由于表格不存在于数据库中,您将面临错误。当你下次运行你的程序时,你不会看到上面的错误,因为表存在并且hibernate将能够成功地删除它们。
答案 1 :(得分:0)
你确定&#34; somechema&#34;在句子中configuration.setProperty("hibernate.default_schema", "somechema");
是正确的shema?
如果找不到正确的表或视图。
另一个选项是使用注释,示例
@Table(schema = "YOURSCHEMA", name = "Census2010")
了解更多信息https://docs.jboss.org/jbossas/jboss4guide/r2/html/ch13.html