« Return to Thread: Can I persist a Map containing a List as Value

Re: Can I persist a Map containing a List as Value

by tware :: Rate this Message:

Reply to Author | View in Thread

You will have to map your "map" element as a normal OneToMany - rather than as a
BasicMap.  The key is that you it actually takes two levels go get your mapping
to look the way you want.

i.e.

- TestEntity.map is a OneToMany with a MapKey to TestHolder
- TestHolder has a ManyToOne backpointer to TestEntity
- TestHolder also contains a BasicCollection that represents your list
- it is then possible to write business methods in TestEntity that can get the
list for an item in "map" without exposing the API user to the fact that
TestHolder exists.

-Tom

adi@... wrote:

> Hi
>
> I tryed this with the following code:
>
> @Entity
> public class TestEntity {
>     @Id @GeneratedValue private int id;
>
>     @OneToMany(cascade = { CascadeType.ALL })
>     @BasicMap(fetch = FetchType.EAGER, keyColumn = @Column(name = "key"),
>             valueColumn = @Column(name = "value"))
>     @CollectionTable(name = "map")
>     private Map<String, TestHolder> map;
>
>     public void setMap(Map<String, TestHolder> map) {
>         this.map = map;
>     }
>
>     public Map<String, TestHolder> getMap() {
>         return map;
>     }
> }
>
> @Entity
> public class TestHolder {
>     @Id @GeneratedValue private int id;
>
>     private String value;
>
>     public void setValue(String value) {
>         this.value = value;
>     }
>
>     public String getValue() {
>         return value;
>     }
> }
>
> private void test() {
>     factory =
> Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
>     EntityManager em = factory.createEntityManager();
>
>     em.getTransaction().begin();
>
>     TestEntity testEntity = new TestEntity();
>     Map<String, TestHolder> map = new HashMap<String, TestHolder>();
>     TestHolder value = new TestHolder("value");
>     map.put("key", value );
>     testEntity.setMap(map );
>     em.persist(testEntity);
>
>     em.getTransaction().commit();
>     em.close();
> }
>
> Definition of table map:
> CREATE TABLE MAP
> (
>   ID     NUMBER(10)                             NOT NULL,
>   VALUE  VARCHAR2(255 BYTE),
>   KEY    VARCHAR2(255 BYTE)
> )
>
> Then I get the error message:
> Internal Exception: java.sql.SQLException: Ungültiger Spaltentyp
> Error Code: 17004
> Call: INSERT INTO map (ID, value, key) VALUES (?, ?, ?)
>     bind => [1, bom.TestHolder@e34726, key]
>
> How can I store the object as entity?
>
> Thanks a lot!
>
> Regards Adrian
>
>
>
>
> Zitat von Tom Ware <tom.ware@...>:
>
>> You will likely need an extra object to do this.  Something like:
>>
>> private Map<String, ListHolder> instructions;
>>
>> @Entity
>> public class ListHolder{
>>    @id
>>    private int id;
>>
>>    private List instructions;
>>
>> ...
>> }
>>
>> It should be pretty easy to write some business methods that make this
>> mapping
>> transparent to the users of the API.
>>
>> -Tom
>>
>> adi@... wrote:
>>> Hi
>>>
>>> I persisted a map like this:
>>>     private Map<String, String> instructions;
>>>
>>> How can I persist a map that contains a List as value?
>>>     private Map<String, List<String>> instructions;
>>>
>>> Thanks a lot!
>>>
>>> Regards Adrian
>>>
>>> _______________________________________________
>>> eclipselink-users mailing list
>>> eclipselink-users@...
>>> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>> _______________________________________________
>> eclipselink-users mailing list
>> eclipselink-users@...
>> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>>
>
>
>
> _______________________________________________
> eclipselink-users mailing list
> eclipselink-users@...
> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
_______________________________________________
eclipselink-users mailing list
eclipselink-users@...
https://dev.eclipse.org/mailman/listinfo/eclipselink-users

 « Return to Thread: Can I persist a Map containing a List as Value