我正在努力将Oracle同义词映射到JPA实体中。

作为背景,我需要在tomcat 7服务器以及JBoss EAP 6.4上都使其成为可能。
在下面,您将找到我的持久性配置的摘要。


数据源查找(找到jndi,绑定数据源)


@Bean
public DataSource datasource() throws NamingException{
    Context initCtx = new InitialContext();
    Context envCtx = (Context) initCtx.lookup("java:comp/env");
    DataSource ds = (DataSource) envCtx.lookup("jdbc/PAMGODW_DS");

    return ds;
}



LocalSessionFactoryBean(似乎可以工作):


@Bean
public LocalSessionFactoryBean sessionFactory() throws NamingException, IOException{
    Properties props = new Properties();
    props.put("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
    props.put("hibernate.synonyms", "true");
    props.put("hibernate.connection.includeSynonyms", "true");


    LocalSessionFactoryBean f = new LocalSessionFactoryBean();
    f.setDataSource(datasource());
    f.setPackagesToScan(new String[]{"com.roche.nrimini.pam.domain.entity", "com.roche.nrimini.pam.domain.service", "com.roche.nrimini.pam.domain.repository"});
    f.setHibernateProperties(props);
    f.setJtaTransactionManager(jtaTransactionManager());
    f.setAnnotatedPackages(new String[]{"com.roche.nrimini.pam.domain.entity", "com.roche.nrimini.pam.domain.service", "com.roche.nrimini.pam.domain.repository"});
    f.afterPropertiesSet();
    System.out.println("LOCALSESSIONFACTORY BEAN");

    return f;}



JtaTransactionManager(似乎也不错):


@Bean
public TransactionManager jtaTransactionManager(){
    JtaTransactionManager manager = new JtaTransactionManager();

    return manager.getTransactionManager();
}



实体@Table(name =“ V_TABLE_SYN”)实际上指向Oracle同义词:


@Entity
@Table(name = "V_TABLE_SYN")
public class SomeSynonym implements Serializable {

// @Id and @Column mappings here


当我尝试查询实体对象时得到的是ORA-00942表或视图不存在。

在JBoss环境中,standalone.xml中的数据源配置如下所示:

<datasource jta="false" jndi-name="java:jboss/PAMGODW_DS" pool-name="PAMGODW_DS" enabled="true" use-ccm="false">
                <connection-url>jdbc:oracle:thin:@someserver/some_service</connection-url>
                <driver-class>oracle.jdbc.OracleDriver</driver-class>
                <connection-property name="includeSynonyms">
                    true
                </connection-property>
                <driver>oracle</driver>
                <pool>
                    <min-pool-size>0</min-pool-size>
                    <max-pool-size>10</max-pool-size>
                </pool>
                <security>
                    <user-name>some_user</user-name>
                    <password>a_pass</password>
                </security>
                <validation>
                    <validate-on-match>false</validate-on-match>
                    <background-validation>false</background-validation>
                </validation>
                <statement>
                    <share-prepared-statements>false</share-prepared-statements>
                </statement>
            </datasource>


在Tomcat中,我得到了:

<Resource name="jdbc/SOME_DS" auth="Container"
          type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
          url="jdbc:oracle:thin:@some_server/some_service"
          username="some_user" password="a_pass" maxActive="20" maxIdle="10"
          maxWait="-1">


我听说过并了解了include.synonyms属性,但是它似乎不起作用,在Tomcat env中,我不知道如何设置它。

请帮助,也许你们中有些人也有类似的问题。

最佳答案

我设法使它起作用。也许有人会发现它有用。似乎除了无法验证oracle同义词外,还必须设置hibernate.hbm2ddl.auto属性。如您在上一篇文章中所见,我根本没有设置此属性。干杯!

07-25 20:29