Announcement Announcement Module
Collapse
No announcement yet.
Spring MVC Application - Omitting setters in the Entity class causes Hibernate Except Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring MVC Application - Omitting setters in the Entity class causes Hibernate Except

    I am creating a Spring MVC application by following along with an example in a book I bought.

    The example defines an entity class - Contact (and I have put the code at the bottom of this post). I then create a service to access this table in the database. The tech stack is: JPA2, Spring Data JPA and Hibernate as the persistence service provider. When I add this application to STS's tc server, I get a Hibernate Exception (I have put the exception after the code for the Contact entity class). What am I doing wrong here?

    If I add a setter, the exception happens on the next property in the Contact class.
    If you need any other information from me, please let me know and I will add it to this thread. Thanks!

    Contact Entity Class
    ===============
    @Entity
    @Table(name="contact")
    public class Contact implements Serializable {

    private Long id;
    private int version;
    private String firstName;
    private String lastName;
    private DateTime birthDate;
    private String description;
    private byte[] photo;

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name="ID")
    public Long getId() {
    return id;
    }

    @Version
    @Column(name = "VERSION")
    public int getVersion() {
    return version;
    }

    @Column(name="FIRST_NAME")
    public String getFirstName() {
    return firstName;
    }

    @Column(name="LAST_NAME")
    public String getLastName() {
    return lastName;
    }

    @Column(name="BIRTH_DATE")
    @Type(type="org.joda.time.contrib.hibernate.Persis tentDateTime")
    @DateTimeFormat(iso=ISO.DATE)
    public DateTime getBirthDate() {
    return birthDate;
    }

    @Column(name="DESCRIPTION")
    public String getDescription() {
    return description;
    }

    @Basic(fetch=FetchType.LAZY)
    @Lob @Column(name="PHOTO")
    public byte[] getPhoto() {
    return photo;
    }

    @Transient
    public String getBirthDateString() {
    String birthDateString = "";
    if (birthDate != null) {
    birthDateString = org.joda.time.format.DateTimeFormat.forPattern("yy yy-MM-dd").print(birthDate);
    }
    return birthDateString;
    }

    //Setter methods omitted
    //TODO: Adding this setter because I am getting an exception [even though the book did not say so]
    //Caused by: org.hibernate.PropertyNotFoundException: Could not find a setter for property id in class com.arris.custom.domain.Contact
    //at org.hibernate.property.BasicPropertyAccessor.creat eSetter(BasicPropertyAccessor.java:262)
    @Id
    @Column(name="ID")
    public void setId(long i) {
    id=i;
    }

    public String toString() {
    return "Contact - Id: " + id + ", First name: " + firstName +
    ", Last name: " + lastName + ", Birthday: " + birthDate;
    }
    }

    Hibernate Stack Trace
    =================
    ERROR: org.springframework.web.context.ContextLoader - Context initialization failed
    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'org.springframework.dao.annotation.PersistenceExc eptionTranslationPostProcessor#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'emf' defined in class path resource [datasource-tx-jpa.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build EntityManagerFactory
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:527)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 94)
    at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:225)
    at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:291 )
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.context.support.AbstractApplic ationContext.registerBeanPostProcessors(AbstractAp plicationContext.java:728)
    at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:449)
    at org.springframework.web.context.ContextLoader.conf igureAndRefreshWebApplicationContext(ContextLoader .java:384)
    at org.springframework.web.context.ContextLoader.init WebApplicationContext(ContextLoader.java:283)
    at org.springframework.web.context.ContextLoaderListe ner.contextInitialized(ContextLoaderListener.java: 111)
    at org.apache.catalina.core.StandardContext.listenerS tart(StandardContext.java:4791)
    at org.apache.catalina.core.StandardContext.startInte rnal(StandardContext.java:5285)
    at org.apache.catalina.util.LifecycleBase.start(Lifec ycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInt ernal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(Co ntainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(Sta ndardHost.java:618)
    at org.apache.catalina.startup.HostConfig.deployDescr iptor(HostConfig.java:650)
    at org.apache.catalina.startup.HostConfig$DeployDescr iptor.run(HostConfig.java:1582)
    at java.util.concurrent.Executors$RunnableAdapter.cal l(Executors.java:441)
    at java.util.concurrent.FutureTask$Sync.innerRun(Futu reTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.jav a:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run Task(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
    Caused by: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'emf' defined in class path resource [datasource-tx-jpa.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build EntityManagerFactory
    at
    ... 24 more
    Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build EntityManagerFactory
    at org.hibernate.ejb.Ejb3Configuration.buildEntityMan agerFactory(Ejb3Configuration.java:915)
    at ... 39 more
    Caused by: org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
    at org.hibernate.tuple.entity.EntityTuplizerFactory.c onstructTuplizer(EntityTuplizerFactory.java:108)

    ... 44 more
    Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)

    ... 53 more
    Caused by: org.hibernate.PropertyNotFoundException: Could not find a setter for property id in class com.arris.custom.domain.Contact
    at org.hibernate.property.BasicPropertyAccessor.creat eSetter(BasicPropertyAccessor.java:262)
    at org.hibernate.property.BasicPropertyAccessor.getSe tter(BasicPropertyAccessor.java:255)
    at org.hibernate.mapping.Property.getSetter(Property. java:309)
    at org.hibernate.tuple.entity.PojoEntityTuplizer.buil dPropertySetter(PojoEntityTuplizer.java:304)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer. <init>(AbstractEntityTuplizer.java:137)
    at org.hibernate.tuple.entity.PojoEntityTuplizer.<ini t>(PojoEntityTuplizer.java:77)
    ... 58 more

  • #2
    Hey!

    I moved this question over to the web forum which seems to be better suited for this question.

    HTH,
    Martin

    Comment

    Working...
    X