-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: mapping error with postgres
PostPosted: Thu Jul 19, 2007 9:52 am 
Beginner
Beginner

Joined: Thu Jun 14, 2007 4:33 am
Posts: 39
Hi,

im using Hibernate 3 and Postgres 8.2

Im trying to map an existig table with a column of char(10).
Code:
create table foobar(... birthdate char(10) null, ...);



My Mappingfile looks like:
Code:
<property name="birthdate"
                          type="string"
                          >
                          <column name="birthdate" sql-type="character(10)"/>
                </property>

JavaCode:
Code:
public String getBirthdate() {
        return _birthdate;
    }

    public void setBirthdate(String birthdate) {
        this._birthdate = birthdate;
    }
private String _birthdate;


I get:
Code:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Wrong column type: birthdate, expected: character(10)
Caused by: org.hibernate.HibernateException: Wrong column type: birthdate, expected: character(10)
   at org.hibernate.mapping.Table.validateColumns(Table.java:261)
   at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1083)
   at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:116)
   at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:317)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)
   at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:822)
   at org.springframework.orm.hibernate3.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:748)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1201)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1171)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:425)
   at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
   at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:156)
   at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
   at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
   at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:284)
   at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
   at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:91)
   at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:75)
   at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:65)
   at Main.main(Main.java:16)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:585)
   at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)


when compiling. I've tried different sql-types (char(10), character(10), varchar, varchar(10) ) but nothing worked.


The strange thing is, that even if i use the schema-export of Hibernate, after exporting the validation fails with no changes in de source!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 20, 2007 3:54 am 
Beginner
Beginner

Joined: Thu Jun 14, 2007 4:33 am
Posts: 39
im still working on it. seams like my new mapping is working, but now i get a different exception.

Code:
<property name="birthdate"
                          type="character"
                          >
                          <column name="birthdate" length="10"/> 
                          </property>

Code:
public Character[] getBirthdate() {
        return _birthdate;
    }

    public void setBirthdate(Character[] birthdate) {
        this._birthdate = birthdate;
    }

Code:
org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of dao.impl.HibernateUbAccountDAOImpl.birthdate
   at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:104)
   at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:337)
   at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:200)
   at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3566)
   at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:129)
   at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:854)
   at org.hibernate.loader.Loader.doQuery(Loader.java:729)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
   at org.hibernate.loader.Loader.loadEntity(Loader.java:1860)
....
Caused by: java.lang.IllegalArgumentException: argument type mismatch
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
....


Whats wrong with my setter/getter?


Top
 Profile  
 
 Post subject: Strange class attribute
PostPosted: Fri Jul 20, 2007 8:54 am 
Newbie

Joined: Tue Jun 12, 2007 6:44 am
Posts: 13
Location: Brazil
Hi

I have checked the code of ur class and found sth really strange

Your attribute is a String named _birthdate. I think you should remove the underscore character from the attribute name so _bithdate would become birthdate.

Ur getter and setter is fine it follows java naming conventions:
Eg: get + Attribute name with first letter in upper
get + Birthdate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 20, 2007 9:36 am 
Beginner
Beginner

Joined: Thu Jun 14, 2007 4:33 am
Posts: 39
True indeed. But its not that easily to change that because of the code-conventions in our company. However this might be not the error causing the exception: the other fields are working fine ;)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 20, 2007 10:04 am 
Newbie

Joined: Tue Jun 12, 2007 6:44 am
Posts: 13
Location: Brazil
Try this:
<property name="_birthdate" column="birthdate"/>

You dont have to specify a the type you wish the attribute value will be mapped to. Hibernate guesses it and creates a column (Probably varchar).

If your attribute was date or time or Calendar then you would have sth like this
<property name="_birthdate" type="timestamp" column="birthdate"/>
<property name="_birthdate" type="date" column="birthdate"/>
or even
<property name="_birthdate" type="time" column="birthdate"/>

hope this helps.

Bye


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.