我想在oracle数据库中使用hibernate创建一个简单的表“account”。
这是我的bean类Account
:
@Entity
@Table(name = "account")
public class Account implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(nullable = false)
private String id;
@Column(nullable = false)
private String iban;
@Column(nullable = false)
private String name;
@Column(name = "`usage`", nullable = false)
private String usage;
@Column(nullable = false)
private String type;
@Column(nullable = false)
private String ccy;
@Column(nullable = false)
private String psuStatus;
@Column(nullable = false)
private String balances;
public Account() {
}
public Account(String id, String iban, String name, String usage,
String type, String ccy, String psuStatus, String balances) {
this.id = id;
this.iban = iban;
this.name = name;
this.usage = usage;
this.type = type;
this.ccy = ccy;
this.psuStatus = psuStatus;
this.balances = balances;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getIban() {
return iban;
}
public void setIban(String iban) {
this.iban = iban;
}
public String getUsage() {
return usage;
}
public void setUsage(String usage) {
this.usage = usage;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getCcy() {
return ccy;
}
public void setCcy(String ccy) {
this.ccy = ccy;
}
public String getPsuStatus() {
return psuStatus;
}
public void setPsuStatus(String psuStatus) {
this.psuStatus = psuStatus;
}
public String getBalances() {
return balances;
}
public void setBalances(String balances) {
this.balances = balances;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Account other = (Account) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
这是配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<!-- dataSource (connexion vers la base de données) -->
<!-- dataSource côté serveur <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/myDataSource"
/> -->
<!-- datasource locale -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="removed for privacy purposes" />
<property name="username" value="removed for privacy purposes" />
<property name="password" value="removed for privacy purposes" />
</bean>
<!-- support d'annotations JPA -->
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<!-- config de la sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>fr.bnpparibas.dsp2aisp.beans</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQL5InnoDBDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<!-- <prop key="hibernate.generate_statistics">true</prop> -->
<prop key="hibernate.hbm2ddl.auto">create</prop> <!-- update -->
<!-- Cache de niv2 -->
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">
org.hibernate.cache.ehcache.EhCacheRegionFactory
</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<!-- Configuring Connection Pool -->
<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.max_size">20</prop>
<prop key="hibernate.c3p0.timeout">50</prop>
<prop key="hibernate.c3p0.max_statements">50</prop>
<prop key="hibernate.c3p0.idle_test_period">2000</prop>
</props>
</property>
</bean>
<!-- support des transactions par annotations @Transactional -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
<property name="autodetectDataSource" value="true" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- MailService -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<!-- SMTP settings -->
<property name="host" value="smtp.dsl.ovh.net" />
<!-- <property name="port" value="21" /> -->
<!-- <property name="username" value="USER_NAME" />
<property name="password" value="PASSWORD" /> -->
<property name="javaMailProperties">
<!-- additional properties specific to JavaMail -->
<props>
<prop key="mail.transport.protocol">smtp</prop>
<prop key="mail.smtp.auth">false</prop>
<prop key="mail.smtp.starttls.enable">false</prop>
</props>
</property>
</bean>
<!-- surcouche de l'entityManager => Hibernate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="accountDao" class="fr.bnpparibas.dsp2aisp.daos.AccountDao">
<property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>
</beans>
问题是,当我运行应用程序时,它不会在数据库中创建表account
。我使用了与MySQL JDBC驱动程序和wampserver相同的示例,它运行得很好。但是使用Oracle驱动程序,出于某种原因,它不起作用。谁知道为什么?
更新:我可以通过将hbm2ddl记录器设置为true来显示错误,这就是我现在所拥有的:
java.sql.SQLException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:673)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:711)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:385)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:30)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:558)
at java.sql.DriverManager.getConnection(DriverManager.java:675)
at java.sql.DriverManager.getConnection(DriverManager.java:219)
at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:153)
at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:144)
at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:196)
at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:159)
at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122)
at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:33)
at org.hibernate.tool.hbm2ddl.DatabaseExporter.<init>(DatabaseExporter.java:35)
at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:425)
at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:361)
at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:350)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:469)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:711)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:727)
at org.springframework.orm.hibernate5.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:511)
at org.springframework.orm.hibernate5.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:495)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1688)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1626)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:742)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:443)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:325)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at com.ibm.ws.webcontainer.webapp.WebApp.notifyServletContextCreated(WebApp.java:2384)
at com.ibm.ws.webcontainer31.osgi.webapp.WebApp31.notifyServletContextCreated(WebApp31.java:514)
at com.ibm.ws.webcontainer.webapp.WebApp.initialize(WebApp.java:1012)
at com.ibm.ws.webcontainer.webapp.WebApp.initialize(WebApp.java:6574)
at com.ibm.ws.webcontainer.osgi.DynamicVirtualHost.startWebApp(DynamicVirtualHost.java:467)
at com.ibm.ws.webcontainer.osgi.DynamicVirtualHost.startWebApplication(DynamicVirtualHost.java:462)
at com.ibm.ws.webcontainer.osgi.WebContainer.startWebApplication(WebContainer.java:1132)
at com.ibm.ws.webcontainer.osgi.WebContainer.access$000(WebContainer.java:108)
at com.ibm.ws.webcontainer.osgi.WebContainer$3.run(WebContainer.java:944)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:522)
at java.util.concurrent.FutureTask.run(FutureTask.java:277)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1153)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.lang.Thread.run(Thread.java:785)
这是否意味着我需要定义oracle架构?
更新2 :现在我有这个错误:
14:29:58.026 [Default Executor-thread-12] ERROR org.hibernate.tool.hbm2ddl.SchemaExport - HHH000389: Unsuccessful: create table account (id varchar(255) not null, balances varchar(255) not null, ccy varchar(255) not null, iban varchar(255) not null, name varchar(255) not null, psuStatus varchar(255) not null, type varchar(255) not null, `usage` varchar(255) not null, primary key (id)) ENGINE=InnoDB
14:29:58.027 [Default Executor-thread-12] ERROR org.hibernate.tool.hbm2ddl.SchemaExport - ORA-00911: invalid character
但奇怪的是,我正在使用Hibernate生成查询。我自己没有写查询。