Joda Time和hibernate 4

时间:2014-01-15 06:32:07

标签: spring hibernate jpa jodatime spring-data

我正在使用hibernate 4和joda时间以及spring数据jpa。 Spring数据提供注释

@CreadedOn
@LastModifiedOn

我正在尝试使用这两个注释.Below是我的pojo的快照

@Entity
@Table(name="restaurant")
@Audited
public class Restaurant {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

private String restaurantName;

@CreatedDate
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
 // @Type(type = "org.jadira.usertype.dateandtime.threetenbp.PersistentDateTime")
private DateTime createdOn;

@LastModifiedDate
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
 // @Type(type = "org.jadira.usertype.dateandtime.threetenbp.PersistentDateTime")
private DateTime modifiedOn;
}

根据提供的说明user types jodatime,我已使用@Type注释我的属性。但仍然没有运气。每次我使用@Type注释并部署它我的错误服务器

  

SEVERE:错误listenerStart

完整堆栈跟踪在

之下
    Jan 15, 2014 11:40:00 AM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive G:\apache-tomcat-7.0.47\webapps\base-spr
ing-data-jpa.war
Jan 15, 2014 11:40:00 AM org.apache.catalina.loader.WebappClassLoader validateJa
rFile
INFO: validateJarFile(G:\apache-tomcat-7.0.47\webapps\base-spring-data-jpa\WEB-I
NF\lib\javax.servlet-api-3.1.0.jar) - jar not loaded. See Servlet Spec 2.3, sect
ion 9.7.2. Offending class: javax/servlet/Servlet.class
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further detail
s.
Jan 15, 2014 11:40:05 AM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
Jan 15, 2014 11:40:05 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.7.Final}
Jan 15, 2014 11:40:05 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jan 15, 2014 11:40:05 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jan 15, 2014 11:40:05 AM org.hibernate.ejb.Ejb3Configuration configure
INFO: HHH000204: Processing PersistenceUnitInfo [
        name: default
        ...]
Jan 15, 2014 11:40:05 AM org.hibernate.service.jdbc.connections.internal.Connect
ionProviderInitiator instantiateExplicitConnectionProvider
INFO: HHH000130: Instantiating explicit connection provider: org.hibernate.ejb.c
onnection.InjectedDataSourceConnectionProvider
Jan 15, 2014 11:40:06 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Jan 15, 2014 11:40:06 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error listenerStart
Jan 15, 2014 11:40:06 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/base-spring-data-jpa] startup failed due to previous errors
Jan 15, 2014 11:40:06 AM org.apache.catalina.loader.WebappClassLoader clearRefer
encesJdbc
SEVERE: The web application [/base-spring-data-jpa] registered the JDBC driver [
com.mysql.jdbc.Driver] but failed to unregister it when the web application was
stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistere
d.
Jan 15, 2014 11:40:06 AM org.apache.catalina.loader.WebappClassLoader clearRefer
encesThreads
SEVERE: The web application [/base-spring-data-jpa] appears to have started a th
read named [Abandoned connection cleanup thread] but has failed to stop it. This
 is very likely to create a memory leak.
Jan 15, 2014 11:40:06 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory G:\apache-tomcat-7.0.47\webapps\docs
Jan 15, 2014 11:40:06 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory G:\apache-tomcat-7.0.47\webapps\exampl
es
Jan 15, 2014 11:40:06 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory G:\apache-tomcat-7.0.47\webapps\host-m
anager
Jan 15, 2014 11:40:07 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory G:\apache-tomcat-7.0.47\webapps\manage
r
Jan 15, 2014 11:40:07 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory G:\apache-tomcat-7.0.47\webapps\ROOT
Jan 15, 2014 11:40:07 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Jan 15, 2014 11:40:07 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-apr-8009"]
Jan 15, 2014 11:40:07 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 6648 ms

从昨天开始,我正试图让这两项工作无法做到。任何人都可以告诉我如何解决这个问题。下面是我的pom依赖和存储库

 <dependencies>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
</dependency>

   <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.2.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>1.1.0.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.0.1.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.2.7.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.2.7.Final</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.27</version>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>1.5.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
</dependency>
<dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>3.2.5.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-envers</artifactId>
    <version>4.2.7.Final</version>
</dependency>
<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.3</version>
</dependency>

      

    <repository>
        <id>spring-libs-snapshot</id>
        <url>http://repo.spring.io/libs-snapshot</url>
    </repository>

</repositories>

2 个答案:

答案 0 :(得分:20)

您不需要使用@Type注释。只需添加以下jpa属性:

entityManagerFactory.getJpaPropertyMap().put("jadira.usertype.autoRegisterUserTypes", "true");

另外我认为你错过了jadira dependecy:

<dependency>
  <groupId>org.jadira.usertype</groupId>
  <artifactId>usertype.core</artifactId>
  <version>3.1.0.CR10</version>
</dependency>

答案 1 :(得分:2)

如果你使用基于注释的配置的gradle和spring boot,你可以使用@jakub-kubrynski建议

<强>的build.gradle

dependencies {
    compile 'org.jadira.usertype:usertype.core:3.2.0.GA'
}

<强> application.properties

#Hibernate config
spring.jpa.properties.jadira.usertype.autoRegisterUserTypes=true