Моя цель - запустить приложение spring + hibernate в базе данных Oracle XE и MySQL.Я не хочу менять свою модель Entity, я просто хочу изменить соединение с базой данных в XML, и оно должно работать как на MySQL, так и на Oracle.
MySql Config в xml:
<bean id="entityManagerFactoryBean"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
...
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.id.new_generator_mappings">false</prop>
</props>
</property>
...
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactoryBean" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/my_mysql_db?useSSL=false" />
<property name="username" value="root" />
<property name="password" value="root123" />
</bean>
Конфигурация Oracle в xml
<bean id="entityManagerFactoryBean"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
...
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</prop>
<prop key="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</prop>
<prop key="hibernate.connection.username">mdsinstance</prop>
<prop key="hibernate.connection.password">admin123</prop>
<prop key="hibernate.default_schema">mdsinstance</prop>
<prop key="hibernate.id.new_generator_mappings">false</prop>
</props>
</property>
...
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactoryBean" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
<property name="username" value="mdsinstance" />
<property name="password" value="admin123" />
</bean>
Hibernate Enity pojo (для mysql и oracle):
Entity
@Table(name = "t_customer")
public class Customer implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "customer_id")
private int customerId;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "customer_creation_date")
private Date customerCreationDate;
@Column(name = "customer_color_code")
private String customerColorCode;
@Lob //if used it give error in mysql as it takes longtext instead of text
@Column(name = "customer_responsible_user",columnDefinition = "TEXT")
private String customerResponsibleUser;
@Column(name = "customer_is_active",columnDefinition = "TINYINT") // gives error for oracle as tinyint not supportrd in oracle
private String customerIsActive;
@Column(name = "customer_setting",columnDefinition = "JSON") //gives error as json not supported in oracle
private String customerSetting;
}
Как вы видите, моя модель сущности одинакова как для базы данных oracle, так и для базы данных mysql. Если я использую @ Lob , то она выдаст ошибку в mysql "ожидает длинный текст, но найденный текст" как в таблице mysqlтип столбца - текст. Аналогично, как управлять mysql типа tinyint и json в oracle с той же сущностью pojo.