2014-12-01 4 views
11

Ich würde bis db Erstellungsdatum und Uhrzeit beibehalten. Ich habe etwas Erfahrung mit Datum und Zeit Klassen, aber ich mag sie nicht.Mit Joda Zeit mit JPA/Hibernate und Frühling

Kürzlich habe ich begonnen, Joda Zeit zu verwenden. Ich muss sagen, dass die Bibliothek am besten mit Datum und Uhrzeit arbeiten kann.

Für jetzt möchte ich DateTime-Objekt von joda beibehalten.

Ich habe schon versucht:

@Type(type="org.joda.time.contrib.hibernate.PersistentDateTime") 
private DateTime creationDate; 

Aber es leider Arbeit dosnt. Hier ist Stack-Trace:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class com.derp.common.init.WebAppConfig: Invocation of init method failed; nested exception is org.hibernate.MappingException: Could not determine type for: org.joda.time.contrib.hibernate.PersistentDateTime 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1568) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:540) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1081) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1006) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:904) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:527) 
    ... 41 more 
Caused by: org.hibernate.MappingException: Could not determine type for: org.joda.time.contrib.hibernate.PersistentDateTime 
    at org.hibernate.cfg.annotations.SimpleValueBinder.fillSimpleValue(SimpleValueBinder.java:510) 
    at org.hibernate.cfg.SetSimpleValueTypeSecondPass.doSecondPass(SetSimpleValueTypeSecondPass.java:42) 
    at org.hibernate.cfg.Configuration.processSecondPassesOfType(Configuration.java:1472) 
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1420) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1930) 
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:372) 
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:453) 
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:438) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1627) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1564) 
    ... 51 more 
Caused by: org.hibernate.annotations.common.reflection.ClassLoadingException: Unable to load Class [org.joda.time.contrib.hibernate.PersistentDateTime] 
    at org.hibernate.annotations.common.util.StandardClassLoaderDelegateImpl.classForName(StandardClassLoaderDelegateImpl.java:60) 
    at org.hibernate.cfg.annotations.SimpleValueBinder.fillSimpleValue(SimpleValueBinder.java:491) 
    ... 61 more 
Caused by: java.lang.ClassNotFoundException: org.joda.time.contrib.hibernate.PersistentDateTime 
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1324) 
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1177) 
    at java.lang.Class.forName0(Native Method) 
    at java.lang.Class.forName(Class.java:344) 
    at org.hibernate.annotations.common.util.StandardClassLoaderDelegateImpl.classForName(StandardClassLoaderDelegateImpl.java:57) 
    ... 62 more 

Und mein Fragment von pom.xml:

<dependency> 
    <groupId>joda-time</groupId> 
    <artifactId>joda-time</artifactId> 
    <version>2.5</version> 
</dependency> 

Ich hoffe, dass jemand mir helfen.

---- ------ Edited

WebMvcConfigurerAdapter

package com.derp.common.init; 

import java.util.Properties; 

import javax.annotation.Resource; 
import javax.sql.DataSource; 

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.PropertySource; 
import org.springframework.core.env.Environment; 
import org.springframework.http.MediaType; 
import org.springframework.jdbc.datasource.DriverManagerDataSource; 
import org.springframework.orm.hibernate4.HibernateTransactionManager; 
import org.springframework.orm.hibernate4.LocalSessionFactoryBean; 
import org.springframework.transaction.annotation.EnableTransactionManagement; 
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer; 
import org.springframework.web.servlet.config.annotation.EnableWebMvc; 
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 

//import com.derp.common.wicketView.HomePage; 

@Configuration 
@ComponentScan("com.derp") 
@EnableWebMvc 
@EnableTransactionManagement 
@PropertySource("classpath:application.properties") 
public class WebAppConfig extends WebMvcConfigurerAdapter { 

    private static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver"; 
    private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password"; 
    private static final String PROPERTY_NAME_DATABASE_URL = "db.url"; 
    private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username"; 

    private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect"; 
    private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql"; 
    private static final String PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO = "hibernate.hbm2ddl.auto"; 
    private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN_SERVICES = "services.entitymanager.packages.to.scan"; 
    private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN_COMMON = "common.entitymanager.packages.to.scan"; 
    private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN_CMS = "cms.entitymanager.packages.to.scan"; 

    @Resource 
    private Environment env; 

