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?