Hibernate properties przy konfiguracji połączenia w tomcacie

0

Cześć

Piszę swój projekt webowy w Javie i mam problem z obsłużeniem dużych liter w nazwach kolumn w bazie PostgreSQL, te podane do hibernate jako duże są konwertowane przez bazę do małych liter, bo hibernate nie opakowuje ich w cudzysłowy. Znalazłem rozwiązanie żeby napisać sobie taki MetaDataDialect, który wygląda tak:

import org.hibernate.cfg.reveng.dialect.JDBCMetaDataDialect;

public class PostgreSQLMetaDialect extends JDBCMetaDataDialect {

    @Override
    public boolean needQuote(String name) {
        return name != null && !name.equals(name.toLowerCase()) || super.needQuote(name);
    }
} 

i później podłączyc go do hibernate properties w taki sposób:

hibernatetool.metadatadialect=com.corriel.PostgreSQLMetaDialect

W związku z samym podłączeniem tego mam taki problem, że nie wiem gdzie to wpiąć, używam JPA więc nie mam czegoś takiego jak hibernate.cfg.xml i połączenie konfiguruję w pliku context.xml w konfiguracji tomcata w taki sposób:

 <Context>
    <Resource name="jdbc/postgres" auth="Container"
              type="javax.sql.DataSource" driverClassName="org.postgresql.Driver"
              url="jdbc:postgresql://localhost/my-database"
              username="*" password="*" maxActive="20" maxIdle="10"
              maxWait="-1"/>
</Context>

więc nie mam też pliku persistence.xml. Cała konfigracja bazy wygląda tak:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <jee:jndi-lookup id="dataSource" jndi-name="jdbc/postgres"/>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
        <property name="packagesToScan" value="com.corriel.users.entity, com.corriel.budget.entity" />
        <property name="jpaProperties">
            <value>
                hibernatetool.metadatadialect=com.corriel.PostgreSQLMetaDialect
            </value>
        </property>
    </bean>

    <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="database" value="POSTGRESQL" />
        <property name="showSql" value="true" />
        <property name="generateDdl" value="false" />
        <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQL9Dialect" />
    </bean>

    <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
        <property name="jpaDialect" ref="jpaDialect" />
    </bean>

    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

    <tx:annotation-driven />

</beans> 

, gdzie próbowałem wpinać tego propertiesa do jpaProperties w entityManagerFactory, ale to chyba nie jest to bo nie działa.

Chciałbym zapytać w jaki sposób podłącza się hibernate properties do takiej konfiguracji i czy mój sposób ma prawo zadziałać, z drugiej strony może macie jakieś inne rozwiązanie na ten problem?

Edit:
Zauważyłem błąd w nazwie pakietu mojej klasy w propertiesach hibernata, ale po poprawieniu i tak nie działa

0

A nie można po prostu nazwać ich z małej? :P Nie wiem czy to zadziała na 100% ale może spróbuj poniższe:

If you have to quote all SQL identifiers, create an orm.xml file and add the setting <delimited-identifiers /> to its <persistence-unit-defaults>

orm.xml:

<entity-mappings version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.or /xml/ns/persistence/orm http://xmlns.jcp.org/xml/ns/persistence/orm_2_1.xsd">
   <persistence-unit-metadata>
      <persistence-unit-defaults>
         <delimited-identifiers/>
      </persistence-unit-defaults>
   </persistence-unit-metadata>
</entity-mappings>

The JPA provider automatically picks up this descriptor if you place it in a METAINF/orm.xml file on the classpath of the persistence unit. If you prefer to use a different name or several files, you’ll have to change the configuration of the persistence unit in your META-INF/persistence.xml

0

Niestety nie udało mi się zastosować Twojego rozwiązania tak żeby działało, ale natchnęło to mnie żeby jeszcze trochę poszukać w tym temacie i znalazłem coś takiego:

 <property name="jpaProperties">
            <value>hibernate.globally_quoted_identifiers=true</value>
        </property>

Przez co hibernate ładnie opakowuje wszystkie nazwy kolumn w cudzysłowy.

Co do nazywania wszystkich kolumn małymi literami to jest trochę denerwujące, bo w momencie gdy chciałbym skorzystać z automatycznego nazywania kolumn przez hibernate to wychodzą takie rzeczy jak userroleid co jest mało czytelne.

Mimo wszystko dzięki za pomoc.

1 użytkowników online, w tym zalogowanych: 0, gości: 1