    @Bean 
    public DataSource dataSource() { 
     DriverManagerDataSource dataSource = new DriverManagerDataSource(); 
     dataSource.setDriverClassName(env.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER)); 
     dataSource.setUrl(env.getRequiredProperty(PROPERTY_NAME_DATABASE_URL)); 
     dataSource.setUsername(env.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME)); 
     dataSource.setPassword(env.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD)); 
     return dataSource; 
    } 

    @Bean 
    public LocalSessionFactoryBean sessionFactory() { 
     LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean(); 
     sessionFactoryBean.setDataSource(dataSource()); 
     //sessionFactoryBean.setPackagesToScan(env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN)); 
     sessionFactoryBean.setPackagesToScan(new String[] { 
       env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN_SERVICES), 
       env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN_COMMON), 
       env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN_CMS) 
       }); 
     sessionFactoryBean.setHibernateProperties(hibProperties()); 
     return sessionFactoryBean; 
    } 

    private Properties hibProperties() { 
     Properties properties = new Properties(); 
     properties.put(PROPERTY_NAME_HIBERNATE_DIALECT, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT)); 
     properties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL)); 
     properties.put(PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO)); 
     return properties; 
    } 

    @Bean 
    public HibernateTransactionManager transactionManager() { 
     HibernateTransactionManager transactionManager = new HibernateTransactionManager(); 
     transactionManager.setSessionFactory(sessionFactory().getObject()); 
     return transactionManager; 
    } 


    @Override 
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) { 
     // Simple strategy: only path extension is taken into account 
     configurer.favorPathExtension(true). 
      ignoreAcceptHeader(true). 
      useJaf(false). 
      defaultContentType(MediaType.TEXT_HTML). 
      mediaType("html", MediaType.TEXT_HTML). 
      mediaType("xml", MediaType.APPLICATION_XML). 
      mediaType("json", MediaType.APPLICATION_JSON); 
    } 
    @Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) { 
     registry.addResourceHandler("/img/**").addResourceLocations("/WEB-INF/img/*"); 
     registry.addResourceHandler("/css/**").addResourceLocations("/WEB-INF/css/*"); 
     registry.addResourceHandler("/js/**").addResourceLocations("/WEB-INF/js/*"); 
     registry.addResourceHandler("/lib/**").addResourceLocations("/WEB-INF/lib/*"); 
    } 
} 

WebApplicationInitializer Klasse:

package com.derp.common.init; 

import javax.servlet.ServletContext; 
import javax.servlet.ServletException; 
import javax.servlet.ServletRegistration.Dynamic; 

import org.springframework.web.WebApplicationInitializer; 
import org.springframework.web.context.ContextLoaderListener; 
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; 
import org.springframework.web.filter.HiddenHttpMethodFilter; 
import org.springframework.web.servlet.DispatcherServlet; 

public class Initializer implements WebApplicationInitializer { 


    @Override 
    public void onStartup(ServletContext servletContext) throws ServletException { 
     AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); 
     ctx.register(WebAppConfig.class); 
     ctx.register(ThymeleafConfig.class); 
     servletContext.addListener(new ContextLoaderListener(ctx)); 

     ctx.setServletContext(servletContext); 

     Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx)); 
     servlet.addMapping("/"); 
     servlet.setAsyncSupported(true);  
     servlet.setLoadOnStartup(1); 
     // Allow to use Put and Delete method for REST architecture 
     registerHiddenFieldFilter(servletContext); 
    } 

    private void registerHiddenFieldFilter(ServletContext aContext) { 
     aContext.addFilter("hiddenHttpMethodFilter", new HiddenHttpMethodFilter()).addMappingForUrlPatterns(null ,true, "/*"); 
    } 
} 
+0

Nach dieser Frage können Sie es als eine Jpa-Eigenschaft hinzufügen: http://stackoverflow.com/questions/21130538/joda-time-and-hibernate-4 – bradleyfitz

+0

Die Methode, auf die Sie mich hingewiesen: 'HibernateJpaVendorAdapter' dosnt existieren in meinem Projekt, also weiß ich nicht wo ich es hinzufügen soll. Ich habe meinen Beitrag mit WebAppConfig und Initialazer Java Conf-Klassen aktualisiert. Kannst du dir das anschauen? – masterdany88

Antwort

13

können Sie Jadira für diesen Zweck verwenden:

hinzufügen dies zu Ihrem pom.xml:

<dependency> 
    <groupId>org.jadira.usertype</groupId> 
    <artifactId>usertype.extended</artifactId> 
    <version>3.2.0.GA</version> 
</dependency> 

Und diese in deinen Hibernate Eigenschaften (hibProperties Methode in der Config-Klasse):

properties.put("jadira.usertype.autoRegisterUserTypes", "true"); 

Nachdem Sie dies tun, können Sie die @Type Anmerkungen von Ihren Joda Eigenschaften entfernen und es sollte gut funktionieren (Arbeiten für mich sowohl für Joda und JSR-310).

+0

Ich habe Ihre Anweisungen gefolgt und jetzt habe ich in db Spalte Typ 'TIMESTAMP (23, 10)' Lets versuchen, einige Daten zu setzen: D – masterdany88

+0

Welche Datenbank verwenden Sie? Ich benutze Postgres und das erzeugt korrekt 'TIMESTAMP OHNE ZEITZONE' für mich. –

+0

Ich benutze h2db http://www.h2database.com/html/main.html – masterdany88

Verwandte Themen