• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Spring hibernate ManyToMany with extra fields

 
Milan Strahinic
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on one demo project which has Driver and Licenses connected ManyToMany. Driver can have more licenses and one license can be connected to more drivers. This is not a driver licence. The license I am talking about is connected with the cargo that they can, or cannot transport. That is the way it should be.
Recently, I had one request to add two extra fields in this connection. Driver and License are connected ManyToMany by table Drivers_License. That extra filed goes to Drivers_License and that is expirationDate, and stateIssued.

This is the look of my database now.


The problem is that I need to break ManyToMany connection and create two OneToMany connections. I also need composite key to be made from driverID and licenseID.

This is the example of that I am talking about.
Hibernate Many-to-Many Association with Extra Columns in Join Table Example

Can you tell me if there is some complete example how to finish this using spring and hibernate, or maybe do you know some example that can handle this by using ManyToMany on a classical way?
 
Dave Tolls
Ranch Hand
Posts: 2091
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The key to this is to get the Driver_License mapping correct.
The Driver and License ones should be easy after that as they will simply be standard OneToMany.

Have you created the composite key?

What does your DriverLicense class look like and maybe we can work through it?
 
Milan Strahinic
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my Driver:




And here is my License:


Also DriverLicense has composite key so it is from two parts:

DriverLicenseID



DriverLicense




My problem is that in dao layer I have problems with DriverLicense get method.
Those two methods return null values somehow.



Maybe I am doing something wrong. Don't know what is the right way to work with composite key.
 
Tim Holloway
Saloon Keeper
Posts: 18359
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that a lot of confusion might be avoided if your "License" object was renamed to something like "TransportLicense". It's psychologically confusing to call it "License" and doubly so if there's a real Driver's License in the mixture.
 
Milan Strahinic
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have any good example, that can be found on internet, and that has Entities, hibernate, spring and GUI? I was searching but there are a lot of examples without GUI. I think that my problems are connected with Entities.
 
Milan Strahinic
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Like I promised here is my solution.

For my particular problem I have chosen this : http://www.codejava.net/frameworks/hibernate/hibernate-many-to-many-association-with-extra-columns-in-join-table-example

This solves work with hibernate. I have also used DTO to repack beans. This example here has one mistake, and that is that they are not using LAZY in composite ID creation. That is why this example has infinite loop problem.

Here is my code that solves this.

Driver entity



DriverLicenseID (my composite key)



License entity



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic