-->
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: Simple java.util.Map-relation
PostPosted: Tue Oct 27, 2009 1:01 pm 
Newbie

Joined: Tue Oct 27, 2009 12:20 pm
Posts: 4
Hi everyone.

I am new to this forum and also to hibernate, but I already was able to answer a lot of my questions which came up, while I experimented with this framework.

One question I couldn't found the answer for is about a simple Map<Integer,CustomClass>.. I will post my code here:

Code:
@Entity
public class Parent implements Serializable{

    @Id
    @GeneratedValue
    private Long id;

    @OneToMany(cascade=CascadeType.ALL)
    @Cascade ({org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
    @MapKey(name="parent_ref")
    private Map<Integer, Child> fvalues = new HashMap<Integer, Child>();

    public void setChild(int ref, Child child) {
        fvalues.put(ref, child);
    }
}


Code:
@Entity
public class Child implements Serializable {
    @Id
    @GeneratedValue
    private Long id;

    public Integer parent_ref;
}


So all I want to do is put Child in a map, which belongs to Parent. The code compiles and tomcat does not give any error, the database is being made without complaints. But when I do

Code:
Parent p = new Parent();
Child c = new Child();
p.setChild(5,c);
em.persist(p);


it does only put the value of the map in the database, not the key. I tried all kinds of combinations of annotations but so far no success. The childs show up in the database but the key-fields are always 0. In my case it should be 5 (from p.setChild(5,c);). Any ideas ? :S

Thanks to everyone with a suggestion in advance!

Greetz,
HellCode

EDIT: code corrections


Top
 Profile  
 
 Post subject: Re: Simple java.util.Map-relation
PostPosted: Wed Oct 28, 2009 9:49 am 
Newbie

Joined: Tue Oct 27, 2009 12:20 pm
Posts: 4
Found the answer myself.

in the parent:
Code:
    @CollectionOfElements()
      @JoinTable(name = "parent_child", joinColumns = { @JoinColumn(name = "parent_id") })
      @MapKey(columns = { @Column(name = "name") })
      @Column(name = "value")
    private Map<Integer, Child> fvalues = new HashMap<Integer, Child>();


a new table "parent_child" is made and the keys will be stored there.

greetz,
HellCode


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.