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.  [ 6 posts ] 
Author Message
 Post subject: @CollectionOfElements and HashMap trouble cont'd
PostPosted: Thu Nov 22, 2007 12:07 pm 
Newbie

Joined: Thu Nov 08, 2007 8:02 pm
Posts: 9
I'm sorry to be a bother, since I already asked a similar question here http://forum.hibernate.org/viewtopic.php?t=981118&highlight=collectionofelements, but I haven't solved that problem yet.

I noticed that I can map a HashMap to the MySQL database with Varchar(255) values if I use this mapping:

Code:
@CollectionOfElements
public Map<String, String> getSettings() {
      return settings;
}


The table will look like
Code:
+--------------+--------------+
| Field        | Type         |
+--------------+--------------+
| Collector_id | bigint(20)   |
| element      | varchar(255) |
| mapkey       | varchar(255) |
+--------------+--------------+


But I want to store more that 255 chars - at least for the value (I already noticed clobs aren't allowed as indices). So I tried

Code:
@CollectionOfElements
@AttributeOverride(name="element.value", column=@Column(columnDefinition="TEXT"))


Which crashed horribly, resulting in

Code:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in file [C:\Users\tag\workspace\xyz\conf\applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.PropertyNotFoundException: Could not find a setter for property bytes in class java.lang.String
Caused by: org.hibernate.PropertyNotFoundException: Could not find a setter for property bytes in class java.lang.String
   at org.hibernate.property.BasicPropertyAccessor.createSetter(BasicPropertyAccessor.java:216)
   at org.hibernate.property.BasicPropertyAccessor.getSetter(BasicPropertyAccessor.java:209)
   at org.hibernate.mapping.Property.getSetter(Property.java:277)
   at org.hibernate.tuple.component.PojoComponentTuplizer.buildSetter(PojoComponentTuplizer.java:137)
   at org.hibernate.tuple.component.AbstractComponentTuplizer.<init>(AbstractComponentTuplizer.java:44)
   at org.hibernate.tuple.component.PojoComponentTuplizer.<init>(PojoComponentTuplizer.java:38)
   at org.hibernate.tuple.component.ComponentEntityModeToTuplizerMapping.<init>(ComponentEntityModeToTuplizerMapping.java:52)
   at org.hibernate.tuple.component.ComponentMetamodel.<init>(ComponentMetamodel.java:50)
   at org.hibernate.mapping.Component.buildType(Component.java:152)
   at org.hibernate.mapping.Component.getType(Component.java:145)
   at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:253)
   at org.hibernate.mapping.Collection.validate(Collection.java:278)
   at org.hibernate.mapping.IndexedCollection.validate(IndexedCollection.java:67)
   at org.hibernate.cfg.Configuration.validate(Configuration.java:1106)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1287)
   at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:915)


Also setting the @Column(length=4096) didn't work and resulted in the same exception.

The only thing that worked was getting rid of the annotations and using xml. Like this:
Code:
      <map name="settings" >
         <key column="id" />
         <index column="keyValue" type="string" length="255"/>
         <element column="value" type="string" length="1024" />
      </map>


which got me an acceptable table:

Code:
mysql> desc  testentity_settings;
+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id       | bigint(20)   | NO   | PRI |         |       |
| value    | text         | YES  |     | NULL    |       |
| keyValue | varchar(255) | NO   | PRI |         |       |
+----------+--------------+------+-----+---------+-------+


But using XML is out of question due to the nature of the project I am working on. So the question is: How can I get sane Hibernate annotation mappings for what I am doing, without it blowing up in my face everytime I try to do map a pojo.

The Hibernate Manual is very sketchy on this subject, especially when dealing with Maps. I read the Annotation Manual, The Hibernate documentation and the relevant portions of Persistence with Hibernate and came up with basically nothing.

btw. I am using hibernate 3.2.5ga and annotations 3.3.0ga


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 23, 2007 6:07 am 
Newbie

Joined: Fri Oct 12, 2007 5:51 pm
Posts: 5
Can you try this:

@CollectionOfElements
@Column(name = "some_column_name", length = MAX_LENGTH)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 23, 2007 6:54 am 
Newbie

Joined: Thu Nov 08, 2007 8:02 pm
Posts: 9
If I do that the whole table fails on creation.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 23, 2007 8:16 am 
Newbie

Joined: Tue Apr 04, 2006 6:58 am
Posts: 4
Location: Tampere,Finland
Add @Lob


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 23, 2007 8:22 am 
Newbie

Joined: Fri Oct 12, 2007 5:51 pm
Posts: 5
This works for me:

@CollectionOfElements
@Column(name = "the_key")
@MapKey(columns={@Column(name="the_value",length=1024)})
private Map<String, String> xxx;

See: http://www.i-proving.ca/space/Technolog ... f+Elements


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 23, 2007 8:30 am 
Newbie

Joined: Thu Nov 08, 2007 8:02 pm
Posts: 9
If I assign a @Lob like this:

Code:
    @CollectionOfElements
    @Lob
    private Map<String, String> settings;


Table creation fails silently.

If I do this instead:

Code:
    @CollectionOfElements
    @Lob
    @AttributeOverride(name="elements.value", column=@Column(length=1024))
    private Map<String, String> settings;


I get an Exception:

Code:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [resources/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Repeated column in mapping for collection: com.ulysses.core.impl.collector.AbstractCollector.settings column: count
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1362)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:540)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:485)
   at java.security.AccessController.doPrivileged(Native Method)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:455)
   at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
   at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:169)
   at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
   at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:170)
   at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:407)
   at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:735)
   at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:369)
   at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:96)
   at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:44)
   at org.springframework.test.context.TestContext.buildApplicationContext(TestContext.java:198)
   at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:233)
   at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:126)
   at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:85)
   at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:231)
   at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:95)
   at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:139)
   at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
   at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
   at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
   at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
   at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
   at org.junit.internal.runners.CompositeRunner.runChildren(CompositeRunner.java:33)
   at org.junit.runners.Suite.access$000(Suite.java:26)
   at org.junit.runners.Suite$1.run(Suite.java:93)
   at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
   at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
   at org.junit.runners.Suite.run(Suite.java:91)
   at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
   at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: org.hibernate.MappingException: Repeated column in mapping for collection: com.ulysses.core.impl.collector.AbstractCollector.settings column: count
   at org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:306)
   at org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:329)
   at org.hibernate.mapping.Collection.validate(Collection.java:286)
   at org.hibernate.mapping.IndexedCollection.validate(IndexedCollection.java:67)
   at org.hibernate.cfg.Configuration.validate(Configuration.java:1106)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1287)
   at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:915)
   at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:753)
   at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:691)
   at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1390)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1359)
   ... 37 more



Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.