Hi,
I am trying to simply use JodaTime in my model/Java App and persist it using hibernate but I end up getting the following exception whenever I try to run any of the tests. The project is a spring based service and I am trying to persist Joda DateTime.
Quote:
Caused by: org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister
at org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:185)
at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:135)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:385)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1769)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1840)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:247)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:373)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:358)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1541)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1479)
... 56 more
Caused by: org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:138)
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructDefaultTuplizer(EntityTuplizerFactory.java:188)
at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:341)
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:507)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:146)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:163)
... 65 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:135)
... 74 more
Caused by: org.hibernate.PropertyNotFoundException: Could not find a setter for property createdAt in class com.company.Merchant
at org.hibernate.property.BasicPropertyAccessor.createSetter(BasicPropertyAccessor.java:252)
at org.hibernate.property.BasicPropertyAccessor.getSetter(BasicPropertyAccessor.java:245)
at org.hibernate.mapping.Property.getSetter(Property.java:326)
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildPropertySetter(PojoEntityTuplizer.java:444)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:201)
at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:82)
... 79 more
Interesting bits of code and configuration
Pom.xml
I am using hibernate 4.2.2.Final
Quote:
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>0.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.jadira.usertype</groupId>
<artifactId>usertype.core</artifactId>
<version>3.1.0.CR10</version>
</dependency>
HBM
Quote:
<property name="createdAt" column="created_at" type="org.jadira.usertype.dateandtime.joda.PersistentDateTime"/>
<property name="lastUpdatedAt" column="last_updated_at" type="org.jadira.usertype.dateandtime.joda.PersistentDateTime"/>
Model for me is outside the service and has no interaction with Hibernate directly, obviously
Model's POM
Quote:
<!-- joda -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.3</version>
</dependency>
<!-- Jackson JSON Processor -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
jackson.version is 2.2.3
Model code with methods for handlind Joda DateTime members
Code:
// Declaration
private DateTime createdAt = new DateTime(DateTimeZone.UTC);
private DateTime lastUpdatedAt = new DateTime(DateTimeZone.UTC);;
// Getters
@JsonIgnore
public DateTime getCreatedAt() {
return this.createdAt;
}
@JsonProperty(CREATED_AT_STRING)
public String getCreatedAtISOString() {
if (this.createdAt != null) {
return this.createdAt.toDateTime(DateTimeZone.UTC).toString();
}
return null;
}
@JsonIgnore
public DateTime getLastUpdatedAt() {
return this.lastUpdatedAt;
}
@JsonProperty(LAST_UPDATED_AT_STRING)
public String getLastUpdatedAtISOString() {
if (this.lastUpdatedAt != null) {
return this.lastUpdatedAt.toDateTime(DateTimeZone.UTC).toString();
}
return null;
}
// Setters
@JsonSetter(CREATED_AT_STRING)
public Builder createdAt(DateTime createdAt) {
if (createdAt != null) {
this.createdAt = createdAt;
}
return this;
}
@JsonSetter(LAST_UPDATED_AT_STRING)
public Builder lastUpdatedAt(DateTime lastUpdatedAt) {
if (lastUpdatedAt != null) {
this.lastUpdatedAt = lastUpdatedAt;
}
return this;
}
The DB alter call is:
ALTER TABLE merchants ADD COLUMN created_at TIMESTAMP;
ALTER TABLE merchants ADD COLUMN last_updated_at TIMESTAMP;
I am very new to Hibernate and would really appreciate some help in this context.
Might be of interest that till before I tried to add JodaTime based time fields to the model, multiple other fields which were strings, uuids etc were all working.