These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: One-To-Many relation, nullable=false doesn't work
PostPosted: Tue Apr 15, 2008 11:33 am 
Newbie

Joined: Wed Oct 17, 2007 4:59 pm
Posts: 8
Althought I tried to find a solution in this forum, I can't make it run.

What I want to do is to build a simple one-to-many association between the two entities: malfunction and activity, where 1 malfunction can have 1:n activities.

I want to manage the creation, update, etc. from the parent (malfunction).

Here my code:

Code:
Malfunction.java

   @OneToMany(fetch = FetchType.EAGER, cascade = javax.persistence.CascadeType.ALL)
   @Cascade( { CascadeType.ALL, CascadeType.DELETE_ORPHAN})
   @JoinColumn(name = "MALFUNCTION_ID")
   @Fetch(FetchMode.SUBSELECT)
   private Set<Activity> activities = new HashSet<Activity>();

Activity.java

   @ManyToOne(optional=false)
   @JoinColumn(name = "MALFUNCTION_ID", nullable=false, insertable=false, updatable=false)
   @org.hibernate.annotations.ForeignKey(name = "FK_MALFUNCTION_ID")
   private Malfunction malfunction;



When I create a new Activity, put it into the Set via malfunction.getActivities().add(activity) and try to save the malfunction object, I will get the following error:

Code:
Hibernate: insert into ACTIVITY (DESCRIPTION, TITLE, TYPE) values (?, ?, ?)
Caused by: java.sql.SQLException: Field 'MALFUNCTION_ID' doesn't have a default value
   at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1056)
   at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
   at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3378)
   at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3310)
   at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1853)


To sum up:
I want to say, that the malfunction is definatelly not null to make the DELETE_ORPHAN work.

Any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 15, 2008 12:13 pm 
Newbie

Joined: Wed Oct 17, 2007 4:59 pm
Posts: 8
I finally found the solution for the first problem...

I had to insert the nullable=false on the parent as well

Code:
@OneToMany(fetch = FetchType.EAGER, cascade = javax.persistence.CascadeType.ALL)
   @Cascade( { CascadeType.ALL, CascadeType.DELETE_ORPHAN})
   @JoinColumn(name = "MALFUNCTION_ID", nullable=false)
   @Fetch(FetchMode.SUBSELECT)
   private Set<Activity> activities = new HashSet<Activity>();


But now no DELETE_ORPHAN is executed, when I remove the activity from the collection (malfunction).


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 16, 2008 7:44 am 
Newbie

Joined: Wed Oct 17, 2007 4:59 pm
Posts: 8
I found the solution.
The problem was that my GUI sets a new java.util.List while calling setActivities(...). I changed my implementation to this:

Code:
if (activities == null) this.activities = null;
      else{
         getActivities().clear();
         for (Activity activity : activities){
            getActivities().add(activity);
         }
      }


Now, the persistanceBag is still the same, and it works fine


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.