Hi,
I have a one-to-many unidirectional relationship between two entities.
These are the classes:
Code:
@Entity
@Table(name = "test_document_types")
public class DocumentType {
private Integer id;
private String name;
private Set<PropertyDefinition> propertyDefinitions = Sets.newHashSet();
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(
cascade = CascadeType.ALL,
orphanRemoval = true,
fetch = FetchType.EAGER
)
@JoinColumn(name = "document_type_id")
public Set<PropertyDefinition> getPropertyDefinitions() {
return propertyDefinitions;
}
public void setPropertyDefinitions(Set<PropertyDefinition> propertyDefinitions) {
this.propertyDefinitions = propertyDefinitions;
}
}
Code:
@Entity
@Table(name = "test_property_definitions")
public class PropertyDefinition {
private Integer id;
private String name;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
This is the DDL:
Code:
CREATE TABLE test_document_types (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(32) NOT NULL,
PRIMARY KEY (id)
) ENGINE = InnoDB;
CREATE TABLE test_property_definitions (
id INT NOT NULL AUTO_INCREMENT,
document_type_id INT NOT NULL,
name VARCHAR(32) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (document_type_id) REFERENCES test_document_types (id)
) ENGINE = InnoDB;
As you can see, there is nothing fancy.
The problem is that an exception is thrown when I save a new DocumentType, because of the cascading operation.
This is an example of saving:
Code:
DocumentType documentType = new DocumentType();
documentType.setName("Document type 1");
PropertyDefinition propertyDefinition1 = new PropertyDefinition();
propertyDefinition1.setName("Property 1");
documentType.getPropertyDefinitions().add(propertyDefinition1);
PropertyDefinition propertyDefinition2 = new PropertyDefinition();
propertyDefinition2.setName("Property 2");
documentType.getPropertyDefinitions().add(propertyDefinition2);
getHibernateTemplate().saveOrUpdate(documentType);
... and produces the following output:
Hibernate: insert into test_document_types (name) values (?)
Hibernate: insert into test_property_definitions (name) values (?)
2011-02-02 21:54:20,937 [WARN] org.hibernate.util.JDBCExceptionReporter: SQL Error: 1364, SQLState: HY000
2011-02-02 21:54:20,937 [WARN] org.hibernate.util.JDBCExceptionReporter: SQL Error: 1364, SQLState: HY000
2011-02-02 21:54:20,937 [ERROR] org.hibernate.util.JDBCExceptionReporter: Field 'document_type_id' doesn't have a default value
2011-02-02 21:54:20,937 [ERROR] org.hibernate.util.JDBCExceptionReporter: Field 'document_type_id' doesn't have a default value
Looks like the value for the foreign key of the child is not set before saving.
Hibernate version: 3.5.6
DBMS: MySQL 5.1.41.0