Skip to main content

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

Ok, l thought you were attempting to map pre-existing  tables not generate them.  What you are seeing when you use Hibernate is a Hibernate  specific extension supporting Entities as Map keys when  there is no relation between the Map value and Map key.  This is not supported in the JPA 1.0 specification. 
 EclipseLink will support this type of relationship if there is  a relationship between class1 and class2 and you use the @MapKey annotation like:

@Entity
public class Class2 {
    @Id
    protected int id;
    protected String name;
    @OneToOne
    protected Class1 class1;
   
public class Class3 {
    @Id
    protected int id;
    protected String name;
    @MapKey(name="class1")
    @ManyToMany
    protected Map<Class1, Class2> class2;

--Gordon



fed wrote:
Hi,

first of all i premise that i am not an expert, i am just learning.

The code of the classes that i use for this test is in my first
message, and this is the generated table schema by eclipselink of the
table class3_class2 that maps the Map association:    (database
postgresql)

CREATE TABLE class3_class2
(
  class3_id integer NOT NULL,
  hm_id integer NOT NULL,
  CONSTRAINT class3_class2_pkey PRIMARY KEY (class3_id, hm_id),
  CONSTRAINT fk_class3_class2_class3_id FOREIGN KEY (class3_id)
      REFERENCES class3 (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT fk_class3_class2_hm_id FOREIGN KEY (hm_id)
      REFERENCES class2 (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (OIDS=FALSE);

as you can see in this table there is no reference to Class1 and it
doesn't know the key of the map.

What it's strange for me is that if i use hibernate, same classes of
the test, same annotation without any hibernate extension i get this
schema generated by hibernate:

CREATE TABLE class3_class2
(
  class3_id integer NOT NULL,
  hm_id integer NOT NULL,
  mapkey_id integer NOT NULL,
  CONSTRAINT class3_class2_pkey PRIMARY KEY (class3_id, mapkey_id),
  CONSTRAINT fk396040be7a38f2a7 FOREIGN KEY (class3_id)
      REFERENCES class3 (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT fk396040be80c8af1c FOREIGN KEY (hm_id)
      REFERENCES class2 (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT fk396040bed96997dd FOREIGN KEY (mapkey_id)
      REFERENCES class1 (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (OIDS=FALSE);

where there is the mapkey_id column that reference to class1 and this
works when i test it.

I don't know why it is different but i want to use eclipselink not hibernate.


You asked me if Class1 and Class2 share the PK, and i am not sure to
understand what you mean, for all the entities that i map i use @Id
@GeneratedValue on the id accessor.
Can you please explain me what do you mean?

For the hashcode and equals i think is ok, Class1 and Class2 has only
a String field and i use hashcode and equals based on it.

Thanks for the help
Bye



2008/4/4, Gordon Yorke <gordon.yorke@xxxxxxxxxx>:
  
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
    
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users
  

Back to the top