8 Replies - 897 Views - Last Post: 26 February 2016 - 12:23 PM

#1 MStrahinic   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 08-January 16

Spring hibernate ManyToMany with extra fields

Posted 18 February 2016 - 10:53 AM

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.

Driver                          Driver_License                   License
-----------                     -------------------             --------------------
driverID                         driverID                         licenseID
driverName                       licenseID                        licenseName
driverNumber                     expirationDate    
driverDateOfBirth                stateIssued



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.
http://www.codejava....n-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?

Is This A Good Question/Topic? 0
  • +

Replies To: Spring hibernate ManyToMany with extra fields

#2 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: Spring hibernate ManyToMany with extra fields

Posted 18 February 2016 - 11:12 AM

How do you want to able to access the expiration date and state issued in your Java entities? Once you've made your design decision in your business classes it will impact how you do the mappings.
Was This Post Helpful? 0
  • +
  • -

#3 MStrahinic   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 08-January 16

Re: Spring hibernate ManyToMany with extra fields

Posted 18 February 2016 - 12:51 PM

I wanted to put in Driver.class all about DriverLicenses and Licenses. It seams that I have GSON serialization loop when I try to work with DriverLicenses list in Driver.
Do you have any idea what will be the best way to organize my classes?
Was This Post Helpful? 0
  • +
  • -

#4 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: Spring hibernate ManyToMany with extra fields

Posted 18 February 2016 - 01:14 PM

Why are you thinking that two OneToMany relationships is an issue here?
Was This Post Helpful? 0
  • +
  • -

#5 MStrahinic   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 08-January 16

Re: Spring hibernate ManyToMany with extra fields

Posted 19 February 2016 - 10:14 AM

Actually I have managed to do this using OneToMany and connecting Driver and DriverLicense. The last problem that I am facing is with hibernate. In When I select all from DriverLicense table I get DRIVER_LICENSE is not mapped.
Was This Post Helpful? 0
  • +
  • -

#6 MStrahinic   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 08-January 16

Re: Spring hibernate ManyToMany with extra fields

Posted 20 February 2016 - 04:20 PM

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.
Was This Post Helpful? 0
  • +
  • -

#7 MStrahinic   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 08-January 16

Re: Spring hibernate ManyToMany with extra fields

Posted 25 February 2016 - 02:13 PM

have very interesting problem : http://www.codejava....tations-example

I suppose that this example works fine, but I cannot find DAO layer example with hibernate, and I have problem saving composite key into database. Maybe someone has complete example with minimal GUI in spring and JSP that can demonstrate this. Or maybe you have some better example.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15506
  • View blog
  • Posts: 62,086
  • Joined: 12-June 08

Re: Spring hibernate ManyToMany with extra fields

Posted 25 February 2016 - 02:16 PM

Please do not open duplicate topics. Merging.
Was This Post Helpful? 0
  • +
  • -

#9 MStrahinic   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 08-January 16

Re: Spring hibernate ManyToMany with extra fields

Posted 26 February 2016 - 12:23 PM

Ok. Like I promised here is my solution.

For my particular problem I have chosen this : http://www.codejava....n-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

@OneToMany(fetch = FetchType.LAZY, mappedBy = "id.driver", cascade = CascadeType.ALL)

private List<DriverLicense> driverLicense = new ArrayList<DriverLicense>();

public List<DriverLicense> getDriverLicense() {
return driverLicense;

public void setDriverLicense(List<DriverLicense> driverLicense) {
this.driverLicense = driverLicense;
}

DriverLicenseID (my composite key)

@Embeddable
public class DriverLicenseID implements Serializable {

private static final long serialVersionUID = 1L;

@ManyToOne(fetch = FetchType.LAZY, cascade=CascadeType.ALL)
@JoinColumn(name="driverID")
private Driver driver;

public Driver getDriver() {
return driver;
}

public void setDriver(Driver driver) {
this.driver = driver;
}

@ManyToOne(fetch = FetchType.LAZY, cascade=CascadeType.ALL)
private License license;

public License getLicense() {
return license;
}

public void setLicense(License license) {
this.license = license;
}
}

License entity

@OneToMany(fetch = FetchType.LAZY, mappedBy = "id.license", cascade = CascadeType.ALL)

private List<DriverLicense> driverLicense = new ArrayList<DriverLicense>();

public List<DriverLicense> getDriverLicense() {
return driverLicense;
}

public void setDriverLicense(List<DriverLicense> driverLicense) {
this.driverLicense = driverLicense;
}
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1