Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Not sure how to best create many to one relationship

 
Mike London
Ranch Hand
Posts: 1227
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Each time a user visits our Website, his user id and password is looked up in the Users class (after Hibernate query). This part works now.

-----------

Also, we want to add a record would to the UsersVisitHistory table showing that this user visited the site on a particular date.

I'm not sure how to represent the many to one relationship in the Users.java class.

Would I just include a reference to the UserVisitHistory class like below and create an instance of the UserVisitHistory class and fill it in and assign it to the uvh reference?:

Users.java

// user fields here

// reference the UserVisitHistory class

UserVisitHistory uvh;

// Getters & Setters for class here.

--------------

Then, would I define a many to one mapping in the UserVisitHistory.java class like this?


Then, it's not clear how I should modify my UsersDao so that it will persist not only a new user, but also add the user visit history record?

------

Is there a complete example of something simple say the classic "CUSTOMER ORDER ORDERITEMS" database relationship that's implemented in Hibernate which demonstrates these techniques?

All the Hibernate books I have seem to be more reference and less for learning. The current crop of books seem to focus on a topic by topic "dissection" of hibernate and, unfortunately, do not have a solution based approach so many questions remain.

Where is the O'Reily "Hibernate CookBook"???

Thanks in advance for help or suggestions.

M
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two "One-To-Many" examples on my website tutorials. One maps a simple Team Has Many Players:



Mapping a Team Has Many Players with Hibernate and JPA Annotations

The other maps a more complicated "Client Has Many Address Objects"



Mapping a One to Many Relationship with Hibernate and JPA Annotations


The key is mapping the one-to-many side. I use annotations, so it looks something like this:



The inverse can also be mapped by referencing the foreign key:

@ManyToOne
@JoinColumn(name="team_id")
public Team getTeam() {return team;}


How you save the objects depends on the Cascade type. You can set it up so that when a One is saved, all the many are saved as well, or, you can set it up that each entity must touch the session:



I much prefer a full cascade, where when just the one object is saved, all of the many are saved as well. In this case, you have the same UserDAO, and just save the user. The corresponding objects are cascaded during the save.



More on Hibernate Cascase Types

Happy Hibernating!

-Cameron McKenzie
 
Mike London
Ranch Hand
Posts: 1227
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.

Since annotations aren't the perfect solution in all cases, I'm trying to stick with regular XML configuration files for the moment.

Do you have examples that work strictly with those?

Thanks again.

M
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Mike.

I'm lazy, so I love the annotations. I don't have the files off hand, but pretty much all the JPA attributes have analogous XML attributes.

I hate playing with those XML files.

One thing I might recommend is checking out Hibernate Tools at hibernate.org. There's an Eclipse plugin that will look at your database tables and create the XML mapping files based on your database layout. It will provide the default settings. From there, you've got to use your brain to figure out what makes sense for you - FetchType, CascadeType, and all of those other things. But sometimes, having a basic XML file that works is a great place to start.

Happy Hibernating!

-Cameron McKenzie
 
Mike London
Ranch Hand
Posts: 1227
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Cameron...

I use MyEclipse which has extensive Hibernate capabilities, including a Hibernate perspective with an HQL interface.

However, MyEclipse doesn't help detect one to many relationships and doesn't, by default (or at all?) use annotations, so, for learning at least, I still want to use regular XML files.

In the "Beginning Hibernate, from Novice to Professional" book, they state that annotations aren't the solutions for all situations so XML configuration files still are still needed.

While I can't really debate whether this is true, I'm still stuck trying to find a source that has a problem-solution approach that starts at the beginning using XML configuration files.

With all the learning required to scale Hibernate (a proprietary solution), I'm wondering if straight JDBC, while it takes longer, isn't a reasonable solution.

Thanks again for your reply.

Mike
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you might want to see this 55 post thread.

Why do we need a framework like Hibernate?

Indeed, annotations aren't right for all situations. And Java isn't the right language for all programming situations. But like Java, the benefits of JPA far outweigh the drawbacks, and more times than not, it's one of the right solutions.

I've been working with Hibernate on a number of projects, all but one of which used JPA. I really haven't hit too many things that a creative use of JPA couldn't solve, and even with xml mapping files, sometimes you need to throw a little straight JDBC in there on the rough spots.

I've worked with alot of persistence frameworks. I can't recall too many EJB, entity bean driven projects that didn't leave the development team looking tattered and torn after the application was developed. With Hibernate it's really been different. Many people just find it to be very natural, and they are impressed with its elegance. I'm sure you will be too.

-Cameron McKenzie
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike,
I highly recommend Java Persistence with Hibernate by Bauer and King (the chief developers of Hibernate). You can get a PDF from the manning.com site, or if you have an O'Reilly Safari account, they recently added the book as well.

Also, the Caveat Emptor example application at the Hibernate site is very useful, and will probably be enough to give you the *.hbm.xml examples you need for the one-to-many mapping you're looking for.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a good number of books out there. I agree about the book you just mentioned, but there are some others, and sometimes if you look at other posters signature, you might find a link to their own book.

Mark
 
Mike London
Ranch Hand
Posts: 1227
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the good replies.

I suppose I'm just trying to crawl before I walk. I'd like to start with XML files, then move to annotations.

I suppose it's kind of (but not analogous to) learning algebra before learning trig (again, in the Hibernate case one isn't necessarily a prerequisite of the other).

I understand that the MyEclipse plug-in will generate the one to many mapping automatically when you drag and drop (reverse engineer) the tables, but I must not be using it correctly...

Sigh...

Thanks again for all the great replies!!!

Mike
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic