2017-10-06 9 views
0

Je travaille une série d'applications de tutoriel pour passer en revue différentes technologies Java EE. Je suis différents tutoriels et plus ou moins s'y tenir. Récemment, j'ai commencé à mettre en place une simple webapp Spring CRUD pour stocker, trouver, modifier et supprimer des employés dans une table Employee DB. J'ai déjà complété une autre application très similaire, qui n'utilisait que Java et Hibernate, et qui visait à atteindre la même fonctionnalité. Cette application a bien fonctionné, j'ai donc décidé de copier les paramètres de connexion à la base de données de cette ancienne application vers la nouvelle version Spring. Le problème est que le bean de Spring DriverManagerDataSource ne semble pas accepter le réglage des mêmes propriétés que la configuration Hibernate d'origine, par exemple "hibernate.hbm2ddl.auto", que je veux pour effacer facilement la base de données au démarrage, ou "defaultSchema", que Postgres requiert pour une raison quelconque, donc je suis bloqué pour configurer la connexion DB.Comment définir certaines propriétés de DriverManagerDataSource de Spring?

Comment puis-je faire en sorte que Spring accepte ces propriétés de la même manière que Hibernate dans l'ancienne application, et pour montrer le même comportement? Pourquoi le bean n'accepte-t-il pas ces propriétés particulières d'une manière prévisible et sensible, comme c'est le cas pour d'autres propriétés telles que "url" ou "password"? Suis-je même supposé les mettre en place, n'y at-il pas un autre mécanisme au printemps qui s'occupe de la fonctionnalité que je veux des propriétés?

Old config app:

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC 
     "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
    <session-factory> 
     <!-- Database connection settings --> 
     <property name="connection.driver_class">org.postgresql.Driver</property> 
     <property name="connection.url">jdbc:postgresql://localhost:5432/test2</property> 
     <property name="connection.username">postgres</property> 
     <property name="connection.password">postgres</property> 

     <property name="hibernate.default_schema">public</property> 
     <property name="show_sql">true</property> 
     <property name="use_sql_comments">true</property> 
     <property name="hibernate.hbm2ddl.auto">create</property> 

     <mapping class="cz.bsc.hibernatetest.hibernatetutorial.domain.Book" /> 
     <mapping class="cz.bsc.hibernatetest.hibernatetutorial.domain.Author" /> 

    </session-factory> 
</hibernate-configuration> 

Une partie de la configuration Spring pris du tutoriel que je tente de modifier comme décrit ci-dessus:

printemps -servlet.xml

<beans...> 
    <bean id="ds" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="org.postgresql.Driver"></property> 
     <property name="url" value="jdbc:postgresql://localhost:5432/test2"></property> 
     <property name="username" value="postgres"></property> 
     <property name="password" value="postgres"></property> 

     <property name="spring.jpa.hibernate.defaultSchema" value="public"></property> 
     <property name="spring.jpa.hibernate.show_sql" value="true"></property> 
     <property name="spring.jpa.hibernate.use_sql_comments" value="true"></property> 
     <property name="spring.jpa.hibernate.hbm2ddl.auto" value="create"></property> 
    </bean> 
</beans> 

Message d'erreur complet renvoyé par mon application dans sa forme actuelle. Je présume que cela indique que j'essaie de définir une propriété d'une manière qui n'est pas prévue, donc une erreur de syntaxe.

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'empController': Unsatisfied dependency expressed through field 'dao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dao' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Cannot resolve reference to bean 'jt' while setting bean property 'template'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jt' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Cannot resolve reference to bean 'ds' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ds' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'spring.jpa.hibernate.defaultSchema' of bean class [org.springframework.jdbc.datasource.DriverManagerDataSource]: Nested property in path 'spring.jpa.hibernate.defaultSchema' does not exist; nested exception is org.springframework.beans.NotReadablePropertyException: Invalid property 'spring' of bean class [org.springframework.jdbc.datasource.DriverManagerDataSource]: Bean property 'spring' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter? 
+0

Spring a soutenu JPA et Hibernate depuis le début. Vous devriez faire plus de recherches. Jetez un oeil à Spring Boot et Spring Data si vous souhaitez être à jour. – duffymo

+0

Merci, je vais jeter un coup d'oeil à ceux-ci et voir si je peux trouver quelque chose pour me signaler la manière d'obtenir la fonctionnalité que je veux. – Sargon1

+0

https://spring.io/guides/gs/accessing-data-jpa/ – duffymo

Répondre

1

Les propriétés de mise en veille prolongée ne font pas partie de la définition de source de données. Il devrait être défini sous session-factory bean.

par exemple:

<beans> 
    <bean id="ds" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="org.postgresql.Driver"></property> 
     <property name="url" value="jdbc:postgresql://localhost:5432/test2"></property> 
     <property name="username" value="postgres"></property> 
     <property name="password" value="postgres"></property> 
    </bean> 


    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 

     <property name="dataSource" ref bean="ds" /> 
       <property name="packagesToScan" value="db entities package name" /> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.defaultSchema">public</prop> 
       <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop> 
       <prop key="hibernate.show_sql">true</prop> 
       <prop key="hibernate.use_sql_comments">true</prop> 
       <prop key="hibernate.hbm2ddl.auto">create</prop> 
      </props> 
     </property> 
    </bean> 

    <bean id="transactionManager" 
     class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 
</beans>