persistence@glassfish.java.net

Re: Problems Mapping Map

From: Marina Vatkina <Marina.Vatkina_at_Sun.COM>
Date: Thu, 03 Apr 2008 17:09:38 -0700

How does the combination of class3_id and the hm_id define to association
between Class1 and Class2?

thanks,
-marina

fedr 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);
> }
> }