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

How do I map two collections of the same Entity in Hibernate?

 
Dmitry Zhuravlev
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings!

Gentlemen, here is a problem I have encountered.

I have this mapped Entity class:


and also I have an Action class:

These mappings look very simple for me. But they dont work!

When I try to save some Manager instance the Exception is thrown:
org.springframework.jdbc.UncategorizedSQLException: Hibernate operation: Could not execute JDBC batch update; uncategorized SQLException for SQL [insert into Manager_Action (Manager_id, actions_id) values (?, ?)]; SQL state [HY000]; error code [1364]; Field 'reactions_id' doesn't have a default value; nested exception is java.sql.BatchUpdateException: Field 'reactions_id' doesn't have a default value


Hibernate creates the following scheme from my mappings: managers table, actions table and manager_action table which consists of 3 columns: manager_id, action _id, reaction_id. So the problem is somewhere here... But I have specified GeneratorType from Action entity and it should use it for reaction_id. Whats my fault?

 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a test you could edit your database and set a default value for the reactions_id column. Just for test.
 
Dmitry Zhuravlev
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, the problem was that Hibernate created link table with 3 columns: manager_id, action_id,reaction_id and was trying to insert there a row with only two values. I used @JoinTable annotation to provide two join table names to create two sepaarte join tables and it fixed the problem. I may guess that another way is to force Hibernate to add default value attribute to the reaction_id column, I tried to do this using @Column(columnDefinition="int(10) default 1") but after this Hibernate stopped creationg of my scheme in db, I dont know why. Probably one can do it manually in db.
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you put as non required? You will find this parameter at @Column annotation.
 
Dmitry Zhuravlev
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hebert Coelho wrote:Could you put as non required? You will find this parameter at @Column annotation.


Hebert, what do you mean?

I checked column annotation here http://download.oracle.com/javaee/5/api/javax/persistence/Column.html#nullable() and have not found any required-looking parameters..
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ops, nullable = false (required). (:

Try to put nullable = true.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic