You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Alexander Saint Croix <sa...@gmail.com> on 2007/12/31 01:12:38 UTC

Annotation suggestions for Map field.

Hello,

I'm curious--for a simple Map field like the following:

private Map<String, String> m = new HashMap<String, String>();

What would be the best way to annotate this?  I'm getting odd errors
with a simple @OneToMany annotation.

Cheers, and once again--thank you guys for the amazing job in 2007--I
eagerly await what 2008 will bring.

Regards,
--
Alexander Saint Croix
.

Re: Annotation suggestions for Map field.

Posted by Alexander Saint Croix <sa...@gmail.com>.
Actually, I want to avoid proprietary annotations--standards are very
important for this project.  What I ended up with is shown here:

package org.eremite.corm.party;
>
> import org.eremite.corm.Archetype;
>
> import javax.persistence.Entity;
> import javax.persistence.GeneratedValue;
> import javax.persistence.Id;
>
> @Entity
> public class Property extends Archetype {
>
>    @Id
>    @GeneratedValue
>    private long ID;
>
>    public Property() {}
>
>    public Property(String name, String desc) {
>        setName(name);
>        setDescription(desc);
>    }
>
>    public long getID() {
>        return ID;
>    }
>
>    public void setID(long ID) {
>        this.ID = ID;
>    }
>
>    public String getKey() {
>        return getName();
>    }
>
>    public void setKey(String key) {
>        setName(key);
>    }
>
>    public String value() {
>        return getDescription();
>    }
>
>    public void setValue(String val) {
>        setDescription(val);
>    }
>
>    public boolean equals(Object that) {
>        if(that instanceof Property) {
>             return this.getKey().equals(((Property)that).getKey());
>        }
>        return false;
>    }
>
>    public int hashCode() {
>        return (getKey() + value()).hashCode();
>    }
>
>    public String toString() {
>        return getKey() + " = " + value();
>    }
> }
>

The Archetype class is show here:

package org.eremite.corm;
>
> import javax.persistence.Id;
> import javax.persistence.MappedSuperclass;
> import java.io.Serializable;
>
> @MappedSuperclass
> public class Archetype implements Serializable {
>
>     private String name = "";
>     private String description = "";
>
>     public Archetype() {}
>
>     public String getName(){
>         return name;
>     }
>
>     public void setName(String name) {
>         this.name = name;
>     }
>
>     public String getDescription(){
>         return description;
>     }
>
>     public void setDescription(String description) {
>         this.description = description;
>     }
> }
>

Then, I keep a Set of these Property objects with a simple
@OneToMany(cascade=CascadeType.ALL) annotation, and have some methods for
correctly adding and removing to and from the set.  Presto.  It's great,
because now I have a standardized Properties table in the database that I
can pore over, and I can use the properties like this in any of my other
classes.

Great to know about the proprietary annotation option, though--someone out
there will find it useful.

Cheers,
--
Alexander R. Saint Croix






On Dec 31, 2007 12:56 PM, Patrick Linskey <pl...@gmail.com> wrote:
> What about the proprietary @PersistentMap annotation? I'd guess that
> this will do exactly what you're looking for (table with three
> columns, one for the fk, one for the key, and one for the value).
>
> -Patrick
>
> On Dec 31, 2007 1:37 AM, Alexander Saint Croix
>
> <sa...@gmail.com> wrote:
> > In response to my own question:
> >
> > I found, buried near the bottom of the JPA spec, the section on the
> > @MapKey annotation, and think that I'd be better off implementing
> > another entity for this purpose rather than relying on the default
> > behavior of the java.util.Map.  In addition to a key, I need to map
> > TWO values, so it makes little sense to use a Map.
> >
> > Cheers,
> > --
> > Alex

Re: Annotation suggestions for Map field.

Posted by Patrick Linskey <pl...@gmail.com>.
What about the proprietary @PersistentMap annotation? I'd guess that
this will do exactly what you're looking for (table with three
columns, one for the fk, one for the key, and one for the value).

-Patrick

On Dec 31, 2007 1:37 AM, Alexander Saint Croix
<sa...@gmail.com> wrote:
> In response to my own question:
>
> I found, buried near the bottom of the JPA spec, the section on the
> @MapKey annotation, and think that I'd be better off implementing
> another entity for this purpose rather than relying on the default
> behavior of the java.util.Map.  In addition to a key, I need to map
> TWO values, so it makes little sense to use a Map.
>
> Cheers,
> --
> Alex
>
>
>
>
>
>
> On Dec 30, 2007 6:12 PM, Alexander Saint Croix
>
> <sa...@gmail.com> wrote:
> > Hello,
> >
> > I'm curious--for a simple Map field like the following:
> >
> > private Map<String, String> m = new HashMap<String, String>();
> >
> > What would be the best way to annotate this?  I'm getting odd errors
> > with a simple @OneToMany annotation.
> >
> > Cheers, and once again--thank you guys for the amazing job in 2007--I
> > eagerly await what 2008 will bring.
> >
> > Regards,
> > --
> > Alexander Saint Croix
> > .
> >
>



-- 
Patrick Linskey
202 669 5907

Re: Annotation suggestions for Map field.

Posted by Alexander Saint Croix <sa...@gmail.com>.
In response to my own question:

I found, buried near the bottom of the JPA spec, the section on the
@MapKey annotation, and think that I'd be better off implementing
another entity for this purpose rather than relying on the default
behavior of the java.util.Map.  In addition to a key, I need to map
TWO values, so it makes little sense to use a Map.

Cheers,
--
Alex






On Dec 30, 2007 6:12 PM, Alexander Saint Croix
<sa...@gmail.com> wrote:
> Hello,
>
> I'm curious--for a simple Map field like the following:
>
> private Map<String, String> m = new HashMap<String, String>();
>
> What would be the best way to annotate this?  I'm getting odd errors
> with a simple @OneToMany annotation.
>
> Cheers, and once again--thank you guys for the amazing job in 2007--I
> eagerly await what 2008 will bring.
>
> Regards,
> --
> Alexander Saint Croix
> .
>