Hibernate 映射Oracle中的表,为什么会用到MySQL的包?-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

Hibernate 映射Oracle中的表,为什么会用到MySQL的包?

a123456678 2016-03-19 11:08:58 1669
各位大神,小菜鸟有一个疑惑。一个项目里面链接了两个数据库,分别是MySQL和Oracle,他们的DateSource和SessionFactory如下:
<!--start 配置DATASOURCES -->
    <bean id="dataSourceOracle" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close">
        <!-- <property name="name" value="${oracle.driverClassName}" /> -->
        <property name="url" value="${oracle.dailypre_url}" />
        <property name="username" value="${oracle.username}" />
        <property name="password" value="${oracle.password}" />
        <property name="maxOpenPreparedStatements" value="100"/>
 
        <property name="maxActive" value="${druid.max_size}" />
        <property name="filters" value="config" />
        <!-- 配置数据库加密 -->
        <property name="connectionProperties"
            value="config.decrypt=true;config.decrypt.key=${oracle.publickey}" />
    </bean>
 
    <bean id="dataSourceMySQL" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close">
        <!-- <property name="name" value="${mysql.driverClassName}" /> 可根据URL自动判断出DBDRIVER-->
        <property name="url" value="${mysql.dailyreport_url}" />
        <property name="username" value="${mysql.username}" />
        <property name="password" value="${mysql.password}" />
 
        <property name="maxActive" value="${druid.max_size}" />
        <property name="filters" value="config" />
        <!-- 配置数据库加密 -->
        <property name="connectionProperties"
            value="config.decrypt=true;config.decrypt.key=${mysql.publickey}" />
    </bean>
    <!--end 配置DATASOURCES -->
 
    <!--start 配置SessionFactory-->
    <bean id="sessionFactoryMySQL"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSourceMySQL"></property>
        <!-- 注解的实体类通过此扫描的方式注入 -->
        <property name="packagesToScan" value="org.daily.**.entity" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <!-- hibernate.current_session_context_class此处与Spring2.x不同,不能为thread,否则报错:?? 
                    is not valid without active transaction -->
                <prop key="hibernate.current_session_context_class">
                    org.springframework.orm.hibernate5.SpringSessionContext
                </prop>
            </props>
        </property>
    </bean>
 
    <bean id="sessionFactoryOracle"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSourceOracle"></property>
        <!-- 注解的实体类通过此扫描的方式注入 -->
        <property name="packagesToScan" value="org.daily.**.entity" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <!-- hibernate.current_session_context_class此处与Spring2.x不同,不能为thread,否则报错:?? 
                    is not valid without active transaction -->
                <prop key="hibernate.current_session_context_class">
                    org.springframework.orm.hibernate5.SpringSessionContext
                </prop>
            </props>
        </property>
    </bean>
    <!--end 配置SessionFactory-->
现提取MySQL数据库中的数据时,没有问题。

但是当提取Oracle中数据时,进行查询测试,



报错,如下:

Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Unable to execute schema management to JDBC target [create table USER_INFO (USER_ID varchar(255) not null, department1 varchar(255), department2 varchar(255), username varchar(255), primary key (USER_ID)) type=InnoDB]
    at org.hibernate.tool.schema.internal.TargetDatabaseImpl.accept(TargetDatabaseImpl.java:59)
    at org.hibernate.tool.schema.internal.SchemaMigratorImpl.applySqlString(SchemaMigratorImpl.java:431)
    at org.hibernate.tool.schema.internal.SchemaMigratorImpl.applySqlStrings(SchemaMigratorImpl.java:420)
    at org.hibernate.tool.schema.internal.SchemaMigratorImpl.createTable(SchemaMigratorImpl.java:236)
    at org.hibernate.tool.schema.internal.SchemaMigratorImpl.doMigrationToTargets(SchemaMigratorImpl.java:167)
    at org.hibernate.tool.schema.internal.SchemaMigratorImpl.doMigration(SchemaMigratorImpl.java:60)
    at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:133)
    at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:101)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:470)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
    at org.springframework.orm.hibernate5.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:416)
    at org.springframework.orm.hibernate5.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:401)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
    ... 71 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'type=InnoDB' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
    at com.mysql.jdbc.Util.getInstance(Util.java:387)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:941)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3870)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3806)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2470)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2617)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2546)
    at com.mysql.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1541)
    at com.mysql.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2605)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1469)
    at com.alibaba.druid.filter.FilterChainImpl.statement_executeUpdate(FilterChainImpl.java:2377)
    at com.alibaba.druid.filter.FilterAdapter.statement_executeUpdate(FilterAdapter.java:2486)
    at com.alibaba.druid.filter.FilterChainImpl.statement_executeUpdate(FilterChainImpl.java:2375)
    at com.alibaba.druid.proxy.jdbc.StatementProxyImpl.executeUpdate(StatementProxyImpl.java:225)
    at com.alibaba.druid.pool.DruidPooledStatement.executeUpdate(DruidPooledStatement.java:169)
    at org.hibernate.tool.schema.internal.TargetDatabaseImpl.accept(TargetDatabaseImpl.java:56)
    ... 86 more


百思不得其解,请高手指教。

实体类代码:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
 
import org.springframework.stereotype.Component;
 
@Component
@Entity
@Table(name = "USER_INFO")
public class UserInfo implements java.io.Serializable{
 
    @Id
    @Column(name = "USER_ID")
    private String userId;
    @Column(name = "username")
    private String userName;
    @Column(name = "department1")
    private String buMen;
    @Column(name = "department2")
    private String keShi;
 
getter setter略
}
SQL Oracle 关系型数据库 MySQL Java 数据库连接 数据库 数据安全/隐私保护
分享到
取消 提交回答
全部回答(1)
  • a123456678
    2019-07-17 19:07:44

    你的两个数据源都是扫描这个包里面的东西,默认是走到了mysql里面,oracle实体和mysql实体包分开就没事了,不要扫描到同一包下面而又是不同的数据库实体

    0 0
数据库
使用钉钉扫一扫加入圈子
+ 订阅

分享数据库前沿,解构实战干货,推动数据库技术变革

推荐文章
相似问题
推荐课程