Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Problems Mapping Map

JPA 1.0 does not support entities being the Key of a map but EclipseLink should. Do Class1 and Class2 share PK's? How would you write SQL based on your knowledge of the table structure? Can you provide an example? It appears from your your table description that your data may not support this type of mapping but if they do share PK's then you could map a OneToOne to Class1 from Class2 and use the MapKey to specify that mapping. Make sure that Class1 correctly implements hashcode and equals.
--Gordon

fed wrote:
Hi,
I have some problems to map an entity that contains a Map association.

The problems is how it maps this association (the code of the classes
that i use is at the end of message)  :

On the database i have 4 tables,  class1, class2, class3 and
class3_class2 that maps the association in the map but this last table
has only 2 column: the class3_id and the hm_id and there is no
reference to Class1 that is the key of the Map, there is not a column
that indicates the MapKey id.

I tried using the @MapKey annotation but has no effect on this.

When i retrieve a stored Class3 object from the database and i look
the keys/values in the Map i see that the keys are the id of the
Class2 object stored in the Map, and the Class1 objects are ignored.

What is wrong with this ?  Seems correct but maybe i miss something.

Thanks for the help.

Bye


These are the classes that i used for test:

// Class1

@Entity
public class Class1 {

        private int id;

        private String string;

        protected Class1(){

        }

        public Class1(String string){
                this.string = string;
        }

        /**
         * @return the id
         */
        @Id
        @GeneratedValue
        public int getId() {
                return id;
        }

        /**
         * @param id the id to set
         */
        public void setId(int id) {
                this.id = id;
        }

        /**
         * @return the string
         */
        public String getString() {
                return string;
        }

        /**
         * @param string the string to set
         */
        public void setString(String string) {
                this.string = string;
        }

        /* (non-Javadoc)
         * @see java.lang.Object#equals(java.lang.Object)
         */
        @Override
        public boolean equals(Object obj) {
                return string.equals(obj);
        }

        /* (non-Javadoc)
         * @see java.lang.Object#hashCode()
         */
        @Override
        public int hashCode() {
                return string.hashCode();
        }

        /* (non-Javadoc)
         * @see java.lang.Object#toString()
         */
        @Override
        public String toString() {
                return string;
        }
}


// Class2

@Entity
public class Class2 {

        private String string;

        private int id;


        protected Class2(){

        }

        public Class2(String string){
                this.string = string;
        }

        /**
         * @return the string
         */
        public String getString() {
                return string;
        }

        /**
         * @param string the string to set
         */
        public void setString(String string) {
                this.string = string;
        }

        /**
         * @return the id
         */
        @Id
        @GeneratedValue
        public int getId() {
                return id;
        }

        /**
         * @param id the id to set
         */
        public void setId(int id) {
                this.id = id;
        }

        /* (non-Javadoc)
         * @see java.lang.Object#equals(java.lang.Object)
         */
        @Override
        public boolean equals(Object obj) {
                return string.equals(obj);
        }

        /* (non-Javadoc)
         * @see java.lang.Object#hashCode()
         */
        @Override
        public int hashCode() {
                return string.hashCode();
        }

        /* (non-Javadoc)
         * @see java.lang.Object#toString()
         */
        @Override
        public String toString() {
                return string;
        }

}

// Class3

@Entity
public class Class3 {

        private int id;

        private Map<Class1,Class2> hm;

        public Class3(){
                hm = new HashMap<Class1,Class2>();
        }

        /**
         * @return the id
         */
        @Id
        @GeneratedValue
        public int getId() {
                return id;
        }

        /**
         * @param id the id to set
         */
        public void setId(int id) {
                this.id = id;
        }

        /**
         * @return the hm
         */
        @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
        public Map<Class1, Class2> getHm() {
                return hm;
        }

        /**
         * @param hm the hm to set
         */
        public void setHm(Map<Class1, Class2> hm) {
                this.hm = hm;
        }

        public void add_key_value(Class1 c1, Class2 c2){

                hm.put(c1, c2);
        }
}
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users



Back to the top