2012-04-17 26 views
6

Tôi gặp phải lỗi sau khi cố chuyển các giá trị từ tệp thuộc tính vào Mùa xuân, để tôi không phải cung cấp trực tiếp chúng trong hibernate.cfg.xml. Có cách tiếp cận nào tốt hơn (và chính xác) không? Tôi biết rằng các tập tin thuộc tính đang được tham chiếu bởi vì nếu tôi đưa vào mật khẩu không hợp lệ, nó không thành công trên đó. Tôi rất biết ơn vì sự giúp đỡ nào.Làm cách nào để chuyển jdbc.properties sang Spring/Hibernate?

WARNING: No connection properties specified - the user must supply JDBC connections 
Exception in thread "main" org.hibernate.HibernateException: Hibernate Dialect must be explicitly set 
    at org.hibernate.dialect.DialectFactory.determineDialect(DialectFactory.java:57) 
    at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:39) 
    at org.hibernate.cfg.SettingsFactory.determineDialect(SettingsFactory.java:426) 

Đây là applicationContext.xml:

<context:component-scan base-package="cmsutil"/> 
    <context:property-placeholder location="jdbc.properties"/> 

    <bean id="dataSource" 
      class="org.springframework.jdbc.datasource.DriverManagerDataSource" 
      p:driverClassName="${jdbc.driverClassName}" 
      p:url="${jdbc.url}" 
      p:username="${jdbc.username}" 
      p:password="${jdbc.password}"/> 

    <bean id="sessionFactory" 
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" 
      p:dataSource-ref="dataSource" 
      p:configurationClass="org.hibernate.cfg.AnnotationConfiguration" 
      p:packagesToScan="cmsutil.*"> 

     <property name="exposeTransactionAwareSessionFactory" value="false" /> 

     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
       <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
       <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> 
       <prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop> 
      </props> 
     </property> 

    </bean> 

    <bean id="txnManager" 
      class="org.springframework.orm.hibernate3.HibernateTransactionManager" 
      p:sessionFactory-ref="sessionFactory"/> 

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

Đây chính là file hibernate.cfg.xml tôi:

<hibernate-configuration> 
    <session-factory> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
    <mapping resource="cmsutil.entity/contentcomponent.hbm.xml"/> 
    </session-factory> 
</hibernate-configuration> 

mỗi Ramesh của yêu cầu - web.xml

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/applicationContext.xml</param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>2</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>*.htm</url-pattern> 
    </servlet-mapping> 
    <session-config> 
     <session-timeout> 
      30 
     </session-timeout> 
    </session-config> 
    <welcome-file-list> 
     <welcome-file>redirect.jsp</welcome-file> 
    </welcome-file-list> 
</web-app> 

Đối với Ramesh, đây là jdbc.propertie của tôi s, nằm trong thư mục gốc (/src/java/jdbc.properties)

jdbc.driverClassName=com.mysql.jdbc.Driver 
jdbc.url=jdbc:mysql://xx:3306/x 
jdbc.username=y 
jdbc.password=z 


hibernate.dialect=org.hibernate.dialect.MySQLDialect 
hibernate.show_sql=true 
hibernate.format_sql=false 
hibernate.generate_statistics=false 

Trả lời

8

thêm đậu này trong bản cập nhật applicationContext.xml

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="location"><value>classpath:jdbc.properties</value></property> 
    </bean> 

này trong web.xml thay vì nghe

<servlet> 
    <servlet-name>context</servlet-name> 
    <servlet-class> 
     org.springframework.web.context.ContextLoaderServlet 
     </servlet-class> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 
+0

Cảm ơn đề xuất. Đã thử rằng, nhưng có lỗi tương tự. Rất bực bội. Điều này phải đơn giản hơn tôi làm. – EdgeCase

+0

bạn có thể cập nhật web.xml tại đây –

+0

Đã thêm web.xml theo yêu cầu của bạn hay không. Cảm ơn. – EdgeCase

0

Tôi gặp vấn đề tương tự nhưng đó là kết quả của một lỗi trong mã java của tôi. Yêu cầu gọi số configure() trên số Configuration trước khi gọi số buildSessionFactory().

Các javadoc cho configure() phương pháp:

Sử dụng ánh xạ và các tài sản quy định tại một nguồn tài nguyên ứng dụng tên hibernate.cfg.xml.

Mã bị hỏng:

public static void main(String[] args) { 
     Configuration cfg = new Configuration(); 

     factory = cfg.buildSessionFactory(); // missing the configure() call 

     Session s = factory.openSession(); 
     Transaction tx = null; 
     try { 
      tx = s.beginTransaction(); 

Giải pháp:

public static void main(String[] args) { 
     Configuration cfg = new Configuration(); 

     factory = cfg.configure().buildSessionFactory(); 

     Session s = factory.openSession(); 
     Transaction tx = null; 
     try { 
      tx = s.beginTransaction(); 

Lưu ý: Tôi đã không sử dụng một cấu hình Spring.

Các vấn đề liên quan