-->
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.  [ 2 posts ] 
Author Message
 Post subject: @CollectionOfElements using Map and @Embeddable
PostPosted: Mon Jan 16, 2006 9:30 am 
Newbie

Joined: Mon Jan 16, 2006 9:09 am
Posts: 2
Location: London, UK
Hi, I was hoping someone might be able to shed some light on something for me. I'm migrating some code of mine to hibernate-annotations as a learning excercise. I'm trying to map a @CollectionOfElements using a Map field. I can get this working if the value type of the Map is annotated as an @Entity, but I believe I should be able to do it if it is @Embeddable as well. I can't find any direct examples, but the hibernate-annotations docs contain an example using a Set rather than a Map.

Hibernate version: 3.1

Mapping documents: hibernate.cfg.xml

Code:
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="show_sql">true</property>
        <property name="dialect">org.hibernate.dialect.HSQLDialect</property>
        <mapping class="scrabble.db.hibernate31.Tile"/>
        <mapping class="scrabble.db.hibernate31.TileSetImpl"/>
    </session-factory>
</hibernate-configuration>


Persistent Classes:

Code:
@Entity(access = AccessType.FIELD)
@Table(name = "TileSet")
class TileSetImpl implements TileSet, Serializable
{
    @Id(generate = GeneratorType.NONE)
    @Column(length = 16)
    private Locale locale;

    @CollectionOfElements
    @JoinTable(table = @Table(name="Tile"), joinColumns = @JoinColumn(name = "tileset_locale"))
    @MapKey(name = "letter")
    private Map<String, Tile> tiles;

    public Locale getLocale()
    {
        return locale;
    }
}


Code:
@Embeddable(access = AccessType.FIELD)
class Tile implements Serializable
{
    @Column(name = "tileset_locale", nullable = false, length = 16)
    private Locale tilesetLocale;

    @Column(nullable = false, length = 2)
    private String letter;

    @Column(nullable = false)
    private int frequency;

    @Column(nullable = false)
    private int score;

    public Locale getTilesetLocale()
    {
        return tilesetLocale;
    }

    public String getLetter()
    {
        return letter;
    }

    public int getFrequency()
    {
        return frequency;
    }

    public int getScore()
    {
        return score;
    }

    public boolean equals(Object o)
    {
        ...
    }

    public int hashCode()
    {
        ...
    }
}



Because the Tile class is completely subordinate to the TileSetImpl class (i.e. external code only interacts with the methods of the TileSet interface it seems logical (to me anyway) to have it as an @Embeddable rather than a full-blown @Entity.

I'm simply trying to run the hbm2ddl Ant task and get the error
Code:
org.hibernate.AnnotationException: Associated class not found: scrabble.db.hibernate31.Tile
.

As I say I can get this to work with the Tile class written as an @Entity with an inner PK class comprised of the tilesetLocale and letter fields.[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 16, 2006 10:17 am 
Newbie

Joined: Mon Jan 16, 2006 9:09 am
Posts: 2
Location: London, UK
Ah, apologies. I assume the issue is covered by this bug http://opensource.atlassian.com/projects/hibernate/browse/ANN-1?


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