Avatar billede fredand Forsker
11. maj 2012 - 16:32 Der er 2 kommentarer og
1 løsning

How to implement many to many relationship with hibernate?

Hello guys!

Is this a bad solution/workaround to implement a many to many relationship?

Correct me if I'm wrong but normaly we should avoid many to many relationships.
And to avid it we implement a "connection"-table

Please consider this example.

Order <- OrderRows -> Article

ORDER
*id
name

ORDERROWS
*fk_order_id
*fk_article_id
quantity

ARTICLE
id
name
price

A order now could have many articles and an article could could exist in many orders.

How ever this seems hard to implement in hibernate with a composite key in the ORDERROWS table.

I did it like this:

@Entity
@Table(name = "Orders")
public class Orders {
    @Id
    @GeneratedValue
    private String id;
    private String name;
}


@Entity
@Table(name = "ARTICLES")
public class Article {
    @Id
    @GeneratedValue
    private String id;
    private String name;
    private String price;
}

I alter the table ORDERROWS to have one primary key instead of a composite key

ORDERROWS
*id
fk_order_id
fk_article_id
quantity

And create a unique index over the two fk-keys to make sure there will be no duplicate rows.

@Entity
@Table(name = "ORDERROWS")
public class OrderRows {
   
    @Id
    @GeneratedValue
    private String id;
   
    @ManyToOne
    @JoinColumn(name="fk_orderId")
    private Order order;

    @ManyToOne
    @JoinColumn(name="fk_articleId")
    private Article article;
   
    private String quantity;
}

What I can see the drawback to this is that I will get en extra column in the DB that consumes memory.

I do not find any other solutions like mine so I bet there might be a good reason and that I'm wrong somewhere.
If you see any "stupidness" or otehr drawbacks in this workaround please let me know.

Best regards
Fredrik
Avatar billede arne_v Ekspert
13. maj 2012 - 01:35 #1
I believe that you need:
* 3 database table
* 2 classes
to implement M:M.

See:
  http://docs.jboss.org/hibernate/orm/3.5/reference/en-US/html/associations.html#assoc-bidirectional-join-m2m

Where you have a collection of the other class in both classes and Hibernate handles everything for you.
Avatar billede fredand Forsker
15. maj 2012 - 21:16 #2
Hello Arne!

You were right!

I newer needed the class OrderRows.

For other that might be interested, I also found this example:
http://www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-many-to-many-using-annotations-1.html
...but that never worked without this article:
http://www.coderanch.com/t/425130/ORM/java/Could-not-determine-type-java
...since I ran into the same problem.

Please give svar, so I can reward you!

Best regards
Fredrik
Avatar billede arne_v Ekspert
15. maj 2012 - 21:39 #3
svar
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester