-->
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.  [ 3 posts ] 
Author Message
 Post subject: Mapping a @Lob valued Map
PostPosted: Sun Jan 23, 2011 2:44 pm 
Newbie

Joined: Sun Jan 23, 2011 2:29 pm
Posts: 3
This is part of my model:

Code:
@Entity
public class Entry
{
   @Id @GeneratedValue
   private long identifier;

   @ElementCollection
   @Column(nullable = false)
   private Map<String, String> titles;
   
   @ElementCollection
   @Column(nullable = false)
   @Lob
   private Map<String, String> contents;

   // Getters and setters, other fields and methods
}


I use the @Lob annotation because the value of map "contents" can be large. Note that I do not care about how the key of map "contents" is mapped to the database. I just couldn't find a way to specify that the @Lob annotation should be applied only to the value of the map.

While Entry.titles is mapped to the database without problems, Entry.contents is not. No database table is created and MySQL/Hibernate complains that:

Quote:
Unsuccessful: create table myblog.Entry_contents (Entry_identifier bigint not null, contents longtext not null, contents_KEY longtext, primary key (Entry_identifier, contents_KEY)) type=InnoDB

BLOB/TEXT column 'contents_KEY' used in key specification without a key length


Any ideas are appreciated!


Top
 Profile  
 
 Post subject: Re: Mapping a @Lob valued Map
PostPosted: Fri Jan 28, 2011 4:40 am 
Newbie

Joined: Thu Jan 27, 2011 10:53 am
Posts: 12
Hi,
Instead of using a map of String,String, you can use an Set<MyClass>, MyClass being an Embedable.
i.e.

Code:
package com.afklm.jraf.tphibernate.businessobject;

import javax.persistence.Basic;
import javax.persistence.Embeddable;
import javax.persistence.Lob;

@Embeddable
public class Content {

   @Basic
   private String title;
   @Lob
   private String content;
   
}

Hope it helps


Top
 Profile  
 
 Post subject: Re: Mapping a @Lob valued Map
PostPosted: Fri Jan 28, 2011 7:09 am 
Newbie

Joined: Sun Jan 23, 2011 2:29 pm
Posts: 3
Thanks for your reply! I too believe your solution will work.

I also posted on stackoverflow.com and this is the answer I received. As you can see one of the suggested solutions is the same as yours:

Quote:
It's definitely a bug in Hibernate. JPA 2.0 specification clearly states that @Lob should be applied to the map value in this case:

The Lob annotation may be used in conjunction with the Basic annotation or with the ElementCollection[100] annotation when the element collection value is of basic type.
...

[100] If the element collection is a Map, this applies to the map value.

The obvious workarounds include defining column type with @MapKeyColumn(columnDefinition = "...") or using @Embeddable as a wrapper for values.

Also this bug seems to be unreported, feel free to report it: Hibernate JIRA.


Personally I preferred the first solution mentioned as it involved the least changes in my model:

Code:
@ElementCollection
@Column(nullable = false)
@Lob
@MapKeyColumn(columnDefinition = "varchar(255)") //TODO: This is a workaround for JPA-11
private Map<String, String> contents;


I also opened a bug: http://opensource.atlassian.com/project ... wse/JPA-11

Again thank you!

Regards,
Panayiotis


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