• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Field doesn't have a default value with manyToMany Hibernate relationship

 
Samar Land
Ranch Hand
Posts: 60
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In manyToMany relationship, When I'm trying to add a new driver with selected vehicle from dropdown list, it's throwing an exception
java.sql.SQLException: Field 'vehicle_id' doesn't have a default value

However, if I set a default value in the DB, It works fine by inserting two different rows to the (driver_series_stats_xref) table, one for what I’m trying to add and other row for the default value that I set in the db.

Entity.java





the create table:


driverDao.java



driverService.java






controller.java(post method)



error meesage
HTTP Status 500 - Request processing failed; nested exception is org.springframework.orm.hibernate4.HibernateJdbcException: JDBC exception on Hibernate data access: SQLException for SQL [n/a]; SQL state [HY000]; error code [1364]; could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement

and
java.sql.SQLException: Field 'vehicle_id' doesn't have a default value
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here you'll find a detailed example of a ManyToMany relationship.

Is it intentional that you use driver_series_stats_xref as the join table, but that you provide the create script of another table?
 
Samar Land
Ranch Hand
Posts: 60
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:Here you'll find a detailed example of a ManyToMany relationship.

Is it intentional that you use driver_series_stats_xref as the join table, but that you provide the create script of another table?

I was trying to recreate the xref table in case I did something wrong, but didn't work ND BACK IT AS IT.

I will check the link, thanks for this.
 
Samar Land
Ranch Hand
Posts: 60
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any suggestions to solve this issue, I'm not sure yet what's going on here, and why it's working when I set the default value in the db.
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you increase the logging of your application to see which SQL statements your ORM tool is generating...
 
Samar Land
Ranch Hand
Posts: 60
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's the stack trace. These are not the SQL statements generated by the ORM tool
 
Samar Land
Ranch Hand
Posts: 60
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this issue fixed by deleting the add to list line in myService.

driver.getListOfVehicles().add(vehicle);
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Samar Land wrote:I have this issue fixed by deleting the add to list line in myService.

driver.getListOfVehicles().add(vehicle);

If that line of code is removed, how did you add the vehicle to the driver's list of vehicles?
 
Samar Land
Ranch Hand
Posts: 60
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, it's a many to many relationship, I created a new Domain, Dao,Service for the DriverVehicle and I called the addOrUpdate method in DriverService to save the vehicle. (I have the vehicle as a dropdown list in my JSP) so All what I have to do is passing the vehicle id and then save the relation in DriverVehicl table)

DriverVehicle.java



DriverService.java

Hope this will be useful for other people.

Sorry for not explain all that in my previous post, but I was excited
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Samar Land wrote:Hope this will be useful for other people.

Thanks for sharing your solution. It might indeed be helpful to other ranchers. Have a cow!

Samar Land wrote:well, it's a many to many relationship, I created a new Domain, Dao,Service for the DriverVehicle and I called the addOrUpdate method in DriverService to save the vehicle. (I have the vehicle as a dropdown list in my JSP) so All what I have to do is passing the vehicle id and then save the relation in DriverVehicl table)

It doesn't matter if it's a many-to-many relationship. I'm a bit surprised you need to add a new domain, dao and service for DriverVehicle. I would expect to be able to create a many-to-many mapping without these additional classes, just using the Driver and Vehicle entities with an appropriate mapping (as explained here).
 
Samar Land
Ranch Hand
Posts: 60
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would expect to be able to create a many-to-many mapping without these additional classes, just using the Driver and Vehicle entities with an appropriate mapping

You're right, it works for me with other classes(driver - news)
All what I needed is mapping the Driver in news class.

I had to create the other classes to handle an extra columns, but my issue was before that.



Yaay! thanks
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Samar Land wrote:I had to create the other classes to handle an extra columns, but my issue was before that.

Then you definitely need an additional entity, as illustrated in the Mapping a Join Table with Additional Columns section.
 
Samar Land
Ranch Hand
Posts: 60
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right, Also I've learned that when I had more than two keys in my reference table I had to create entity.

Like (vehicleId, driverId, teamId) the three keys are set up in DB as primary key, So I needed to create a new entity to handle the primary key with @Id annotation.


It's working now for all my forms! Thanks everybody for the help
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Samar Land wrote:It's working now for all my forms!

Awesome!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic