Hacker News new | ask | show | jobs
by mahmud 5323 days ago
Hibernate. I learned JPA months after I wrote most of the apps. What can I say, growing pains. But it's understandable given the size of our project.
1 comments

Thanks for the offer. I've also just started using Play at work and one question has been driving me nuts: I'm trying to form a many-to-many relationship with JPA.

I have two models, with a third model to represent the relationship (can't just use @ManyToMany as the relationship has attributes). The primary key on the relationship model is a composite primary key of two other models. When I add a new relationship model to the @OneToMany set on one of the first models, can I get the relationship model's key to change automatically, or do I have to do it manually?

e.g. with three models like (psuedo-Java-Scala coming up)

    class Order(val customerName)
        @OneToMany
        Set<OrderItem> items;
    class Item(itemDescription)
        @OneToMany
        Set<OrderItem> orders;
    class OrderItem(val quantity)
        @Id
        @PrimaryJoinColumn
        @ManyToOne
        Order order;
        @Id
        @PrimaryJoinColumn
        @ManyToOne
        Item item;

    OrderItem oi;
    oi.item = bananas;
    order.items.add(oi);
    order.save(); // This saves oi, but with a null value for order_id
    oi.order = order; //This seems redundant, but the save is wrong without it.
It seems like I have to do it manually, but to my mind adding it to the Set should be enough information to change the keys. Is my thinking wrong or am I using JPA wrong?
Yep, you figured it out. JPA requires you to set it on both sides. (Well, really you only HAVE to set it on the owning side of the association, which is what Hibernate monitors, but you should do both. Otherwise, a future programmer may be surprised that the objects don't have a consistent view of the association later on.
Yeah, I can't believe JPA/most ORMs don't maintain both sides of a relationship. That sort of stuff should just work.

I implemented my own ORM (http://joist.ws) a few years ago to solve this and my other JPA/Hibernate annoyances. It works really well, modulo the fact that I suck at docs/promotion/etc. so it doesn't have many users (working on that).

Also note that, specific for the playframework, someone seems to have implemented the necessary magic in a plugin:

http://www.playframework.org/modules/associations

Which is nifty, other than I'm slightly wary of the "magic that happens via runtime class rewriting" that seems to be Play's standard way of doing things. The results are admittedly impressive, but it seems like you're coupling yourself to yet-another-runtime-environment/container for things to work right.

I had a similar problem to this, it pissed me off the ages although I did spend allot of time trying to do it with @ManyToMany as having a separate Entity just for the relationship seemed like bloat to me. As I recommended before, read the book "Java Persistence with Hibernate" and things will start to make more sense.

Hibernate is generally far more concerned with being flexible and robust than it is with being DRY or providing a perfect database abstraction. You really do still have to think about your data from the point of view of the DBMS regards 'Owning' Associations etc.

One thing with Play! is that it does try to over abstract hibernate a little bit and turn it into ActiveRecord which is fine for simple things but hibernate is not like ActiveRecord!

B-b-b-but DRY. Okay, thanks for reassuring me I'm not doing it wrong and there's not a @UpdateAutomaticallyLikeActiveRecord annotation I'm missing or something :)